Skip to content

Commit e8dae53

Browse files
fix(metrics): parse OTLP metrics URL via URL/pathname, not string suffix
Handles query strings and trailing slashes so the /v1/traces->/v1/metrics swap can't produce a malformed endpoint, matching normalizeOtlpTracesUrl.
1 parent e56f0f7 commit e8dae53

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

apps/sim/instrumentation-node.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,20 @@ function normalizeOtlpTracesUrl(url: string): string {
6363
}
6464
}
6565

66-
// Metrics counterpart to `normalizeOtlpTracesUrl`. The endpoint may already
67-
// carry a signal-specific suffix (traces); swap it for `/v1/metrics`.
66+
// Metrics counterpart to `normalizeOtlpTracesUrl`. Operates on the parsed
67+
// pathname (not a raw string suffix) so query strings and trailing slashes
68+
// don't corrupt the result: swap a `/v1/traces` suffix for `/v1/metrics`,
69+
// otherwise append `/v1/metrics`.
6870
function normalizeOtlpMetricsUrl(url: string): string {
6971
if (!url) return url
7072
try {
71-
if (url.endsWith('/v1/metrics')) return url
72-
const base = url.replace(/\/v1\/traces$/, '').replace(/\/$/, '')
73-
return `${base}/v1/metrics`
73+
const u = new URL(url)
74+
const path = u.pathname.replace(/\/$/, '')
75+
if (path.endsWith('/v1/metrics')) return url
76+
u.pathname = path.endsWith('/v1/traces')
77+
? path.replace(/\/v1\/traces$/, '/v1/metrics')
78+
: `${path}/v1/metrics`
79+
return u.toString()
7480
} catch {
7581
return url
7682
}

0 commit comments

Comments
 (0)