Skip to content

Commit

Permalink
fix(opentelemetry-node): support HTTP_REQUEST_METHOD attribute (#11929
Browse files Browse the repository at this point in the history
)

Fixes #11926
  • Loading branch information
jeengbe authored May 7, 2024
1 parent 0ee4235 commit a09daa4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export function parseOtelSpanDescription(otelSpan: OtelSpan): SpanDescription {
const { attributes, name } = otelSpan;

// if http.method exists, this is an http request span
const httpMethod = attributes[SemanticAttributes.HTTP_METHOD];
//
// TODO: Referencing `http.request.method` is a temporary workaround until the semantic
// conventions export an attribute key for it.
const httpMethod = attributes['http.request.method'] || attributes[SemanticAttributes.HTTP_METHOD];
if (httpMethod) {
return descriptionForHttpMethod(otelSpan, httpMethod);
}
Expand Down
64 changes: 52 additions & 12 deletions packages/opentelemetry-node/test/spanprocessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ describe('SentrySpanProcessor', () => {
});
});

it('updates based on attributes for HTTP_METHOD for client', async () => {
it('updates based on attributes for deprecated HTTP_METHOD for client', async () => {
const tracer = provider.getTracer('default');

tracer.startActiveSpan('GET /users', parentOtelSpan => {
Expand All @@ -533,7 +533,27 @@ describe('SentrySpanProcessor', () => {
});
});

it('updates based on attributes for HTTP_METHOD for server', async () => {
it('updates based on attributes for HTTP_REQEUST_METHOD for client', async () => {
const tracer = provider.getTracer('default');

tracer.startActiveSpan('GET /users', parentOtelSpan => {
tracer.startActiveSpan('/users/all', { kind: SpanKind.CLIENT }, child => {
const sentrySpan = getSpanForOtelSpan(child);

child.setAttribute('http.request.method', 'GET');

child.end();

// eslint-disable-next-line deprecation/deprecation
expect(sentrySpan?.op).toBe('http.client');
expect(spanToJSON(sentrySpan!).op).toBe('http.client');

parentOtelSpan.end();
});
});
});

it('updates based on attributes for deprecated HTTP_METHOD for server', async () => {
const tracer = provider.getTracer('default');

tracer.startActiveSpan('GET /users', parentOtelSpan => {
Expand All @@ -553,14 +573,34 @@ describe('SentrySpanProcessor', () => {
});
});

it('updates op/description based on attributes for HTTP_METHOD without HTTP_ROUTE', async () => {
it('updates based on attributes for HTTP_REQUEST_METHOD for server', async () => {
const tracer = provider.getTracer('default');

tracer.startActiveSpan('GET /users', parentOtelSpan => {
tracer.startActiveSpan('/users/all', { kind: SpanKind.SERVER }, child => {
const sentrySpan = getSpanForOtelSpan(child);

child.setAttribute('http.request.method', 'GET');

child.end();

// eslint-disable-next-line deprecation/deprecation
expect(sentrySpan?.op).toBe('http.server');
expect(spanToJSON(sentrySpan!).op).toBe('http.server');

parentOtelSpan.end();
});
});
});

it('updates op/description based on attributes for HTTP_REQUEST_METHOD without HTTP_ROUTE', async () => {
const tracer = provider.getTracer('default');

tracer.startActiveSpan('GET /users', parentOtelSpan => {
tracer.startActiveSpan('HTTP GET', child => {
const sentrySpan = getSpanForOtelSpan(child);

child.setAttribute(SemanticAttributes.HTTP_METHOD, 'GET');
child.setAttribute('http.request.method', 'GET');

child.end();

Expand All @@ -571,14 +611,14 @@ describe('SentrySpanProcessor', () => {
});
});

it('updates based on attributes for HTTP_METHOD with HTTP_ROUTE', async () => {
it('updates based on attributes for HTTP_REQUEST_METHOD with HTTP_ROUTE', async () => {
const tracer = provider.getTracer('default');

tracer.startActiveSpan('GET /users', parentOtelSpan => {
tracer.startActiveSpan('HTTP GET', child => {
const sentrySpan = getSpanForOtelSpan(child);

child.setAttribute(SemanticAttributes.HTTP_METHOD, 'GET');
child.setAttribute('http.request.method', 'GET');
child.setAttribute(SemanticAttributes.HTTP_ROUTE, '/my/route/{id}');
child.setAttribute(SemanticAttributes.HTTP_TARGET, '/my/route/123');
child.setAttribute(SemanticAttributes.HTTP_URL, 'http://example.com/my/route/123');
Expand All @@ -604,14 +644,14 @@ describe('SentrySpanProcessor', () => {
});
});

it('updates based on attributes for HTTP_METHOD with HTTP_TARGET', async () => {
it('updates based on attributes for HTTP_REQUEST_METHOD with HTTP_TARGET', async () => {
const tracer = provider.getTracer('default');

tracer.startActiveSpan('GET /users', parentOtelSpan => {
tracer.startActiveSpan('HTTP GET', child => {
const sentrySpan = getSpanForOtelSpan(child);

child.setAttribute(SemanticAttributes.HTTP_METHOD, 'GET');
child.setAttribute('http.request.method', 'GET');
child.setAttribute(SemanticAttributes.HTTP_TARGET, '/my/route/123');
child.setAttribute(SemanticAttributes.HTTP_URL, 'http://example.com/my/route/123');

Expand Down Expand Up @@ -643,7 +683,7 @@ describe('SentrySpanProcessor', () => {
tracer.startActiveSpan('HTTP GET', child => {
const sentrySpan = getSpanForOtelSpan(child);

child.setAttribute(SemanticAttributes.HTTP_METHOD, 'GET');
child.setAttribute('http.request.method', 'GET');
child.setAttribute(SemanticAttributes.HTTP_TARGET, '/my/route/123');
child.setAttribute(SemanticAttributes.HTTP_URL, 'http://example.com/my/route/123?what=123#myHash');

Expand Down Expand Up @@ -676,7 +716,7 @@ describe('SentrySpanProcessor', () => {
tracer.startActiveSpan('GET /users', otelSpan => {
const sentrySpan = getSpanForOtelSpan(otelSpan);

otelSpan.setAttribute(SemanticAttributes.HTTP_METHOD, 'GET');
otelSpan.setAttribute('http.request.method', 'GET');
otelSpan.setAttribute(SemanticAttributes.HTTP_TARGET, '/my/route/123');

otelSpan.end();
Expand All @@ -692,7 +732,7 @@ describe('SentrySpanProcessor', () => {
tracer.startActiveSpan('GET /', otelSpan => {
const sentrySpan = getSpanForOtelSpan(otelSpan);

otelSpan.setAttribute(SemanticAttributes.HTTP_METHOD, 'GET');
otelSpan.setAttribute('http.request.method', 'GET');
otelSpan.setAttribute(SemanticAttributes.HTTP_TARGET, '/');

otelSpan.end();
Expand All @@ -708,7 +748,7 @@ describe('SentrySpanProcessor', () => {
tracer.startActiveSpan('GET /users', otelSpan => {
const sentrySpan = getSpanForOtelSpan(otelSpan);

otelSpan.setAttribute(SemanticAttributes.HTTP_METHOD, 'GET');
otelSpan.setAttribute('http.request.method', 'GET');
otelSpan.setAttribute(SemanticAttributes.HTTP_ROUTE, '/my/route/:id');

otelSpan.end();
Expand Down

0 comments on commit a09daa4

Please sign in to comment.