Skip to content

Commit 604eea2

Browse files
committed
[log] log requestId
1 parent da018ad commit 604eea2

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,23 @@ let version: string | undefined;
1515

1616
export interface LogContext {
1717
organizationId?: string;
18+
requestId?: string;
1819
sessionId?: string;
1920
userId?: string;
2021
workspaceId?: string;
2122
instanceId?: string;
2223
}
24+
25+
/**
26+
* allows to globally augment the log context, default is an identity function
27+
*/
28+
let logContextAugmenter: LogContext.Augmenter = (context) => context;
29+
2330
export namespace LogContext {
31+
export type Augmenter = (context: LogContext | undefined) => LogContext | undefined;
32+
export function setAugmenter(augmenter: Augmenter): void {
33+
logContextAugmenter = augmenter;
34+
}
2435
export function from(params: { userId?: string; user?: any; request?: any }) {
2536
return <LogContext>{
2637
sessionId: params.request?.requestID,
@@ -306,6 +317,7 @@ function makeLogItem(
306317
if (context !== undefined && Object.keys(context).length == 0) {
307318
context = undefined;
308319
}
320+
context = logContextAugmenter(context);
309321
context = scrubPayload(context, plainLogging);
310322

311323
let reportedErrorEvent: {} = {};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License.AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { LogContext } from "@gitpod/gitpod-protocol/lib/util/logging";
8+
import { AsyncLocalStorage } from "node:async_hooks";
9+
10+
// we are installing a special augmenter that sets userId, sessionId and requestId if they are not set.
11+
12+
const asyncLocalStorage = new AsyncLocalStorage<LogContext>();
13+
const augmenter: LogContext.Augmenter = (ctx) => {
14+
const globalContext = asyncLocalStorage.getStore();
15+
return {
16+
...globalContext,
17+
...ctx,
18+
};
19+
};
20+
LogContext.setAugmenter(augmenter);
21+
22+
export async function runWithContext<T>(context: LogContext, fun: () => T): Promise<T> {
23+
return asyncLocalStorage.run(context, fun);
24+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import * as opentracing from "opentracing";
4949
import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing";
5050
import { GitpodHostUrl } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url";
5151
import { maskIp } from "../analytics";
52+
import { runWithContext } from "../util/log-context";
53+
import { v4 } from "uuid";
5254

5355
export type GitpodServiceFactory = () => GitpodServerImpl;
5456

@@ -373,6 +375,19 @@ class GitpodJsonRpcProxyFactory<T extends object> extends JsonRpcProxyFactory<T>
373375
}
374376

375377
protected async onRequest(method: string, ...args: any[]): Promise<any> {
378+
const userId = this.clientMetadata.userId;
379+
return runWithContext(
380+
{
381+
requestId: v4(),
382+
userId,
383+
},
384+
() => {
385+
return this.internalOnRequest(method, ...args);
386+
},
387+
);
388+
}
389+
390+
private async internalOnRequest(method: string, ...args: any[]): Promise<any> {
376391
const span = TraceContext.startSpan(method, undefined);
377392
const ctx = { span };
378393
const userId = this.clientMetadata.userId;

0 commit comments

Comments
 (0)