Skip to content

Commit

Permalink
[log] log contextId and userId (#18688)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenefftinge authored Sep 11, 2023
1 parent 49d1d4f commit 225040d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
11 changes: 11 additions & 0 deletions components/gitpod-protocol/src/util/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ export interface LogContext {
workspaceId?: string;
instanceId?: string;
}

/**
* allows to globally augment the log context, default is an identity function
*/
let logContextAugmenter: LogContext.Augmenter = (context) => context;

export namespace LogContext {
export type Augmenter = (context: LogContext | undefined) => LogContext | undefined;
export function setAugmenter(augmenter: Augmenter): void {
logContextAugmenter = augmenter;
}
export function from(params: { userId?: string; user?: any; request?: any }) {
return <LogContext>{
sessionId: params.request?.requestID,
Expand Down Expand Up @@ -306,6 +316,7 @@ function makeLogItem(
if (context !== undefined && Object.keys(context).length == 0) {
context = undefined;
}
context = logContextAugmenter(context);
context = scrubPayload(context, plainLogging);

let reportedErrorEvent: {} = {};
Expand Down
38 changes: 38 additions & 0 deletions components/server/src/util/log-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License.AGPL.txt in the project root for license information.
*/

import { LogContext } from "@gitpod/gitpod-protocol/lib/util/logging";
import { AsyncLocalStorage } from "node:async_hooks";
import { v4 } from "uuid";

// we are installing a special augmenter that enhances the log context if executed within `runWithContext`
// with a contextId and a contextTimeMs, which denotes the amount of milliseconds since the context was created.
type EnhancedLogContext = LogContext & {
contextId?: string;
contextTimeMs: number;
};
const asyncLocalStorage = new AsyncLocalStorage<EnhancedLogContext>();
const augmenter: LogContext.Augmenter = (ctx) => {
const globalContext = asyncLocalStorage.getStore();
const contextTime = globalContext?.contextTimeMs ? Date.now() - globalContext.contextTimeMs : undefined;
return {
...globalContext,
contextTime,
...ctx,
};
};
LogContext.setAugmenter(augmenter);

export async function runWithContext<T>(context: LogContext, fun: () => T): Promise<T> {
return asyncLocalStorage.run(
{
...context,
contextId: v4(),
contextTimeMs: Date.now(),
},
fun,
);
}
13 changes: 13 additions & 0 deletions components/server/src/websocket/websocket-connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import * as opentracing from "opentracing";
import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing";
import { GitpodHostUrl } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url";
import { maskIp } from "../analytics";
import { runWithContext } from "../util/log-context";

export type GitpodServiceFactory = () => GitpodServerImpl;

Expand Down Expand Up @@ -373,6 +374,18 @@ class GitpodJsonRpcProxyFactory<T extends object> extends JsonRpcProxyFactory<T>
}

protected async onRequest(method: string, ...args: any[]): Promise<any> {
const userId = this.clientMetadata.userId;
return runWithContext(
{
userId,
},
() => {
return this.internalOnRequest(method, ...args);
},
);
}

private async internalOnRequest(method: string, ...args: any[]): Promise<any> {
const span = TraceContext.startSpan(method, undefined);
const ctx = { span };
const userId = this.clientMetadata.userId;
Expand Down

0 comments on commit 225040d

Please sign in to comment.