11/* eslint-disable @typescript-eslint/no-this-alias */
22/* eslint-disable @typescript-eslint/ban-types */
3- import { context , Context , diag , propagation , ROOT_CONTEXT , Span , SpanKind , SpanStatusCode , trace } from "@opentelemetry/api" ;
4- import { RPCMetadata , RPCType , setRPCMetadata } from "@opentelemetry/core" ;
3+ import type { Context , Exception , Span , SpanAttributes , SpanAttributeValue , SpanContext , SpanStatus , TimeInput } from "@opentelemetry/api" ;
4+ import { context , diag , propagation , ROOT_CONTEXT , SpanKind , SpanStatusCode , trace } from "@opentelemetry/api" ;
5+ import type { RPCMetadata } from "@opentelemetry/core" ;
6+ import { RPCType , setRPCMetadata } from "@opentelemetry/core" ;
57import {
68 InstrumentationBase ,
79 InstrumentationNodeModuleDefinition ,
@@ -12,11 +14,12 @@ import { getIncomingRequestAttributes } from "@opentelemetry/instrumentation-htt
1214import { SemanticAttributes } from "@opentelemetry/semantic-conventions" ;
1315import type * as http from "http" ;
1416import type * as https from "http" ;
15- import { IncomingMessage } from "http" ;
17+ import type { IncomingMessage } from "http" ;
1618import isPromise from "is-promise" ;
17- import { Duplex } from "stream" ;
18- import WS , { ErrorEvent , Server , WebSocket } from "ws" ;
19- import { WSInstrumentationConfig } from "./types" ;
19+ import type { Duplex } from "stream" ;
20+ import type { ErrorEvent , Server , WebSocket } from "ws" ;
21+ import type WS from "ws" ;
22+ import type { WSInstrumentationConfig } from "./types" ;
2023
2124const endSpan = ( traced : ( ) => any | Promise < any > , span : Span ) => {
2225 try {
@@ -419,8 +422,11 @@ export class WSInstrumentation extends InstrumentationBase<WS> {
419422 parentSpan ?. setAttributes ( {
420423 [ SemanticAttributes . HTTP_STATUS_CODE ] : 101 ,
421424 } ) ;
422- parentSpan ?. end ( ) ;
423- self . _requestSpans . delete ( request ) ;
425+ if ( parentSpan ) {
426+ const actualEndTime = new Date ( ) ;
427+ const originalEnd = parentSpan . end ;
428+ parentSpan . end = ( _endtime ?: TimeInput ) => originalEnd ( actualEndTime ) ; // Set the end time to the current time but don't end the span yet
429+ }
424430
425431 return callback . call ( this , websocket , request ) ;
426432 } ) ,
0 commit comments