Skip to content

Commit

Permalink
fix: ensure http url (#10)
Browse files Browse the repository at this point in the history
* fix: Ensure `HTTP_URL` is full URL incl. path

---------

Co-authored-by: Francesco Novy <francesco.novy@sentry.io>
  • Loading branch information
djMax and mydea authored Nov 9, 2023
1 parent 7a4a5f5 commit 8167773
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
14 changes: 12 additions & 2 deletions __tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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<void>((accept, reject) => {
Expand All @@ -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/);
});
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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}`;
}

0 comments on commit 8167773

Please sign in to comment.