/** * Test-environment setup for @suaveplan/broadcast-channel. * * Wires a real in-memory OpenTelemetry `MeterProvider` BEFORE any source * module loads, so the meter resolved at module load is the real provider's * meter. Tests that inspect emitted instruments read them out of the in-memory * exporter via `flushTestMetrics()`. */ import { metrics } from "@opentelemetry/api"; import { AggregationTemporality, InMemoryMetricExporter, MeterProvider, PeriodicExportingMetricReader, } from "@opentelemetry/sdk-metrics"; export const testInMemoryMetricExporter = new InMemoryMetricExporter( AggregationTemporality.CUMULATIVE ); const reader = new PeriodicExportingMetricReader({ exporter: testInMemoryMetricExporter, exportIntervalMillis: 60_000, }); export const testMeterProvider = new MeterProvider({ readers: [reader] }); metrics.setGlobalMeterProvider(testMeterProvider); export async function flushTestMetrics(): Promise< ReturnType > { await testMeterProvider.forceFlush(); const collected = testInMemoryMetricExporter.getMetrics(); testInMemoryMetricExporter.reset(); return collected; }