@@ -3,12 +3,13 @@ import { SpanKind } from '@opentelemetry/api';
3
3
import { registerInstrumentations } from '@opentelemetry/instrumentation' ;
4
4
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http' ;
5
5
import { 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' ;
9
7
import type { EventProcessor , Hub , Integration } from '@sentry/types' ;
10
8
import type { ClientRequest , IncomingMessage , ServerResponse } from 'http' ;
11
9
10
+ import { OTEL_ATTR_DROP_SPAN , OTEL_ATTR_ORIGIN } from '../constants' ;
11
+ import { setOtelSpanMetadata } from '../opentelemetry/spanData' ;
12
+ import { getCurrentHub } from '../sdk/hub' ;
12
13
import type { NodeExperimentalClient , OtelSpan } from '../types' ;
13
14
import { getRequestSpanData } from '../utils/getRequestSpanData' ;
14
15
@@ -110,7 +111,7 @@ export class Http implements Integration {
110
111
requireParentforOutgoingSpans : true ,
111
112
requireParentforIncomingSpans : false ,
112
113
requestHook : ( span , req ) => {
113
- this . _updateSentrySpan ( span as unknown as OtelSpan , req ) ;
114
+ this . _updateSpan ( span as unknown as OtelSpan , req ) ;
114
115
} ,
115
116
responseHook : ( span , res ) => {
116
117
this . _addRequestBreadcrumb ( span as unknown as OtelSpan , res ) ;
@@ -122,8 +123,6 @@ export class Http implements Integration {
122
123
this . _shouldCreateSpanForRequest =
123
124
// eslint-disable-next-line deprecation/deprecation
124
125
this . _shouldCreateSpanForRequest || clientOptions ?. shouldCreateSpanForRequest ;
125
-
126
- client ?. on ?.( 'otelSpanEnd' , this . _onSpanEnd ) ;
127
126
}
128
127
129
128
/**
@@ -133,64 +132,33 @@ export class Http implements Integration {
133
132
this . _unload ?.( ) ;
134
133
}
135
134
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 {
140
137
if ( ! this . _shouldCreateSpans ) {
141
- mutableOptions . drop = true ;
142
- return ;
138
+ return true ;
143
139
}
144
140
145
141
if ( this . _shouldCreateSpanForRequest ) {
146
- const url = getHttpUrl ( ( otelSpan as OtelSpan ) . attributes ) ;
142
+ const url = getHttpUrl ( otelSpan . attributes ) ;
147
143
if ( url && ! this . _shouldCreateSpanForRequest ( url ) ) {
148
- mutableOptions . drop = true ;
149
- return ;
144
+ return true ;
150
145
}
151
146
}
152
147
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
+ }
175
150
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' ) ;
179
154
180
- sentrySpan . setTag ( 'http.status_code' , statusCode ) ;
155
+ if ( span . kind === SpanKind . SERVER ) {
156
+ setOtelSpanMetadata ( span , { request } ) ;
181
157
}
182
158
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 ) ;
185
161
}
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
- } ) ;
194
162
}
195
163
196
164
/** Add a breadcrumb for outgoing requests. */
0 commit comments