2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
4
import { NextApiHandler } from 'next' ;
5
- import { context , Exception , propagation , Span , SpanKind , SpanStatusCode , trace } from '@opentelemetry/api' ;
5
+ import { context , Exception , Span , SpanStatusCode , trace } from '@opentelemetry/api' ;
6
6
import { SemanticAttributes } from '@opentelemetry/semantic-conventions' ;
7
7
import { metrics } from '@opentelemetry/api' ;
8
- import { AttributeNames } from '../enums/AttributeNames' ;
9
8
10
9
const meter = metrics . getMeter ( 'frontend' ) ;
11
10
const requestCounter = meter . createCounter ( 'app.frontend.requests' ) ;
12
11
13
12
const InstrumentationMiddleware = ( handler : NextApiHandler ) : NextApiHandler => {
14
13
return async ( request , response ) => {
15
- const { headers , method, url = '' , httpVersion } = request ;
14
+ const { method, url = '' } = request ;
16
15
const [ target ] = url . split ( '?' ) ;
17
16
18
- let span ;
19
- const baggage = propagation . getBaggage ( context . active ( ) ) ;
20
- if ( baggage ?. getEntry ( 'synthetic_request' ) ?. value == 'true' ) {
21
- // if synthetic_request baggage is set, create a new trace linked to the span in context
22
- // this span will look similar to the auto-instrumented HTTP span
23
- const syntheticSpan = trace . getSpan ( context . active ( ) ) as Span ;
24
- const tracer = trace . getTracer ( process . env . OTEL_SERVICE_NAME as string ) ;
25
- span = tracer . startSpan ( `HTTP ${ method } ` , {
26
- root : true ,
27
- kind : SpanKind . SERVER ,
28
- links : [ { context : syntheticSpan . spanContext ( ) } ] ,
29
- attributes : {
30
- 'app.synthetic_request' : true ,
31
- [ SemanticAttributes . HTTP_TARGET ] : target ,
32
- [ SemanticAttributes . HTTP_METHOD ] : method ,
33
- [ SemanticAttributes . HTTP_USER_AGENT ] : headers [ 'user-agent' ] || '' ,
34
- [ SemanticAttributes . HTTP_URL ] : `${ headers . host } ${ url } ` ,
35
- [ SemanticAttributes . HTTP_FLAVOR ] : httpVersion ,
36
- } ,
37
- } ) ;
38
- } else {
39
- // continue current trace/span
40
- span = trace . getSpan ( context . active ( ) ) as Span ;
41
- }
42
-
43
- if ( request . query [ 'sessionId' ] != null ) {
44
- span . setAttribute ( AttributeNames . SESSION_ID , request . query [ 'sessionId' ] ) ;
45
- }
17
+ const span = trace . getSpan ( context . active ( ) ) as Span ;
46
18
47
19
let httpStatus = 200 ;
48
20
try {
@@ -56,9 +28,6 @@ const InstrumentationMiddleware = (handler: NextApiHandler): NextApiHandler => {
56
28
} finally {
57
29
requestCounter . add ( 1 , { method, target, status : httpStatus } ) ;
58
30
span . setAttribute ( SemanticAttributes . HTTP_STATUS_CODE , httpStatus ) ;
59
- if ( baggage ?. getEntry ( 'synthetic_request' ) ?. value == 'true' ) {
60
- span . end ( ) ;
61
- }
62
31
}
63
32
} ;
64
33
} ;
0 commit comments