Skip to content

Commit

Permalink
[server] Trace websocket connections as parent of API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
geropl authored and roboquat committed Dec 15, 2021
1 parent 1fc9d31 commit af13739
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
14 changes: 8 additions & 6 deletions components/gitpod-protocol/src/util/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
}

Expand Down
14 changes: 13 additions & 1 deletion components/server/src/websocket/websocket-connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,21 @@ class GitpodJsonRpcConnectionHandler<T extends object> 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<T>(
this.createAccessGuard(request),
this.createRateLimiter(clientMetadata.id, request),
clientMetadata,
ctx,
);
const proxy = factory.createProxy();
factory.target = this.targetFactory(proxy, request);
Expand All @@ -272,6 +283,7 @@ class GitpodJsonRpcProxyFactory<T extends object> extends JsonRpcProxyFactory<T>
protected readonly accessGuard: FunctionAccessGuard,
protected readonly rateLimiter: RateLimiter,
protected readonly clientMetadata: ClientMetadata,
protected readonly connectionCtx: TraceContext,
) {
super();
}
Expand All @@ -283,7 +295,7 @@ class GitpodJsonRpcProxyFactory<T extends object> extends JsonRpcProxyFactory<T>
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 {
Expand Down

0 comments on commit af13739

Please sign in to comment.