diff --git a/components/gitpod-protocol/src/util/tracing.ts b/components/gitpod-protocol/src/util/tracing.ts index 4d7141d63994f7..38b1fcc253755e 100644 --- a/components/gitpod-protocol/src/util/tracing.ts +++ b/components/gitpod-protocol/src/util/tracing.ts @@ -8,7 +8,7 @@ import * as opentracing from 'opentracing'; import { TracingConfig, initTracerFromEnv } from 'jaeger-client'; import { Sampler, SamplingDecision } from './jaeger-client-types'; -import { initGlobalTracer } from 'opentracing'; +import { followsFrom, initGlobalTracer } from 'opentracing'; import { injectable } from 'inversify'; import { ResponseError } from 'vscode-jsonrpc'; import { log, LogContext } from './logging'; @@ -22,13 +22,15 @@ export type TraceContextWithSpan = TraceContext & { export namespace TraceContext { - export function startSpan(operation: string, parentCtx?: TraceContext): opentracing.Span { - let options: opentracing.SpanOptions | undefined = undefined; + export function startSpan(operation: string, parentCtx?: TraceContext, ...referencedSpans: (opentracing.Span | opentracing.SpanContext | undefined)[]): opentracing.Span { + const options: opentracing.SpanOptions = {}; if (parentCtx) { - options = { - childOf: parentCtx.span - }; + options.childOf = parentCtx.span; } + if (referencedSpans) { + options.references = referencedSpans.filter(s => s !== undefined).map(s => followsFrom(s!)); + } + return opentracing.globalTracer().startSpan(operation, options); } diff --git a/components/server/src/websocket/websocket-connection-manager.ts b/components/server/src/websocket/websocket-connection-manager.ts index 865f276b82c4fa..52420dd3b8d260 100644 --- a/components/server/src/websocket/websocket-connection-manager.ts +++ b/components/server/src/websocket/websocket-connection-manager.ts @@ -244,10 +244,21 @@ class GitpodJsonRpcConnectionHandler extends JsonRpcConnection onConnection(connection: MessageConnection, request?: object): void { const clientMetadata = ClientMetadata.fromRequest(request); + // trace the ws connection itself + const span = opentracing.globalTracer().startSpan("ws-connection"); + const ctx = { span }; + traceClientMetadata(ctx, clientMetadata); + TraceContext.setOWI(ctx, { + userId: clientMetadata.userId, + sessionId: clientMetadata.sessionId, + }); + connection.onClose(() => span.finish()); + const factory = new GitpodJsonRpcProxyFactory( this.createAccessGuard(request), this.createRateLimiter(clientMetadata.id, request), clientMetadata, + ctx, ); const proxy = factory.createProxy(); factory.target = this.targetFactory(proxy, request); @@ -272,6 +283,7 @@ class GitpodJsonRpcProxyFactory extends JsonRpcProxyFactory protected readonly accessGuard: FunctionAccessGuard, protected readonly rateLimiter: RateLimiter, protected readonly clientMetadata: ClientMetadata, + protected readonly connectionCtx: TraceContext, ) { super(); } @@ -283,7 +295,7 @@ class GitpodJsonRpcProxyFactory extends JsonRpcProxyFactory increaseApiCallUserCounter(method, "anonymous"); } - const span = opentracing.globalTracer().startSpan(method); + const span = TraceContext.startSpan(method, undefined, this.connectionCtx.span); const ctx = { span }; const userId = this.clientMetadata.userId; try {