File tree Expand file tree Collapse file tree 3 files changed +51
-0
lines changed Expand file tree Collapse file tree 3 files changed +51
-0
lines changed Original file line number Diff line number Diff line change @@ -15,12 +15,23 @@ let version: string | undefined;
15
15
16
16
export interface LogContext {
17
17
organizationId ?: string ;
18
+ requestId ?: string ;
18
19
sessionId ?: string ;
19
20
userId ?: string ;
20
21
workspaceId ?: string ;
21
22
instanceId ?: string ;
22
23
}
24
+
25
+ /**
26
+ * allows to globally augment the log context, default is an identity function
27
+ */
28
+ let logContextAugmenter : LogContext . Augmenter = ( context ) => context ;
29
+
23
30
export namespace LogContext {
31
+ export type Augmenter = ( context : LogContext | undefined ) => LogContext | undefined ;
32
+ export function setAugmenter ( augmenter : Augmenter ) : void {
33
+ logContextAugmenter = augmenter ;
34
+ }
24
35
export function from ( params : { userId ?: string ; user ?: any ; request ?: any } ) {
25
36
return < LogContext > {
26
37
sessionId : params . request ?. requestID ,
@@ -306,6 +317,7 @@ function makeLogItem(
306
317
if ( context !== undefined && Object . keys ( context ) . length == 0 ) {
307
318
context = undefined ;
308
319
}
320
+ context = logContextAugmenter ( context ) ;
309
321
context = scrubPayload ( context , plainLogging ) ;
310
322
311
323
let reportedErrorEvent : { } = { } ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -49,6 +49,8 @@ import * as opentracing from "opentracing";
49
49
import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing" ;
50
50
import { GitpodHostUrl } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url" ;
51
51
import { maskIp } from "../analytics" ;
52
+ import { runWithContext } from "../util/log-context" ;
53
+ import { v4 } from "uuid" ;
52
54
53
55
export type GitpodServiceFactory = ( ) => GitpodServerImpl ;
54
56
@@ -373,6 +375,19 @@ class GitpodJsonRpcProxyFactory<T extends object> extends JsonRpcProxyFactory<T>
373
375
}
374
376
375
377
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 > {
376
391
const span = TraceContext . startSpan ( method , undefined ) ;
377
392
const ctx = { span } ;
378
393
const userId = this . clientMetadata . userId ;
You can’t perform that action at this time.
0 commit comments