Skip to content

Commit 4de955d

Browse files
fix(otel): address staging pr comments for trigger otel (#4586)
1 parent ed39edb commit 4de955d

3 files changed

Lines changed: 30 additions & 42 deletions

File tree

apps/sim/instrumentation-node.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
import { createLogger } from '@sim/logger'
1515
import { TraceAttr } from '@/lib/copilot/generated/trace-attributes-v1'
1616
import { env } from './lib/core/config/env'
17+
import { parseOtlpHeaders } from './lib/monitoring/otlp'
1718

1819
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.ERROR)
1920

@@ -47,29 +48,6 @@ function isBusinessSpan(spanName: string): boolean {
4748
return ALLOWED_SPAN_PREFIXES.some((prefix) => spanName.startsWith(prefix))
4849
}
4950

50-
// Parse `OTEL_EXPORTER_OTLP_HEADERS`: `key1=value1,key2=value2`
51-
// (URL-encoded values, whitespace tolerated).
52-
function parseOtlpHeadersEnv(raw: string): Record<string, string> {
53-
const out: Record<string, string> = {}
54-
if (!raw) return out
55-
for (const part of raw.split(',')) {
56-
const trimmed = part.trim()
57-
if (!trimmed) continue
58-
const eq = trimmed.indexOf('=')
59-
if (eq <= 0) continue
60-
const key = trimmed.slice(0, eq).trim()
61-
const rawVal = trimmed.slice(eq + 1).trim()
62-
let val = rawVal
63-
try {
64-
val = decodeURIComponent(rawVal)
65-
} catch {
66-
// value wasn't URL-encoded; keep as-is.
67-
}
68-
if (key) out[key] = val
69-
}
70-
return out
71-
}
72-
7351
// Append `/v1/traces` to the OTLP base URL unless already present.
7452
// The HTTP exporter doesn't auto-suffix the signal path even though
7553
// the spec says the env var is a base URL.
@@ -214,7 +192,7 @@ async function initializeOpenTelemetry() {
214192
},
215193
})
216194

217-
const otlpHeaders = parseOtlpHeadersEnv(process.env.OTEL_EXPORTER_OTLP_HEADERS || '')
195+
const otlpHeaders = parseOtlpHeaders(process.env.OTEL_EXPORTER_OTLP_HEADERS || '')
218196
const exporterUrl = normalizeOtlpTracesUrl(telemetryConfig.endpoint)
219197

220198
const exporter = new OTLPTraceExporter({

apps/sim/lib/monitoring/otlp.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Parse OTLP headers per the OTEL spec format `key1=value1,key2=value2`.
3+
* Values are URL-decoded; the raw value is preserved if decoding fails.
4+
* Keys and values are trimmed; empty or malformed entries are skipped.
5+
* @see https://opentelemetry.io/docs/specs/otel/protocol/exporter/
6+
*/
7+
export function parseOtlpHeaders(raw: string): Record<string, string> {
8+
const out: Record<string, string> = {}
9+
if (!raw) return out
10+
for (const part of raw.split(',')) {
11+
const trimmed = part.trim()
12+
if (!trimmed) continue
13+
const eq = trimmed.indexOf('=')
14+
if (eq <= 0) continue
15+
const key = trimmed.slice(0, eq).trim()
16+
if (!key) continue
17+
const rawVal = trimmed.slice(eq + 1).trim()
18+
let val = rawVal
19+
try {
20+
val = decodeURIComponent(rawVal)
21+
} catch {
22+
// value wasn't URL-encoded; keep as-is.
23+
}
24+
out[key] = val
25+
}
26+
return out
27+
}

apps/sim/trigger.config.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { resourceFromAttributes } from '@opentelemetry/resources'
55
import { additionalFiles, additionalPackages } from '@trigger.dev/build/extensions/core'
66
import { defineConfig } from '@trigger.dev/sdk'
77
import { env } from './lib/core/config/env'
8+
import { parseOtlpHeaders } from './lib/monitoring/otlp'
89

910
const grafanaEndpoint = env.GRAFANA_OTLP_ENDPOINT
1011
const grafanaHeaders = env.GRAFANA_OTLP_HEADERS
@@ -20,24 +21,6 @@ if (grafanaConfigured && !grafanaFullyConfigured) {
2021
)
2122
}
2223

23-
/**
24-
* Parse OTLP headers per the OTEL spec format `key1=value1,key2=value2`.
25-
* Values are URL-decoded; keys/values are trimmed; empty entries are skipped.
26-
* @see https://opentelemetry.io/docs/specs/otel/protocol/exporter/
27-
*/
28-
function parseOtlpHeaders(raw: string): Record<string, string> {
29-
const out: Record<string, string> = {}
30-
for (const pair of raw.split(',')) {
31-
const eq = pair.indexOf('=')
32-
if (eq === -1) continue
33-
const key = pair.slice(0, eq).trim()
34-
const value = pair.slice(eq + 1).trim()
35-
if (!key) continue
36-
out[key] = decodeURIComponent(value)
37-
}
38-
return out
39-
}
40-
4124
const grafanaTelemetry = grafanaFullyConfigured
4225
? (() => {
4326
const baseUrl = grafanaEndpoint!.replace(/\/+$/, '')

0 commit comments

Comments
 (0)