Skip to content

Commit 7d01d20

Browse files
committed
fix(telemetry): never send collector credentials to the anonymous endpoint, match it by host
OTEL_EXPORTER_OTLP_HEADERS belong to the operator's collector, so they are dropped when the endpoint resolves to the anonymous one; that check now compares hosts, so any spelling of the anonymous URL gets traces only. The browser relay falls back to telemetry.config.ts like the server export, and the traces URL is built on the parsed path, keeping query strings and accepting a trailing slash.
1 parent 796d81a commit 7d01d20

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

‎apps/sim/app/api/telemetry/route.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
normalizeOtlpTracesUrl,
1313
resolveOtlpTarget,
1414
} from '@/lib/monitoring/otlp'
15+
import telemetryConfig from '@/telemetry.config'
1516

1617
const logger = createLogger('TelemetryAPI')
1718

@@ -67,7 +68,7 @@ async function forwardToCollector(data: Record<string, unknown>): Promise<boolea
6768
}
6869
if (isTelemetryDisabled()) return false
6970

70-
const target = resolveOtlpTarget()
71+
const target = resolveOtlpTarget(telemetryConfig.endpoint)
7172
const endpoint = normalizeOtlpTracesUrl(target.endpoint)
7273
const timeout = DEFAULT_TIMEOUT
7374

‎apps/sim/lib/monitoring/otlp.ts‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,20 @@ export function isTelemetryDisabled(): boolean {
3838
return process.env.NEXT_TELEMETRY_DISABLED === '1'
3939
}
4040

41+
/** True when `endpoint` names the anonymous collector's host, however the URL is spelled. */
42+
function isAnonymousCollector(endpoint: string): boolean {
43+
try {
44+
return new URL(endpoint).host === new URL(ANONYMOUS_TELEMETRY_ENDPOINT).host
45+
} catch {
46+
return false
47+
}
48+
}
49+
4150
/**
4251
* The collector this process sends telemetry to: `OTEL_EXPORTER_OTLP_ENDPOINT`, then the legacy
4352
* `TELEMETRY_ENDPOINT`, then `fallback` (a fork's `telemetry.config.ts` endpoint, or the anonymous
44-
* collector), with the `OTEL_EXPORTER_OTLP_HEADERS` the configured collector expects.
53+
* collector). `OTEL_EXPORTER_OTLP_HEADERS` belong to the operator's own collector, so they are
54+
* never sent to the anonymous one.
4555
*/
4656
export function resolveOtlpTarget(fallback: string = ANONYMOUS_TELEMETRY_ENDPOINT): {
4757
endpoint: string
@@ -50,24 +60,26 @@ export function resolveOtlpTarget(fallback: string = ANONYMOUS_TELEMETRY_ENDPOIN
5060
} {
5161
const endpoint =
5262
process.env.OTEL_EXPORTER_OTLP_ENDPOINT || process.env.TELEMETRY_ENDPOINT || fallback
63+
const anonymous = isAnonymousCollector(endpoint)
5364
return {
5465
endpoint,
55-
headers: parseOtlpHeaders(process.env.OTEL_EXPORTER_OTLP_HEADERS || ''),
56-
anonymous: endpoint === ANONYMOUS_TELEMETRY_ENDPOINT,
66+
headers: anonymous ? {} : parseOtlpHeaders(process.env.OTEL_EXPORTER_OTLP_HEADERS || ''),
67+
anonymous,
5768
}
5869
}
5970

6071
/**
61-
* Appends `/v1/traces` to an OTLP base URL unless already present. The HTTP exporter doesn't
62-
* auto-suffix the signal path even though the spec says the env var is a base URL.
72+
* Appends `/v1/traces` to an OTLP base URL's path unless it already ends there (with or without a
73+
* trailing slash), keeping any query string. The HTTP exporter doesn't auto-suffix the signal path
74+
* even though the spec says the env var is a base URL.
6375
*/
6476
export function normalizeOtlpTracesUrl(url: string): string {
6577
if (!url) return url
6678
try {
6779
const u = new URL(url)
68-
if (u.pathname.endsWith('/v1/traces')) return url
69-
const base = url.replace(/\/$/, '')
70-
return `${base}/v1/traces`
80+
const path = u.pathname.replace(/\/+$/, '')
81+
u.pathname = path.endsWith('/v1/traces') ? path : `${path}/v1/traces`
82+
return u.toString()
7183
} catch {
7284
return url
7385
}

0 commit comments

Comments
 (0)