Skip to content

Commit 88f6989

Browse files
authored
fix(otel): sanitize OTLP endpoint URL resolution (#13791)
* fix(otel): sanitize OTLP endpoint signal URL resolution * fix(otel): preserve signal URLs with query params * fix(otel): accept case-insensitive signal paths
1 parent a7c0aa9 commit 88f6989

2 files changed

Lines changed: 105 additions & 2 deletions

File tree

‎extensions/diagnostics-otel/src/service.test.ts‎

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const sdkStart = vi.hoisted(() => vi.fn().mockResolvedValue(undefined));
3030
const sdkShutdown = vi.hoisted(() => vi.fn().mockResolvedValue(undefined));
3131
const logEmit = vi.hoisted(() => vi.fn());
3232
const logShutdown = vi.hoisted(() => vi.fn().mockResolvedValue(undefined));
33+
const traceExporterCtor = vi.hoisted(() => vi.fn());
3334

3435
vi.mock("@opentelemetry/api", () => ({
3536
metrics: {
@@ -55,7 +56,11 @@ vi.mock("@opentelemetry/exporter-metrics-otlp-http", () => ({
5556
}));
5657

5758
vi.mock("@opentelemetry/exporter-trace-otlp-http", () => ({
58-
OTLPTraceExporter: class {},
59+
OTLPTraceExporter: class {
60+
constructor(options?: unknown) {
61+
traceExporterCtor(options);
62+
}
63+
},
5964
}));
6065

6166
vi.mock("@opentelemetry/exporter-logs-otlp-http", () => ({
@@ -119,6 +124,7 @@ describe("diagnostics-otel service", () => {
119124
sdkShutdown.mockClear();
120125
logEmit.mockClear();
121126
logShutdown.mockClear();
127+
traceExporterCtor.mockClear();
122128
registerLogTransportMock.mockReset();
123129
});
124130

@@ -227,4 +233,100 @@ describe("diagnostics-otel service", () => {
227233

228234
await service.stop?.(ctx);
229235
});
236+
237+
test("appends signal path when endpoint contains non-signal /v1 segment", async () => {
238+
const service = createDiagnosticsOtelService();
239+
await service.start({
240+
config: {
241+
diagnostics: {
242+
enabled: true,
243+
otel: {
244+
enabled: true,
245+
endpoint: "https://www.comet.com/opik/api/v1/private/otel",
246+
protocol: "http/protobuf",
247+
traces: true,
248+
metrics: false,
249+
logs: false,
250+
},
251+
},
252+
},
253+
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
254+
});
255+
256+
const options = traceExporterCtor.mock.calls[0]?.[0] as { url?: string } | undefined;
257+
expect(options?.url).toBe("https://www.comet.com/opik/api/v1/private/otel/v1/traces");
258+
await service.stop?.();
259+
});
260+
261+
test("keeps already signal-qualified endpoint unchanged", async () => {
262+
const service = createDiagnosticsOtelService();
263+
await service.start({
264+
config: {
265+
diagnostics: {
266+
enabled: true,
267+
otel: {
268+
enabled: true,
269+
endpoint: "https://collector.example.com/v1/traces",
270+
protocol: "http/protobuf",
271+
traces: true,
272+
metrics: false,
273+
logs: false,
274+
},
275+
},
276+
},
277+
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
278+
});
279+
280+
const options = traceExporterCtor.mock.calls[0]?.[0] as { url?: string } | undefined;
281+
expect(options?.url).toBe("https://collector.example.com/v1/traces");
282+
await service.stop?.();
283+
});
284+
285+
test("keeps signal-qualified endpoint unchanged when it has query params", async () => {
286+
const service = createDiagnosticsOtelService();
287+
await service.start({
288+
config: {
289+
diagnostics: {
290+
enabled: true,
291+
otel: {
292+
enabled: true,
293+
endpoint: "https://collector.example.com/v1/traces?timeout=30s",
294+
protocol: "http/protobuf",
295+
traces: true,
296+
metrics: false,
297+
logs: false,
298+
},
299+
},
300+
},
301+
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
302+
});
303+
304+
const options = traceExporterCtor.mock.calls[0]?.[0] as { url?: string } | undefined;
305+
expect(options?.url).toBe("https://collector.example.com/v1/traces?timeout=30s");
306+
await service.stop?.();
307+
});
308+
309+
test("keeps signal-qualified endpoint unchanged when signal path casing differs", async () => {
310+
const service = createDiagnosticsOtelService();
311+
await service.start({
312+
config: {
313+
diagnostics: {
314+
enabled: true,
315+
otel: {
316+
enabled: true,
317+
endpoint: "https://collector.example.com/v1/Traces",
318+
protocol: "http/protobuf",
319+
traces: true,
320+
metrics: false,
321+
logs: false,
322+
},
323+
},
324+
},
325+
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
326+
});
327+
328+
const options = traceExporterCtor.mock.calls[0]?.[0] as { url?: string } | undefined;
329+
expect(options?.url).toBe("https://collector.example.com/v1/Traces");
330+
await service.stop?.();
331+
});
230332
});

‎extensions/diagnostics-otel/src/service.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ function resolveOtelUrl(endpoint: string | undefined, path: string): string | un
2323
if (!endpoint) {
2424
return undefined;
2525
}
26-
if (endpoint.includes("/v1/")) {
26+
const endpointWithoutQueryOrFragment = endpoint.split(/[?#]/, 1)[0] ?? endpoint;
27+
if (/\/v1\/(?:traces|metrics|logs)$/i.test(endpointWithoutQueryOrFragment)) {
2728
return endpoint;
2829
}
2930
return `${endpoint}/${path}`;

0 commit comments

Comments
 (0)