File tree Expand file tree Collapse file tree 3 files changed +62
-0
lines changed Expand file tree Collapse file tree 3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -20,7 +20,17 @@ export interface LogContext {
20
20
workspaceId ?: string ;
21
21
instanceId ?: string ;
22
22
}
23
+
24
+ /**
25
+ * allows to globally augment the log context, default is an identity function
26
+ */
27
+ let logContextAugmenter : LogContext . Augmenter = ( context ) => context ;
28
+
23
29
export namespace LogContext {
30
+ export type Augmenter = ( context : LogContext | undefined ) => LogContext | undefined ;
31
+ export function setAugmenter ( augmenter : Augmenter ) : void {
32
+ logContextAugmenter = augmenter ;
33
+ }
24
34
export function from ( params : { userId ?: string ; user ?: any ; request ?: any } ) {
25
35
return < LogContext > {
26
36
sessionId : params . request ?. requestID ,
@@ -306,6 +316,7 @@ function makeLogItem(
306
316
if ( context !== undefined && Object . keys ( context ) . length == 0 ) {
307
317
context = undefined ;
308
318
}
319
+ context = logContextAugmenter ( context ) ;
309
320
context = scrubPayload ( context , plainLogging ) ;
310
321
311
322
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
+ import { v4 } from "uuid" ;
10
+
11
+ // we are installing a special augmenter that enhances the log context if executed within `runWithContext`
12
+ // with a contextId and a contextTimeMs, which denotes the amount of milliseconds since the context was created.
13
+ type EnhancedLogContext = LogContext & {
14
+ contextId ?: string ;
15
+ contextTimeMs : number ;
16
+ } ;
17
+ const asyncLocalStorage = new AsyncLocalStorage < EnhancedLogContext > ( ) ;
18
+ const augmenter : LogContext . Augmenter = ( ctx ) => {
19
+ const globalContext = asyncLocalStorage . getStore ( ) ;
20
+ const contextTime = globalContext ?. contextTimeMs ? Date . now ( ) - globalContext . contextTimeMs : undefined ;
21
+ return {
22
+ ...globalContext ,
23
+ contextTime,
24
+ ...ctx ,
25
+ } ;
26
+ } ;
27
+ LogContext . setAugmenter ( augmenter ) ;
28
+
29
+ export async function runWithContext < T > ( context : LogContext , fun : ( ) => T ) : Promise < T > {
30
+ return asyncLocalStorage . run (
31
+ {
32
+ ...context ,
33
+ contextId : v4 ( ) ,
34
+ contextTimeMs : Date . now ( ) ,
35
+ } ,
36
+ fun ,
37
+ ) ;
38
+ }
Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ 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" ;
52
53
53
54
export type GitpodServiceFactory = ( ) => GitpodServerImpl ;
54
55
@@ -373,6 +374,18 @@ class GitpodJsonRpcProxyFactory<T extends object> extends JsonRpcProxyFactory<T>
373
374
}
374
375
375
376
protected async onRequest ( method : string , ...args : any [ ] ) : Promise < any > {
377
+ const userId = this . clientMetadata . userId ;
378
+ return runWithContext (
379
+ {
380
+ userId,
381
+ } ,
382
+ ( ) => {
383
+ return this . internalOnRequest ( method , ...args ) ;
384
+ } ,
385
+ ) ;
386
+ }
387
+
388
+ private async internalOnRequest ( method : string , ...args : any [ ] ) : Promise < any > {
376
389
const span = TraceContext . startSpan ( method , undefined ) ;
377
390
const ctx = { span } ;
378
391
const userId = this . clientMetadata . userId ;
You can’t perform that action at this time.
0 commit comments