46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
import type { Counter, Histogram } from "@opentelemetry/api";
|
|
import { metrics } from "@opentelemetry/api";
|
|
|
|
const METRIC_CONVERT_TOTAL = "emblem_solver.convert.total";
|
|
const METRIC_CONVERT_DURATION = "emblem_solver.convert.duration_ms";
|
|
|
|
let convertCounter: Counter | undefined;
|
|
let convertDuration: Histogram | undefined;
|
|
|
|
/** Lazy access to the `convert.total` counter. */
|
|
export function getConvertCounter(): Counter {
|
|
if (!convertCounter) {
|
|
const meter = metrics.getMeter("@suaveplan/emblem-solver");
|
|
convertCounter = meter.createCounter(METRIC_CONVERT_TOTAL, {
|
|
description: "Total number of convert() invocations",
|
|
});
|
|
}
|
|
return convertCounter;
|
|
}
|
|
|
|
/** Lazy access to the `convert.duration_ms` histogram. */
|
|
export function getConvertDuration(): Histogram {
|
|
if (!convertDuration) {
|
|
const meter = metrics.getMeter("@suaveplan/emblem-solver");
|
|
convertDuration = meter.createHistogram(METRIC_CONVERT_DURATION, {
|
|
description: "Duration of convert() in milliseconds",
|
|
unit: "ms",
|
|
});
|
|
}
|
|
return convertDuration;
|
|
}
|
|
|
|
/** Internal test helper: reset the cached instruments. */
|
|
export function __resetTelemetryForTests(): void {
|
|
convertCounter = undefined;
|
|
convertDuration = undefined;
|
|
}
|
|
|
|
/** Internal test helper: query the cached instruments. */
|
|
export function __peekTelemetryForTests(): {
|
|
counter: Counter | undefined;
|
|
histogram: Histogram | undefined;
|
|
} {
|
|
return { counter: convertCounter, histogram: convertDuration };
|
|
}
|