Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.8 KB
Raw Blame
import { Counter, metrics as metricsApi } from '@opentelemetry/api'
import { MeterProvider } from '@opentelemetry/sdk-metrics'

import { defaultConfig } from '~/common/config/config'
import { logger } from '~/common/utils/logger'
import { registerShutdownHandler } from '~/lifecycle'

import { counterWithExemplars } from './exemplars'
import { createOtlpMeterProvider } from './otlp-provider'

/**
 * OTLP metrics push — the same OTel-SDK path customers use, pointed at our own
 * ingest (capture-logs /v1/metrics). Complements tracing/otel.ts (spans) and the
 * collector-shipped container logs: with this, a service pushes all three
 * signals instead of having its metrics scraped.
 *
 * Off unless both OTEL_METRICS_EXPORT_URL and OTEL_METRICS_EXPORT_TOKEN are set,
 * so nothing changes for deployments that don't opt in.
 */

let provider: MeterProvider | null = null

export const initMetrics = (): void => {
    if (provider) {
        return
    }
    if (!defaultConfig.OTEL_METRICS_EXPORT_URL || !defaultConfig.OTEL_METRICS_EXPORT_TOKEN) {
        return
    }

    logger.info('Starting OTLP metrics push', { endpoint: defaultConfig.OTEL_METRICS_EXPORT_URL })

    provider = createOtlpMeterProvider({
        url: defaultConfig.OTEL_METRICS_EXPORT_URL,
        token: defaultConfig.OTEL_METRICS_EXPORT_TOKEN,
        serviceName: defaultConfig.OTEL_SERVICE_NAME ?? `node-${defaultConfig.PLUGIN_SERVER_MODE ?? 'nodejs'}`,
        exportIntervalMillis: defaultConfig.OTEL_METRICS_EXPORT_INTERVAL_MS,
    })
    metricsApi.setGlobalMeterProvider(provider)

    registerShutdownHandler(async () => {
        await provider?.shutdown()
    })
}

let piiReplacementsCounter: Counter | null = null

/**
 * PII replacements performed by the ingest scrubber. Previously only visible in
 * per-team billing usage rows — no operational metric existed for it at all.
 *
 * The counter is acquired lazily on first record: the OTel metrics API has no
 * proxy provider, so a counter created at module load (before initMetrics runs)
 * would be bound to the noop meter forever.
 */
export function recordPiiReplacements(source: string, count: number): void {
    if (count <= 0) {
        return
    }
    if (piiReplacementsCounter === null) {
        piiReplacementsCounter = counterWithExemplars(
            'logs_pii_replacements_total',
            metricsApi.getMeter('logs-ingestion').createCounter('logs_pii_replacements_total', {
                description: 'PII values replaced by the ingest scrubber, by pipeline source (logs | traces).',
            })
        )
    }
    piiReplacementsCounter.add(count, { source })
}

/** Test seam: forget the cached counter so a test-installed provider is picked up. */
export function resetPiiReplacementsCounterForTests(): void {
    piiReplacementsCounter = null
}