75 lines
2.3 KiB
TypeScript
75 lines
2.3 KiB
TypeScript
import type { Counter, Meter, MeterProvider } from "@opentelemetry/api";
|
|
import { describe, expect, it } from "@suaveplan/testing/runner";
|
|
import {
|
|
createScalarInstruments,
|
|
recordUiRequest,
|
|
UI_LABEL_VALUE,
|
|
} from "./instruments.js";
|
|
|
|
interface RecordedObservation {
|
|
readonly value: number;
|
|
readonly attributes: Record<string, unknown>;
|
|
}
|
|
|
|
function buildRecordingProvider(): {
|
|
readonly provider: MeterProvider;
|
|
readonly observations: RecordedObservation[];
|
|
} {
|
|
const observations: RecordedObservation[] = [];
|
|
const counter = {
|
|
add(value: number, attributes?: Record<string, unknown>): void {
|
|
observations.push({
|
|
value,
|
|
attributes: { ...(attributes ?? {}) },
|
|
});
|
|
},
|
|
} as unknown as Counter;
|
|
const meter = {
|
|
createCounter(): Counter {
|
|
return counter;
|
|
},
|
|
} as unknown as Meter;
|
|
const provider = {
|
|
getMeter(): Meter {
|
|
return meter;
|
|
},
|
|
} as unknown as MeterProvider;
|
|
return { provider, observations };
|
|
}
|
|
|
|
describe("instruments", () => {
|
|
it("createScalarInstruments wires a counter on the supplied provider", {
|
|
timeout: 5000,
|
|
}, () => {
|
|
const { provider, observations } = buildRecordingProvider();
|
|
const instruments = createScalarInstruments({
|
|
meterProvider: provider,
|
|
});
|
|
recordUiRequest(instruments);
|
|
expect(observations.length).toBe(1);
|
|
expect(observations[0]?.attributes).toEqual({ ui: UI_LABEL_VALUE });
|
|
});
|
|
|
|
it("recordUiRequest labels per-plugin observations", {
|
|
timeout: 5000,
|
|
}, () => {
|
|
const { provider, observations } = buildRecordingProvider();
|
|
const instruments = createScalarInstruments({
|
|
meterProvider: provider,
|
|
});
|
|
recordUiRequest(instruments, { plugin: "billing" });
|
|
expect(observations[0]?.attributes).toEqual({
|
|
ui: UI_LABEL_VALUE,
|
|
plugin: "billing",
|
|
});
|
|
});
|
|
|
|
it("falls back to the OTel global meter provider", {
|
|
timeout: 5000,
|
|
}, () => {
|
|
const instruments = createScalarInstruments();
|
|
expect(instruments.requestsTotal).toBeDefined();
|
|
expect(instruments.meter).toBeDefined();
|
|
});
|
|
});
|