126 lines
4.3 KiB
TypeScript
126 lines
4.3 KiB
TypeScript
import { afterEach, describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
__getApiOtelConfigForTests,
|
|
__resetApiOtelForTests,
|
|
createApiInstrumentations,
|
|
initApiOtel,
|
|
parseOtlpHeaders,
|
|
parseSamplerArg,
|
|
readApiOtelConfig,
|
|
} from "./otel.js";
|
|
|
|
afterEach(() => {
|
|
__resetApiOtelForTests();
|
|
});
|
|
|
|
describe("parseSamplerArg", () => {
|
|
it("returns the fallback for undefined/empty values", () => {
|
|
expect(parseSamplerArg(undefined, 0.5)).toBe(0.5);
|
|
expect(parseSamplerArg("", 0.25)).toBe(0.25);
|
|
});
|
|
|
|
it("returns the fallback for non-numeric input", () => {
|
|
expect(parseSamplerArg("abc", 0.3)).toBe(0.3);
|
|
});
|
|
|
|
it("clamps to [0, 1]", () => {
|
|
expect(parseSamplerArg("-0.5", 0)).toBe(0);
|
|
expect(parseSamplerArg("1.5", 0)).toBe(1);
|
|
});
|
|
|
|
it("returns parsed numbers in range", () => {
|
|
expect(parseSamplerArg("0.42", 0)).toBeCloseTo(0.42);
|
|
});
|
|
});
|
|
|
|
describe("parseOtlpHeaders", () => {
|
|
it("returns undefined for missing input", () => {
|
|
expect(parseOtlpHeaders(undefined)).toBeUndefined();
|
|
expect(parseOtlpHeaders("")).toBeUndefined();
|
|
});
|
|
|
|
it("parses a single key=value pair", () => {
|
|
expect(parseOtlpHeaders("x-sentry-auth=Sentry sentry_key=abc")).toEqual({
|
|
"x-sentry-auth": "Sentry sentry_key=abc",
|
|
});
|
|
});
|
|
|
|
it("parses comma-separated pairs and trims whitespace", () => {
|
|
expect(parseOtlpHeaders(" foo=bar, baz=qux ")).toEqual({ foo: "bar", baz: "qux" });
|
|
});
|
|
|
|
it("skips empty pairs and pairs without `=`", () => {
|
|
expect(parseOtlpHeaders("foo=bar,,no-equals")).toEqual({ foo: "bar" });
|
|
});
|
|
});
|
|
|
|
describe("readApiOtelConfig", () => {
|
|
it("defaults service.name to hivegrid-api", () => {
|
|
expect(readApiOtelConfig({}).serviceName).toBe("hivegrid-api");
|
|
});
|
|
|
|
it("respects OTEL_SERVICE_NAME override", () => {
|
|
expect(readApiOtelConfig({ OTEL_SERVICE_NAME: "api-canary" }).serviceName).toBe("api-canary");
|
|
});
|
|
|
|
it("defaults the traces-sampler to 0.1", () => {
|
|
expect(readApiOtelConfig({}).tracesSamplerArg).toBe(0.1);
|
|
});
|
|
|
|
it("respects OTEL_TRACES_SAMPLER_ARG override", () => {
|
|
expect(readApiOtelConfig({ OTEL_TRACES_SAMPLER_ARG: "1.0" }).tracesSamplerArg).toBe(1);
|
|
expect(readApiOtelConfig({ OTEL_TRACES_SAMPLER_ARG: "0" }).tracesSamplerArg).toBe(0);
|
|
});
|
|
|
|
it("treats an empty endpoint as undefined", () => {
|
|
expect(readApiOtelConfig({ OTEL_EXPORTER_OTLP_ENDPOINT: "" }).endpoint).toBeUndefined();
|
|
});
|
|
|
|
it("prefers SENTRY_ENVIRONMENT over NODE_ENV for deployment.environment", () => {
|
|
expect(readApiOtelConfig({ NODE_ENV: "production", SENTRY_ENVIRONMENT: "canary" }).deploymentEnvironment).toBe("canary");
|
|
});
|
|
|
|
it("derives a service.instance.id with hostname + pid", () => {
|
|
const { serviceInstanceId } = readApiOtelConfig({});
|
|
expect(serviceInstanceId).toMatch(/^.+-\d+$/);
|
|
expect(serviceInstanceId.endsWith(`-${process.pid}`)).toBe(true);
|
|
});
|
|
|
|
it("prefers SENTRY_RELEASE, falling back to APP_REVISION, for service.version", () => {
|
|
expect(readApiOtelConfig({ SENTRY_RELEASE: "sha-1" }).serviceVersion).toBe("sha-1");
|
|
expect(readApiOtelConfig({ APP_REVISION: "sha-2" }).serviceVersion).toBe("sha-2");
|
|
expect(readApiOtelConfig({ SENTRY_RELEASE: "sha-1", APP_REVISION: "sha-2" }).serviceVersion).toBe("sha-1");
|
|
});
|
|
});
|
|
|
|
describe("initApiOtel", () => {
|
|
it("registers only the remote I/O and logging instrumentations used by the API", () => {
|
|
expect(createApiInstrumentations().map((instrumentation) => instrumentation.instrumentationName)).toEqual([
|
|
"@opentelemetry/instrumentation-http",
|
|
"@opentelemetry/instrumentation-undici",
|
|
"@opentelemetry/instrumentation-pg",
|
|
"@opentelemetry/instrumentation-ioredis",
|
|
"@opentelemetry/instrumentation-aws-sdk",
|
|
"@opentelemetry/instrumentation-pino",
|
|
]);
|
|
});
|
|
|
|
it("is a no-op when OTEL_EXPORTER_OTLP_ENDPOINT is unset", () => {
|
|
const sdk = initApiOtel({
|
|
endpoint: undefined,
|
|
headers: undefined,
|
|
serviceName: "test",
|
|
serviceVersion: undefined,
|
|
serviceInstanceId: "test-1",
|
|
deploymentEnvironment: undefined,
|
|
serviceNamespace: "hivegrid",
|
|
tracesSamplerArg: 0,
|
|
debug: false,
|
|
});
|
|
expect(sdk).toBeUndefined();
|
|
// Cached config is still recorded so the test contract can be asserted.
|
|
expect(__getApiOtelConfigForTests()?.serviceName).toBe("test");
|
|
});
|
|
});
|