Skip to content

Commit a09daa4

Browse files
authored
fix(opentelemetry-node): support HTTP_REQUEST_METHOD attribute (#11929)
Fixes #11926
1 parent 0ee4235 commit a09daa4

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

packages/opentelemetry-node/src/utils/parseOtelSpanDescription.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ export function parseOtelSpanDescription(otelSpan: OtelSpan): SpanDescription {
2424
const { attributes, name } = otelSpan;
2525

2626
// if http.method exists, this is an http request span
27-
const httpMethod = attributes[SemanticAttributes.HTTP_METHOD];
27+
//
28+
// TODO: Referencing `http.request.method` is a temporary workaround until the semantic
29+
// conventions export an attribute key for it.
30+
const httpMethod = attributes['http.request.method'] || attributes[SemanticAttributes.HTTP_METHOD];
2831
if (httpMethod) {
2932
return descriptionForHttpMethod(otelSpan, httpMethod);
3033
}

packages/opentelemetry-node/test/spanprocessor.test.ts

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ describe('SentrySpanProcessor', () => {
513513
});
514514
});
515515

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

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

536-
it('updates based on attributes for HTTP_METHOD for server', async () => {
536+
it('updates based on attributes for HTTP_REQEUST_METHOD for client', async () => {
537+
const tracer = provider.getTracer('default');
538+
539+
tracer.startActiveSpan('GET /users', parentOtelSpan => {
540+
tracer.startActiveSpan('/users/all', { kind: SpanKind.CLIENT }, child => {
541+
const sentrySpan = getSpanForOtelSpan(child);
542+
543+
child.setAttribute('http.request.method', 'GET');
544+
545+
child.end();
546+
547+
// eslint-disable-next-line deprecation/deprecation
548+
expect(sentrySpan?.op).toBe('http.client');
549+
expect(spanToJSON(sentrySpan!).op).toBe('http.client');
550+
551+
parentOtelSpan.end();
552+
});
553+
});
554+
});
555+
556+
it('updates based on attributes for deprecated HTTP_METHOD for server', async () => {
537557
const tracer = provider.getTracer('default');
538558

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

556-
it('updates op/description based on attributes for HTTP_METHOD without HTTP_ROUTE', async () => {
576+
it('updates based on attributes for HTTP_REQUEST_METHOD for server', async () => {
577+
const tracer = provider.getTracer('default');
578+
579+
tracer.startActiveSpan('GET /users', parentOtelSpan => {
580+
tracer.startActiveSpan('/users/all', { kind: SpanKind.SERVER }, child => {
581+
const sentrySpan = getSpanForOtelSpan(child);
582+
583+
child.setAttribute('http.request.method', 'GET');
584+
585+
child.end();
586+
587+
// eslint-disable-next-line deprecation/deprecation
588+
expect(sentrySpan?.op).toBe('http.server');
589+
expect(spanToJSON(sentrySpan!).op).toBe('http.server');
590+
591+
parentOtelSpan.end();
592+
});
593+
});
594+
});
595+
596+
it('updates op/description based on attributes for HTTP_REQUEST_METHOD without HTTP_ROUTE', async () => {
557597
const tracer = provider.getTracer('default');
558598

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

563-
child.setAttribute(SemanticAttributes.HTTP_METHOD, 'GET');
603+
child.setAttribute('http.request.method', 'GET');
564604

565605
child.end();
566606

@@ -571,14 +611,14 @@ describe('SentrySpanProcessor', () => {
571611
});
572612
});
573613

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

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

581-
child.setAttribute(SemanticAttributes.HTTP_METHOD, 'GET');
621+
child.setAttribute('http.request.method', 'GET');
582622
child.setAttribute(SemanticAttributes.HTTP_ROUTE, '/my/route/{id}');
583623
child.setAttribute(SemanticAttributes.HTTP_TARGET, '/my/route/123');
584624
child.setAttribute(SemanticAttributes.HTTP_URL, 'http://example.com/my/route/123');
@@ -604,14 +644,14 @@ describe('SentrySpanProcessor', () => {
604644
});
605645
});
606646

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

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

614-
child.setAttribute(SemanticAttributes.HTTP_METHOD, 'GET');
654+
child.setAttribute('http.request.method', 'GET');
615655
child.setAttribute(SemanticAttributes.HTTP_TARGET, '/my/route/123');
616656
child.setAttribute(SemanticAttributes.HTTP_URL, 'http://example.com/my/route/123');
617657

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

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

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

679-
otelSpan.setAttribute(SemanticAttributes.HTTP_METHOD, 'GET');
719+
otelSpan.setAttribute('http.request.method', 'GET');
680720
otelSpan.setAttribute(SemanticAttributes.HTTP_TARGET, '/my/route/123');
681721

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

695-
otelSpan.setAttribute(SemanticAttributes.HTTP_METHOD, 'GET');
735+
otelSpan.setAttribute('http.request.method', 'GET');
696736
otelSpan.setAttribute(SemanticAttributes.HTTP_TARGET, '/');
697737

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

711-
otelSpan.setAttribute(SemanticAttributes.HTTP_METHOD, 'GET');
751+
otelSpan.setAttribute('http.request.method', 'GET');
712752
otelSpan.setAttribute(SemanticAttributes.HTTP_ROUTE, '/my/route/:id');
713753

714754
otelSpan.end();

0 commit comments

Comments
 (0)