Skip to content

Commit af13739

Browse files
geroplroboquat
authored andcommitted
[server] Trace websocket connections as parent of API calls
1 parent 1fc9d31 commit af13739

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

components/gitpod-protocol/src/util/tracing.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import * as opentracing from 'opentracing';
99
import { TracingConfig, initTracerFromEnv } from 'jaeger-client';
1010
import { Sampler, SamplingDecision } from './jaeger-client-types';
11-
import { initGlobalTracer } from 'opentracing';
11+
import { followsFrom, initGlobalTracer } from 'opentracing';
1212
import { injectable } from 'inversify';
1313
import { ResponseError } from 'vscode-jsonrpc';
1414
import { log, LogContext } from './logging';
@@ -22,13 +22,15 @@ export type TraceContextWithSpan = TraceContext & {
2222

2323

2424
export namespace TraceContext {
25-
export function startSpan(operation: string, parentCtx?: TraceContext): opentracing.Span {
26-
let options: opentracing.SpanOptions | undefined = undefined;
25+
export function startSpan(operation: string, parentCtx?: TraceContext, ...referencedSpans: (opentracing.Span | opentracing.SpanContext | undefined)[]): opentracing.Span {
26+
const options: opentracing.SpanOptions = {};
2727
if (parentCtx) {
28-
options = {
29-
childOf: parentCtx.span
30-
};
28+
options.childOf = parentCtx.span;
3129
}
30+
if (referencedSpans) {
31+
options.references = referencedSpans.filter(s => s !== undefined).map(s => followsFrom(s!));
32+
}
33+
3234
return opentracing.globalTracer().startSpan(operation, options);
3335
}
3436

components/server/src/websocket/websocket-connection-manager.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,21 @@ class GitpodJsonRpcConnectionHandler<T extends object> extends JsonRpcConnection
244244
onConnection(connection: MessageConnection, request?: object): void {
245245
const clientMetadata = ClientMetadata.fromRequest(request);
246246

247+
// trace the ws connection itself
248+
const span = opentracing.globalTracer().startSpan("ws-connection");
249+
const ctx = { span };
250+
traceClientMetadata(ctx, clientMetadata);
251+
TraceContext.setOWI(ctx, {
252+
userId: clientMetadata.userId,
253+
sessionId: clientMetadata.sessionId,
254+
});
255+
connection.onClose(() => span.finish());
256+
247257
const factory = new GitpodJsonRpcProxyFactory<T>(
248258
this.createAccessGuard(request),
249259
this.createRateLimiter(clientMetadata.id, request),
250260
clientMetadata,
261+
ctx,
251262
);
252263
const proxy = factory.createProxy();
253264
factory.target = this.targetFactory(proxy, request);
@@ -272,6 +283,7 @@ class GitpodJsonRpcProxyFactory<T extends object> extends JsonRpcProxyFactory<T>
272283
protected readonly accessGuard: FunctionAccessGuard,
273284
protected readonly rateLimiter: RateLimiter,
274285
protected readonly clientMetadata: ClientMetadata,
286+
protected readonly connectionCtx: TraceContext,
275287
) {
276288
super();
277289
}
@@ -283,7 +295,7 @@ class GitpodJsonRpcProxyFactory<T extends object> extends JsonRpcProxyFactory<T>
283295
increaseApiCallUserCounter(method, "anonymous");
284296
}
285297

286-
const span = opentracing.globalTracer().startSpan(method);
298+
const span = TraceContext.startSpan(method, undefined, this.connectionCtx.span);
287299
const ctx = { span };
288300
const userId = this.clientMetadata.userId;
289301
try {

0 commit comments

Comments
 (0)