Skip to content

Commit 2443926

Browse files
committed
[server] register ctx log augmenter
1 parent e327baa commit 2443926

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

components/server/src/init.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ import { TracingManager } from "@gitpod/gitpod-protocol/lib/util/tracing";
5959
import { TypeORM } from "@gitpod/gitpod-db/lib";
6060
import { dbConnectionsEnqueued, dbConnectionsFree, dbConnectionsTotal } from "./prometheus-metrics";
6161
import { getExperimentsClientForBackend } from "@gitpod/gitpod-protocol/lib/experiments/configcat-server";
62+
import { installCtxLogAugmenter } from "./util/log-context";
6263
if (process.env.NODE_ENV === "development") {
6364
require("longjohn");
6465
}
6566

6667
log.enableJSONLogging("server", process.env.VERSION, LogrusLogLevel.getFromEnv());
68+
installCtxLogAugmenter();
6769

6870
// eslint-disable-next-line @typescript-eslint/no-floating-promises
6971
(async () => {

components/server/src/util/log-context.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@ function mapToLogContext(ctx: RequestContext): LogContextOptions {
2626

2727
// we are installing a special augmenter that enhances the log context if executed within `runWithContext`
2828
// with a contextId and a contextTimeMs, which denotes the amount of milliseconds since the context was created.
29-
const augmenter: LogContext.Augmenter = (ctx) => {
30-
const requestContext = ctxTryGet();
31-
let derivedContext: LogContextOptions = {};
32-
if (requestContext) {
33-
derivedContext = mapToLogContext(requestContext);
34-
}
35-
const result = {
36-
...derivedContext,
37-
...ctx,
29+
export function installCtxLogAugmenter() {
30+
const augmenter: LogContext.Augmenter = (ctx) => {
31+
const requestContext = ctxTryGet();
32+
let derivedContext: LogContextOptions = {};
33+
if (requestContext) {
34+
derivedContext = mapToLogContext(requestContext);
35+
}
36+
const result = {
37+
...derivedContext,
38+
...ctx,
39+
};
40+
// if its an empty object return undefined
41+
return Object.keys(result).length === 0 ? undefined : result;
3842
};
39-
// if its an empty object return undefined
40-
return Object.keys(result).length === 0 ? undefined : result;
41-
};
42-
LogContext.setAugmenter(augmenter);
43+
LogContext.setAugmenter(augmenter);
44+
}

0 commit comments

Comments
 (0)