@@ -3,12 +3,13 @@ import { SpanKind } from '@opentelemetry/api';
33import { registerInstrumentations } from '@opentelemetry/instrumentation' ;
44import { HttpInstrumentation } from '@opentelemetry/instrumentation-http' ;
55import { SemanticAttributes } from '@opentelemetry/semantic-conventions' ;
6- import { hasTracingEnabled , Transaction } from '@sentry/core' ;
7- import { getCurrentHub } from '@sentry/node' ;
8- import { _INTERNAL_getSentrySpan } from '@sentry/opentelemetry-node' ;
6+ import { hasTracingEnabled } from '@sentry/core' ;
97import type { EventProcessor , Hub , Integration } from '@sentry/types' ;
108import type { ClientRequest , IncomingMessage , ServerResponse } from 'http' ;
119
10+ import { OTEL_ATTR_DROP_SPAN , OTEL_ATTR_ORIGIN } from '../constants' ;
11+ import { setOtelSpanMetadata } from '../opentelemetry/spanData' ;
12+ import { getCurrentHub } from '../sdk/hub' ;
1213import type { NodeExperimentalClient , OtelSpan } from '../types' ;
1314import { getRequestSpanData } from '../utils/getRequestSpanData' ;
1415
@@ -110,7 +111,7 @@ export class Http implements Integration {
110111 requireParentforOutgoingSpans : true ,
111112 requireParentforIncomingSpans : false ,
112113 requestHook : ( span , req ) => {
113- this . _updateSentrySpan ( span as unknown as OtelSpan , req ) ;
114+ this . _updateSpan ( span as unknown as OtelSpan , req ) ;
114115 } ,
115116 responseHook : ( span , res ) => {
116117 this . _addRequestBreadcrumb ( span as unknown as OtelSpan , res ) ;
@@ -122,8 +123,6 @@ export class Http implements Integration {
122123 this . _shouldCreateSpanForRequest =
123124 // eslint-disable-next-line deprecation/deprecation
124125 this . _shouldCreateSpanForRequest || clientOptions ?. shouldCreateSpanForRequest ;
125-
126- client ?. on ?.( 'otelSpanEnd' , this . _onSpanEnd ) ;
127126 }
128127
129128 /**
@@ -133,64 +132,33 @@ export class Http implements Integration {
133132 this . _unload ?.( ) ;
134133 }
135134
136- private _onSpanEnd : ( otelSpan : unknown , mutableOptions : { drop : boolean } ) => void = (
137- otelSpan : unknown ,
138- mutableOptions : { drop : boolean } ,
139- ) => {
135+ /** If the span for this request should be dropped (=not sent to Sentry). */
136+ private _shouldDropSpan ( otelSpan : OtelSpan ) : boolean {
140137 if ( ! this . _shouldCreateSpans ) {
141- mutableOptions . drop = true ;
142- return ;
138+ return true ;
143139 }
144140
145141 if ( this . _shouldCreateSpanForRequest ) {
146- const url = getHttpUrl ( ( otelSpan as OtelSpan ) . attributes ) ;
142+ const url = getHttpUrl ( otelSpan . attributes ) ;
147143 if ( url && ! this . _shouldCreateSpanForRequest ( url ) ) {
148- mutableOptions . drop = true ;
149- return ;
144+ return true ;
150145 }
151146 }
152147
153- return ;
154- } ;
155-
156- /** Update the Sentry span data based on the OTEL span. */
157- private _updateSentrySpan ( span : OtelSpan , request : ClientRequest | IncomingMessage ) : void {
158- const data = getRequestSpanData ( span ) ;
159- const { attributes } = span ;
160-
161- const sentrySpan = _INTERNAL_getSentrySpan ( span . spanContext ( ) . spanId ) ;
162- if ( ! sentrySpan ) {
163- return ;
164- }
165-
166- sentrySpan . origin = 'auto.http.otel.http' ;
167-
168- const additionalData : Record < string , unknown > = {
169- url : data . url ,
170- } ;
171-
172- if ( sentrySpan instanceof Transaction && span . kind === SpanKind . SERVER ) {
173- sentrySpan . setMetadata ( { request } ) ;
174- }
148+ return false ;
149+ }
175150
176- if ( attributes [ SemanticAttributes . HTTP_STATUS_CODE ] ) {
177- const statusCode = attributes [ SemanticAttributes . HTTP_STATUS_CODE ] as string ;
178- additionalData [ ' http.response.status_code' ] = statusCode ;
151+ /** Update the span with data we need. */
152+ private _updateSpan ( span : OtelSpan , request : ClientRequest | IncomingMessage ) : void {
153+ span . setAttribute ( OTEL_ATTR_ORIGIN , 'auto. http.otel.http' ) ;
179154
180- sentrySpan . setTag ( 'http.status_code' , statusCode ) ;
155+ if ( span . kind === SpanKind . SERVER ) {
156+ setOtelSpanMetadata ( span , { request } ) ;
181157 }
182158
183- if ( data [ 'http.query' ] ) {
184- additionalData [ 'http.query' ] = data [ 'http.query' ] . slice ( 1 ) ;
159+ if ( this . _shouldDropSpan ( span as unknown as OtelSpan ) ) {
160+ span . setAttribute ( OTEL_ATTR_DROP_SPAN , true ) ;
185161 }
186- if ( data [ 'http.fragment' ] ) {
187- additionalData [ 'http.fragment' ] = data [ 'http.fragment' ] . slice ( 1 ) ;
188- }
189-
190- Object . keys ( additionalData ) . forEach ( prop => {
191- const value = additionalData [ prop ] ;
192- sentrySpan . setData ( prop , value ) ;
193- } ) ;
194162 }
195163
196164 /** Add a breadcrumb for outgoing requests. */
0 commit comments