Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(opentelemetry-node): support HTTP_REQUEST_METHOD attribute #11929

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading