Skip to content

Commit 84b3361

Browse files
committed
Create a new timestamped span to enable let us keep the proper endTime
1 parent 29b0fde commit 84b3361

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

packages/opentelemetry-instrumentation-ws/src/index.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
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";
57
import {
68
InstrumentationBase,
79
InstrumentationNodeModuleDefinition,
@@ -12,11 +14,12 @@ import { getIncomingRequestAttributes } from "@opentelemetry/instrumentation-htt
1214
import { SemanticAttributes } from "@opentelemetry/semantic-conventions";
1315
import type * as http from "http";
1416
import type * as https from "http";
15-
import { IncomingMessage } from "http";
17+
import type { IncomingMessage } from "http";
1618
import 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

2124
const 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
}),

packages/opentelemetry-instrumentation-ws/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Span } from "@opentelemetry/api";
2-
import { InstrumentationConfig } from "@opentelemetry/instrumentation";
1+
import type { Span } from "@opentelemetry/api";
2+
import type { InstrumentationConfig } from "@opentelemetry/instrumentation";
33

44
export interface HookInfo {
55
payload: any | any[];

0 commit comments

Comments
 (0)