39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
/**
|
|
* Test-environment setup for @suaveplan/server-keys.
|
|
*
|
|
* Wires a real in-memory OpenTelemetry `MeterProvider` BEFORE any source
|
|
* module is imported, so the meter resolved by `instruments.ts` at module
|
|
* load is the real provider's meter — not the noop fallback. Tests that
|
|
* inspect emitted metrics flush via `flushTestMetrics`.
|
|
*
|
|
* Node ≥18 already exposes `crypto.subtle` and `crypto.getRandomValues`
|
|
* globally, so no crypto polyfill is required.
|
|
*/
|
|
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);
|
|
|
|
/** Force-flush the test meter provider and return the captured snapshots. */
|
|
export async function flushTestMetrics() {
|
|
await testMeterProvider.forceFlush();
|
|
const collected = testInMemoryMetricExporter.getMetrics();
|
|
testInMemoryMetricExporter.reset();
|
|
return collected;
|
|
}
|