Skip to content

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

5.5 KB
Raw Blame
import opentelemetry, { diag, DiagConsoleLogger, DiagLogLevel } from "@opentelemetry/api";
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-proto";
import { PrometheusExporter } from "@opentelemetry/exporter-prometheus";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { HttpInstrumentation } from "@opentelemetry/instrumentation-http";
import { RuntimeNodeInstrumentation } from "@opentelemetry/instrumentation-runtime-node";
import { defaultResource, resourceFromAttributes } from "@opentelemetry/resources";
import {
  AggregationTemporality,
  AggregationType,
  createAllowListAttributesProcessor,
  MeterProvider,
  PeriodicExportingMetricReader,
  type ViewOptions
} from "@opentelemetry/sdk-metrics";
import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from "@opentelemetry/semantic-conventions";
import tracer from "dd-trace";
import dotenv from "dotenv";

import { getTelemetryConfig } from "../config/env";
import {
  HIGH_CARDINALITY_METER_NAMES,
  HTTP_INSTRUMENTATION_METER_ATTRIBUTES,
  HTTP_INSTRUMENTATION_METER_NAME,
  INFISICAL_CORE_METER_ATTRIBUTES,
  METER_AGGREGATION_CARDINALITY_LIMIT,
  resolveHttpSemconvOptIn
} from "./telemetry-attributes";

dotenv.config();

const initTelemetryInstrumentation = ({
  exportType,
  otlpURL,
  otlpUser,
  otlpPassword,
  otlpPushInterval,
  dropHighCardinalityMeters
}: {
  exportType?: string;
  otlpURL?: string;
  otlpUser?: string;
  otlpPassword?: string;
  otlpPushInterval?: number;
  dropHighCardinalityMeters?: boolean;
}) => {
  diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);

  const serviceVersion = process.env.INFISICAL_PLATFORM_VERSION || "unknown";

  const resource = defaultResource().merge(
    resourceFromAttributes({
      [ATTR_SERVICE_NAME]: "infisical-core",
      [ATTR_SERVICE_VERSION]: serviceVersion,
      "git.commit.sha": process.env.DD_GIT_COMMIT_SHA || "unknown"
    })
  );

  const metricReaders = [];
  switch (exportType) {
    case "prometheus": {
      const promExporter = new PrometheusExporter();
      metricReaders.push(promExporter);
      break;
    }
    case "otlp": {
      const otlpExporter = new OTLPMetricExporter({
        url: `${otlpURL}/v1/metrics`,
        headers: {
          Authorization: `Basic ${btoa(`${otlpUser}:${otlpPassword}`)}`
        },
        temporalityPreference: AggregationTemporality.DELTA
      });
      metricReaders.push(
        new PeriodicExportingMetricReader({
          exporter: otlpExporter,
          exportIntervalMillis: otlpPushInterval
        })
      );
      break;
    }
    default:
      throw new Error("Invalid OTEL export type");
  }

  const views: ViewOptions[] = [
    {
      meterName: "InfisicalCore",
      aggregationCardinalityLimit: METER_AGGREGATION_CARDINALITY_LIMIT,
      attributesProcessors: [createAllowListAttributesProcessor(INFISICAL_CORE_METER_ATTRIBUTES)]
    },
    {
      meterName: HTTP_INSTRUMENTATION_METER_NAME,
      aggregationCardinalityLimit: METER_AGGREGATION_CARDINALITY_LIMIT,
      attributesProcessors: [createAllowListAttributesProcessor(HTTP_INSTRUMENTATION_METER_ATTRIBUTES)]
    }
  ];

  if (dropHighCardinalityMeters) {
    HIGH_CARDINALITY_METER_NAMES.forEach((meterName) => {
      // The empty allowlist is load-bearing, not redundant with DROP. The SDK applies the attributes
      // processor before hashing the attribute set, but consults the aggregator after: a DROP view still
      // inserts a map entry keyed by the full attribute set, and under cumulative temporality (which the
      // Prometheus exporter mandates) that entry is retained for the lifetime of the process with no
      // cardinality cap. Collapsing every point onto the empty attribute set bounds that to one entry
      // per instrument.
      views.push({
        meterName,
        aggregation: { type: AggregationType.DROP },
        attributesProcessors: [createAllowListAttributesProcessor([])]
      });
    });
  }

  const meterProvider = new MeterProvider({
    resource,
    readers: metricReaders,
    views
  });

  opentelemetry.metrics.setGlobalMeterProvider(meterProvider);

  const requestedSemconvOptIn = process.env.OTEL_SEMCONV_STABILITY_OPT_IN;
  const resolvedSemconvOptIn = resolveHttpSemconvOptIn(requestedSemconvOptIn);
  if (requestedSemconvOptIn !== resolvedSemconvOptIn) {
    // The value an operator set is not the value the instrumentation ends up reading, so say so at boot.
    // The logger is not available here: instrumentation is imported before the app config is loaded.
    // eslint-disable-next-line no-console
    console.log(
      `Telemetry: set OTEL_SEMCONV_STABILITY_OPT_IN to "${resolvedSemconvOptIn}"${
        requestedSemconvOptIn ? ` (from "${requestedSemconvOptIn}")` : ""
      }. Infisical requires the stable HTTP semantic conventions. Set "http/dup" to also emit the old metric names while migrating dashboards.`
    );
  }
  process.env.OTEL_SEMCONV_STABILITY_OPT_IN = resolvedSemconvOptIn;

  registerInstrumentations({
    instrumentations: [new HttpInstrumentation(), new RuntimeNodeInstrumentation()]
  });
};

const setupTelemetry = () => {
  const appCfg = getTelemetryConfig();

  if (appCfg.useOtel) {
    // eslint-disable-next-line no-console
    console.log("Initializing telemetry instrumentation");
    initTelemetryInstrumentation({
      ...appCfg.OTEL,
      dropHighCardinalityMeters: Boolean(appCfg.dropHighCardinalityMeters)
    });
  }

  if (appCfg.useDataDogTracer) {
    // eslint-disable-next-line no-console
    console.log("Initializing Datadog tracer");
    tracer.init({ ...appCfg.TRACER });
  }
};

void setupTelemetry();