Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

10.8 KB
Raw Blame
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

import staticServe from "@fastify/static";
import opentelemetry from "@opentelemetry/api";
import {
  AggregationType,
  createAllowListAttributesProcessor,
  type DataPoint,
  InMemoryMetricExporter,
  MeterProvider,
  PeriodicExportingMetricReader,
  type ViewOptions
} from "@opentelemetry/sdk-metrics";
import Fastify, { type FastifyInstance } from "fastify";

import { HIGH_CARDINALITY_METER_NAMES, INFISICAL_CORE_METER_ATTRIBUTES } from "./telemetry-attributes";

const mockConfig = {
  OTEL_TELEMETRY_COLLECTION_ENABLED: true,
  OTEL_DROP_HIGH_CARDINALITY_METERS: false
};

vi.mock("@app/lib/config/env", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@app/lib/config/env")>()),
  getConfig: () => mockConfig
}));

const CORE_REQUEST_COUNT = "infisical.core.http.request.count";
const LEGACY_REQUEST_COUNT = "infisical.http.server.request.count";

let reader: PeriodicExportingMetricReader;

// Mirrors the InfisicalCore half of initTelemetryInstrumentation(). The View is under test as much as the
// plugin is: an attribute added at a call site is only bounded because the allowlist drops it. The HTTP
// instrumentation meter is deliberately NOT wired up here, because nothing in this file registers
// HttpInstrumentation and a View with no instrument behind it would imply coverage that does not exist.
// That pipeline is covered by http-instrumentation.test.ts and http-semconv-baseline.test.ts.
//
// Two things make a per-test provider necessary, and both fail silently rather than erroring:
// the OpenTelemetry API ignores a second setGlobalMeterProvider() unless the global is disabled first,
// and metrics.ts memoizes each meter and instrument on first use, so an instrument created against an
// earlier provider keeps recording into it. Without disable() + resetModules() only the first test in
// the file observes anything and the rest pass or fail for the wrong reason.
const installMeterProvider = () => {
  opentelemetry.metrics.disable();
  const views: ViewOptions[] = [
    {
      meterName: "InfisicalCore",
      attributesProcessors: [createAllowListAttributesProcessor(INFISICAL_CORE_METER_ATTRIBUTES)]
    }
  ];
  if (mockConfig.OTEL_DROP_HIGH_CARDINALITY_METERS) {
    HIGH_CARDINALITY_METER_NAMES.forEach((meterName) => {
      views.push({
        meterName,
        aggregation: { type: AggregationType.DROP },
        attributesProcessors: [createAllowListAttributesProcessor([])]
      });
    });
  }
  vi.resetModules();
  reader = new PeriodicExportingMetricReader({
    exporter: new InMemoryMetricExporter(0),
    exportIntervalMillis: 60_000
  });
  opentelemetry.metrics.setGlobalMeterProvider(new MeterProvider({ readers: [reader], views }));
};

const buildApp = async (extend?: (app: FastifyInstance) => Promise<void>): Promise<FastifyInstance> => {
  // Imported after the provider is installed so the plugin binds its instruments to this test's reader.
  const { apiMetrics } = await import("@app/server/plugins/api-metrics");
  const app = Fastify();
  await app.register(apiMetrics);
  app.get("/api/v1/workspace/:workspaceId", async () => ({ ok: true }));
  app.get("/api/v1/boom", async () => {
    throw new Error("boom");
  });
  await extend?.(app);
  await app.ready();
  return app;
};

let collected = false;

const takeSnapshot = async () => {
  if (collected) {
    throw new Error(
      "reader.collect() was already called in this test. The reader is delta, so this snapshot is empty. Take one snapshot and read every metric out of it (see collectPair)."
    );
  }
  collected = true;
  const { resourceMetrics } = await reader.collect();
  return resourceMetrics.scopeMetrics.flatMap((scope) => scope.metrics);
};

// Returns every data point recorded for a metric, across all attribute sets.
const collect = async (metricName: string): Promise<DataPoint<number>[]> => {
  const metrics = await takeSnapshot();
  return metrics
    .filter((metric) => metric.descriptor.name === metricName)
    .flatMap((metric) => metric.dataPoints as DataPoint<number>[]);
};

// Reads two metrics out of one snapshot, which is the only way to assert on both.
const collectPair = async (first: string, second: string): Promise<[DataPoint<number>[], DataPoint<number>[]]> => {
  const metrics = await takeSnapshot();
  const pointsFor = (name: string) =>
    metrics
      .filter((metric) => metric.descriptor.name === name)
      .flatMap((metric) => metric.dataPoints as DataPoint<number>[]);
  return [pointsFor(first), pointsFor(second)];
};

describe("api-metrics plugin", () => {
  // The first import transforms the plugin's whole module graph, which can outlast a test's timeout on
  // its own. Each test still re-imports after resetModules(), so it binds to its own provider.
  beforeAll(async () => {
    await import("@app/server/plugins/api-metrics");
  }, 30_000);

  beforeEach(() => {
    collected = false;
    mockConfig.OTEL_TELEMETRY_COLLECTION_ENABLED = true;
    mockConfig.OTEL_DROP_HIGH_CARDINALITY_METERS = false;
  });

  afterEach(() => {
    opentelemetry.metrics.disable();
  });

  test("records a successful response with the bounded label set", async () => {
    installMeterProvider();
    const app = await buildApp();
    await app.inject({ method: "GET", url: "/api/v1/workspace/abc-123" });

    const points = await collect(CORE_REQUEST_COUNT);
    expect(points).toHaveLength(1);
    expect(points[0].attributes).toStrictEqual({
      "http.request.method": "GET",
      "http.route": "/api/v1/workspace/:workspaceId",
      "http.response.status_code": 200
    });
    await app.close();
  });

  test("records an error response, and keeps the route template", async () => {
    installMeterProvider();
    const app = await buildApp();
    await app.inject({ method: "GET", url: "/api/v1/boom" });

    const points = await collect(CORE_REQUEST_COUNT);
    expect(points).toHaveLength(1);
    expect(points[0].attributes["http.response.status_code"]).toBe(500);
    expect(points[0].attributes["http.route"]).toBe("/api/v1/boom");
    await app.close();
  });

  test("normalizes a method outside the semconv set to _OTHER", async () => {
    installMeterProvider();
    const app = await buildApp();
    // PROPFIND is one of the 35 methods Node's parser accepts but semconv does not name.
    await app.inject({ method: "PROPFIND" as "GET", url: "/api/v1/no-such-route" });

    const points = await collect(CORE_REQUEST_COUNT);
    expect(points).toHaveLength(1);
    expect(points[0].attributes["http.request.method"]).toBe("_OTHER");
    await app.close();
  });

  test("normalizes only the core counter, leaving the per-actor meter's method raw", async () => {
    installMeterProvider();
    const app = await buildApp();
    await app.inject({ method: "PROPFIND" as "GET", url: "/api/v1/no-such-route" });

    // The core counter is normalized so its labels join against the OTel HTTP instrumentation. The legacy
    // meter is not, because folding PROPFIND onto _OTHER there would erase the scanner traffic that meter
    // exists to attribute, and the raw method is bounded by Node's own parser anyway.
    const [core, legacy] = await collectPair(CORE_REQUEST_COUNT, LEGACY_REQUEST_COUNT);
    expect(core[0].attributes["http.request.method"]).toBe("_OTHER");
    expect(legacy).toHaveLength(1);
    expect(legacy[0].attributes["http.request.method"]).toBe("PROPFIND");
    await app.close();
  });

  test("collapses distinct path parameters onto one series", async () => {
    installMeterProvider();
    const app = await buildApp();
    for (const id of ["a", "b", "c", "d", "e"]) {
      // eslint-disable-next-line no-await-in-loop
      await app.inject({ method: "GET", url: `/api/v1/workspace/${id}` });
    }

    const points = await collect(CORE_REQUEST_COUNT);
    expect(points).toHaveLength(1);
    expect(points[0].value).toBe(5);
    await app.close();
  });

  test("collapses unmatched paths onto http.route=unknown, never the raw path", async () => {
    installMeterProvider();
    const app = await buildApp();
    for (const suffix of ["one", "two", "three"]) {
      // eslint-disable-next-line no-await-in-loop
      await app.inject({ method: "GET", url: `/api/v1/missing-${suffix}` });
    }

    const points = await collect(CORE_REQUEST_COUNT);
    expect(points).toHaveLength(1);
    expect(points[0].attributes["http.route"]).toBe("unknown");
    expect(points[0].value).toBe(3);
    await app.close();
  });

  test("keeps recording the core counter when the high-cardinality meters are dropped", async () => {
    mockConfig.OTEL_DROP_HIGH_CARDINALITY_METERS = true;
    installMeterProvider();
    const app = await buildApp();
    await app.inject({ method: "GET", url: "/api/v1/workspace/abc-123" });

    // The denominator has to survive the kill switch; that is the whole point of the split.
    const [core, legacy] = await collectPair(CORE_REQUEST_COUNT, LEGACY_REQUEST_COUNT);
    expect(core).toHaveLength(1);
    expect(core[0].value).toBe(1);
    expect(legacy).toHaveLength(0);
    await app.close();
  });

  test("ignores static asset routes so hashed filenames never become http.route", async () => {
    // STANDALONE_MODE serves the built frontend from the API, and serve-ui.ts registers @fastify/static
    // with wildcard: false, which declares one route per file. Recording those would put a content-hashed
    // filename in http.route on the meter documented as the bounded one, and would make the counter mean
    // something different depending on whether the deployment serves its own UI.
    const assetDir = fs.mkdtempSync(path.join(os.tmpdir(), "api-metrics-assets-"));
    fs.mkdirSync(path.join(assetDir, "assets"));
    const assetNames = ["app-Bmt1dKu1.js", "vendor-YXR_I9Ib.js", "styles-hwHhd4i8.css"];
    assetNames.forEach((name) => fs.writeFileSync(path.join(assetDir, "assets", name), "// built asset"));

    installMeterProvider();
    const app = await buildApp(async (instance) => {
      await instance.register(staticServe, { root: assetDir, wildcard: false, index: false });
    });

    for (const name of assetNames) {
      // eslint-disable-next-line no-await-in-loop
      const res = await app.inject({ method: "GET", url: `/assets/${name}` });
      expect(res.statusCode).toBe(200);
    }
    await app.inject({ method: "GET", url: "/api/v1/workspace/abc-123" });

    const points = await collect(CORE_REQUEST_COUNT);
    expect(points).toHaveLength(1);
    expect(points[0].attributes["http.route"]).toBe("/api/v1/workspace/:workspaceId");

    await app.close();
    fs.rmSync(assetDir, { recursive: true, force: true });
  });

  test("records nothing at all when telemetry is disabled", async () => {
    mockConfig.OTEL_TELEMETRY_COLLECTION_ENABLED = false;
    installMeterProvider();
    const app = await buildApp();
    await app.inject({ method: "GET", url: "/api/v1/workspace/abc-123" });

    expect(await collect(CORE_REQUEST_COUNT)).toHaveLength(0);
    await app.close();
  });
});