@@ -7,35 +7,19 @@ import { RuntimeLogFields } from '../../tracing/LogFields'
77import { CustomHttpTags , OpentracingTags , VTEXIncomingRequestTags } from '../../tracing/Tags'
88import { UserLandTracer } from '../../tracing/UserLandTracer'
99import { cloneAndSanitizeHeaders } from '../../tracing/utils'
10- import { hrToMillis , hrToMillisFloat } from '../../utils'
10+ import { hrToMillis } from '../../utils'
1111import { ServiceContext } from '../worker/runtime/typings'
12- import {
13- createConcurrentRequestsInstrument ,
14- createRequestsResponseSizesInstrument ,
15- createRequestsTimingsInstrument ,
16- createTotalAbortedRequestsInstrument ,
17- createTotalRequestsInstrument ,
18- RequestsMetricLabels ,
19- } from './metrics/instruments'
2012
2113const PATHS_BLACKLISTED_FOR_TRACING = [ '/_status' , '/healthcheck' ]
2214
2315export const addTracingMiddleware = ( tracer : Tracer ) => {
24- const concurrentRequests = createConcurrentRequestsInstrument ( )
25- const requestTimings = createRequestsTimingsInstrument ( )
26- const totalRequests = createTotalRequestsInstrument ( )
27- const responseSizes = createRequestsResponseSizesInstrument ( )
28- const abortedRequests = createTotalAbortedRequestsInstrument ( )
2916
3017 return async function addTracing ( ctx : ServiceContext , next : ( ) => Promise < void > ) {
31- const start = process . hrtime ( )
32- concurrentRequests . inc ( 1 )
3318 const rootSpan = tracer . extract ( FORMAT_HTTP_HEADERS , ctx . request . headers ) as undefined | SpanContext
3419 ctx . tracing = { tracer, currentSpan : undefined }
3520
3621 if ( ! shouldTrace ( ctx , rootSpan ) ) {
3722 await next ( )
38- concurrentRequests . dec ( 1 )
3923 return
4024 }
4125
@@ -47,9 +31,6 @@ export const addTracingMiddleware = (tracer: Tracer) => {
4731 const initialSamplingDecision = getTraceInfo ( currentSpan ) . isSampled
4832
4933 ctx . tracing = { currentSpan, tracer }
50- ctx . req . once ( 'aborted' , ( ) =>
51- abortedRequests . inc ( { [ RequestsMetricLabels . REQUEST_HANDLER ] : ( currentSpan as any ) . operationName as string } , 1 )
52- )
5334
5435 let responseClosed = false
5536 ctx . res . once ( 'close' , ( ) => ( responseClosed = true ) )
@@ -60,22 +41,6 @@ export const addTracingMiddleware = (tracer: Tracer) => {
6041 ErrorReport . create ( { originalError : err } ) . injectOnSpan ( currentSpan , ctx . vtex ?. logger )
6142 throw err
6243 } finally {
63- const responseLength = ctx . response . length
64- if ( responseLength ) {
65- responseSizes . observe (
66- { [ RequestsMetricLabels . REQUEST_HANDLER ] : ( currentSpan as any ) . operationName as string } ,
67- responseLength
68- )
69- }
70-
71- totalRequests . inc (
72- {
73- [ RequestsMetricLabels . REQUEST_HANDLER ] : ( currentSpan as any ) . operationName as string ,
74- [ RequestsMetricLabels . STATUS_CODE ] : ctx . response . status ,
75- } ,
76- 1
77- )
78-
7944 const traceInfo = getTraceInfo ( currentSpan )
8045 if ( traceInfo ?. isSampled ) {
8146 if ( ! initialSamplingDecision ) {
@@ -98,14 +63,6 @@ export const addTracingMiddleware = (tracer: Tracer) => {
9863 }
9964
10065 const onResFinished = ( ) => {
101- requestTimings . observe (
102- {
103- [ RequestsMetricLabels . REQUEST_HANDLER ] : ( currentSpan as any ) . operationName as string ,
104- } ,
105- hrToMillisFloat ( process . hrtime ( start ) )
106- )
107-
108- concurrentRequests . dec ( 1 )
10966 currentSpan ?. finish ( )
11067 }
11168
@@ -120,7 +77,8 @@ export const addTracingMiddleware = (tracer: Tracer) => {
12077
12178export const nameSpanOperationMiddleware = ( operationType : string , operationName : string ) => {
12279 return function nameSpanOperation ( ctx : ServiceContext , next : ( ) => Promise < void > ) {
123- ctx . tracing ?. currentSpan ?. setOperationName ( `${ operationType } :${ operationName } ` )
80+ ctx . requestHandlerName = `${ operationType } :${ operationName } `
81+ ctx . tracing ?. currentSpan ?. setOperationName ( ctx . requestHandlerName )
12482 return next ( )
12583 }
12684}
0 commit comments