From 8167773b0de10395e9b0d4231b7cc7ea82d4e443 Mon Sep 17 00:00:00 2001 From: Max Metral Date: Thu, 9 Nov 2023 14:30:45 -0500 Subject: [PATCH] fix: ensure http url (#10) * fix: Ensure `HTTP_URL` is full URL incl. path --------- Co-authored-by: Francesco Novy --- __tests__/index.spec.ts | 14 ++++++++++++-- src/index.ts | 16 +++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/__tests__/index.spec.ts b/__tests__/index.spec.ts index 320c579..94cd1a9 100644 --- a/__tests__/index.spec.ts +++ b/__tests__/index.spec.ts @@ -5,6 +5,7 @@ import { ReadableSpan, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; import { registerInstrumentations } from '@opentelemetry/instrumentation'; import { SpanStatusCode } from '@opentelemetry/api'; +import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; import { FetchInstrumentation } from '../src'; @@ -47,9 +48,12 @@ test('Basic function', async () => { server.listen(12345, accept); }); - await fetch('http://localhost:12345', { keepalive: false }); + await fetch('http://localhost:12345/my-path', { keepalive: false }); expect(config.onRequest).toHaveBeenCalledTimes(1); - await fetch('http://localhost:12345', { headers: { 'x-error': '1' }, keepalive: false }); + await fetch('http://localhost:12345', { + headers: { 'x-error': '1' }, + keepalive: false, + }); expect(config.onRequest).toHaveBeenCalledTimes(2); await new Promise((accept, reject) => { @@ -71,8 +75,14 @@ test('Basic function', async () => { expect(exportedSpans.length).toBe(3); expect(exportedSpans[0].status.code).toEqual(SpanStatusCode.OK); + expect(exportedSpans[0].attributes[SemanticAttributes.HTTP_URL]).toEqual( + 'http://localhost:12345/my-path', + ); expect(exportedSpans[1].status.code).toEqual(SpanStatusCode.ERROR); expect(exportedSpans[1].status.message).toMatch(/500/); + expect(exportedSpans[1].attributes[SemanticAttributes.HTTP_URL]).toEqual( + 'http://localhost:12345/', + ); expect(exportedSpans[2].status.code).toEqual(SpanStatusCode.ERROR); expect(exportedSpans[2].status.message).toMatch(/ECONNREFUSED/); }); diff --git a/src/index.ts b/src/index.ts index d16dd2e..0c705ff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -165,7 +165,7 @@ export class FetchInstrumentation implements Instrumentation { const span = this.tracer.startSpan(`HTTP ${request.method}`, { kind: SpanKind.CLIENT, attributes: { - [SemanticAttributes.HTTP_URL]: String(request.origin), + [SemanticAttributes.HTTP_URL]: getAbsoluteUrl(request.origin, request.path), [SemanticAttributes.HTTP_METHOD]: request.method, [SemanticAttributes.HTTP_TARGET]: request.path, 'http.client': 'fetch', @@ -228,3 +228,17 @@ export class FetchInstrumentation implements Instrumentation { } } } + +function getAbsoluteUrl(origin: string, path: string = '/'): string { + const url = `${origin}`; + + if (origin.endsWith('/') && path.startsWith('/')) { + return `${url}${path.slice(1)}`; + } + + if (!origin.endsWith('/') && !path.startsWith('/')) { + return `${url}/${path.slice(1)}`; + } + + return `${url}${path}`; +}