: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(logs): swallow otel recording errors so telemetry can't mask ingestion failures (#70153)
Daniel Visca980364e91c58
diff --git a/nodejs/src/logs/ingestion-otel-metrics.test.ts b/nodejs/src/logs/ingestion-otel-metrics.test.tsindex b58fe0056160..3e809fa5c66d 100644--- a/nodejs/src/logs/ingestion-otel-metrics.test.ts+++ b/nodejs/src/logs/ingestion-otel-metrics.test.ts@@ -123,6 +123,43 @@ describe('ingestion-otel-metrics', () => {expect(names.map((name) => dataPointsFor(name).length)).toEqual([0, 0, 0, 0, 0])})+ it.each([+ ['recordLogsReceived', () => recordLogsReceived(1, 1)],+ ['recordLogsAllowed', () => recordLogsAllowed(1, 1)],+ ['recordLogsDropped', () => recordLogsDropped(42, 1, 1)],+ ['recordLogMessageDropped', () => recordLogMessageDropped('rate_limited', '42')],+ ['recordLogMessageDlq', () => recordLogMessageDlq('KafkaError', '42')],+ [+ 'recordLogProcessingDuration',+ () =>+ recordLogProcessingDuration(0.02, {+ json_parse_enabled: 'true',+ pii_scrub_enabled: 'false',+ compression_codec: 'snappy',+ }),+ ],+ ] as const)(+ '%s swallows a throwing OTel SDK so callers in finally blocks keep the original error',+ (_name, record) => {+ // These run in finally blocks and error handlers of the ingestion hot path;+ // a throw here would mask the real processing error and DLQ with the wrong reason.+ const throwing = () => {+ throw new Error('otel exploded')+ }+ metricsApi.disable() // the API ignores a second setGlobalMeterProvider without this+ metricsApi.setGlobalMeterProvider({+ getMeter: () =>+ ({+ createCounter: () => ({ add: throwing }),+ createHistogram: () => ({ record: throwing }),+ }) as any,+ } as any)+ resetLogsIngestionInstrumentsForTests()++ expect(record).not.toThrow()+ }+ )+it('records processing duration with the prom bucket ladder and pipeline attributes', async () => {const attributes = { json_parse_enabled: 'true', pii_scrub_enabled: 'false', compression_codec: 'snappy' }recordLogProcessingDuration(0.02, attributes)diff --git a/nodejs/src/logs/ingestion-otel-metrics.ts b/nodejs/src/logs/ingestion-otel-metrics.tsindex fd161d25a21c..2858bd88ba0f 100644--- a/nodejs/src/logs/ingestion-otel-metrics.ts+++ b/nodejs/src/logs/ingestion-otel-metrics.ts@@ -86,39 +86,56 @@ function addPositive(counter: Counter, value: number, attributes?: Attributes):}}-export function recordLogsReceived(bytes: number, records: number): void {+/**+ * These run in finally blocks and error handlers of the ingestion hot path — a throw+ * here would mask the real processing error and DLQ the message with the wrong reason,+ * so recording failures are swallowed.+ */+function swallowing<Args extends unknown[]>(record: (...args: Args) => void): (...args: Args) => void {+ return (...args: Args): void => {+ try {+ record(...args)+ } catch {+ // never let telemetry break ingestion+ }+ }+}++export const recordLogsReceived = swallowing((bytes: number, records: number): void => {const { bytesReceived, recordsReceived } = getInstruments()addPositive(bytesReceived, bytes)addPositive(recordsReceived, records)-}+})-export function recordLogsAllowed(bytes: number, records: number): void {+export const recordLogsAllowed = swallowing((bytes: number, records: number): void => {const { bytesAllowed, recordsAllowed } = getInstruments()addPositive(bytesAllowed, bytes)addPositive(recordsAllowed, records)-}+})-export function recordLogsDropped(teamId: number, bytes: number, records: number): void {+export const recordLogsDropped = swallowing((teamId: number, bytes: number, records: number): void => {const { bytesDropped, recordsDropped } = getInstruments()const attributes = { team_id: teamId.toString() }addPositive(bytesDropped, bytes, attributes)addPositive(recordsDropped, records, attributes)-}+})-export function recordLogMessageDropped(reason: string, teamId: string, count: number = 1): void {+export const recordLogMessageDropped = swallowing((reason: string, teamId: string, count: number = 1): void => {addPositive(getInstruments().messagesDropped, count, { reason, team_id: teamId })-}+})-export function recordLogMessageDlq(reason: string, teamId: string): void {+export const recordLogMessageDlq = swallowing((reason: string, teamId: string): void => {getInstruments().messagesDlq.add(1, { reason, team_id: teamId })-}+})-export function recordLogProcessingDuration(- seconds: number,- attributes: { json_parse_enabled: string; pii_scrub_enabled: string; compression_codec: string }-): void {- getInstruments().processingDuration.record(seconds, attributes)-}+export const recordLogProcessingDuration = swallowing(+ (+ seconds: number,+ attributes: { json_parse_enabled: string; pii_scrub_enabled: string; compression_codec: string }+ ): void => {+ getInstruments().processingDuration.record(seconds, attributes)+ }+)/** Test seam: forget cached instruments so a test-installed provider is picked up. */export function resetLogsIngestionInstrumentsForTests(): void {