40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
/**
|
|
* Test-environment setup for @suaveplan/api-migrations.
|
|
*
|
|
* Wires a real in-memory OpenTelemetry MeterProvider BEFORE any source
|
|
* module is imported, so the meter resolved at module load is the real
|
|
* provider's meter — not the noop fallback. Tests that inspect emitted
|
|
* metrics (counter `migrations.applied_total`, histogram
|
|
* `migrations.apply_duration_ms`) read them out of
|
|
* `testInMemoryMetricExporter`.
|
|
*
|
|
* Node ≥18 exposes `crypto.subtle` globally, so no crypto polyfill is
|
|
* required. Server-tier: `node:fs/promises` is available natively.
|
|
*/
|
|
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() {
|
|
await testMeterProvider.forceFlush();
|
|
const collected = testInMemoryMetricExporter.getMetrics();
|
|
testInMemoryMetricExporter.reset();
|
|
return collected;
|
|
}
|