Skip to content

Commit 5641a7a

Browse files
committed
[ts] Add metric gitpod_logs_total(level) for TS components
1 parent c5ae89e commit 5641a7a

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 * as prometheusClient from "prom-client";
8+
import { LogHook } from "./logging";
9+
10+
const logsCounter = new prometheusClient.Counter({
11+
name: "gitpod_logs_total",
12+
help: "Total number of logs by level",
13+
labelNames: ["level"],
14+
registers: [prometheusClient.register],
15+
});
16+
17+
export function reportLogCount(level: string) {
18+
logsCounter.inc({ level });
19+
}
20+
21+
export function installLogCountMetric() {
22+
LogHook.setHook((item) => {
23+
reportLogCount((item.severity || "").toLowerCase());
24+
});
25+
}

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
import { scrubber } from "./scrubbing";
8-
import * as prometheusClient from "prom-client";
98

109
const inspect: (object: unknown) => string = require("util").inspect; // undefined in frontend
1110

@@ -45,6 +44,14 @@ export namespace LogContext {
4544
}
4645
}
4746

47+
let logItemHook: LogHook.Hook | undefined = undefined;
48+
export namespace LogHook {
49+
export type Hook = (item: LogItem) => void;
50+
export function setHook(hook: Hook): void {
51+
logItemHook = hook;
52+
}
53+
}
54+
4855
export interface LogPayload {
4956
// placeholder to indicate that only dictionary-style objects should be passed as payload
5057
}
@@ -248,16 +255,7 @@ namespace GoogleLogSeverity {
248255
};
249256
}
250257

251-
const logsCounter = new prometheusClient.Counter({
252-
name: "gitpod_logs_total",
253-
help: "Total number of logs by level",
254-
labelNames: ["level"],
255-
registers: [prometheusClient.register],
256-
});
257-
258258
function doLog(calledViaConsole: boolean, consoleLog: ConsoleLog, severity: GoogleLogSeverity, args: unknown[]): void {
259-
logsCounter.labels(severity).inc();
260-
261259
if (!jsonLogging) {
262260
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
263261
consoleLog(...args);
@@ -343,7 +341,7 @@ function makeLogItem(
343341
payloadArgs = payloadArgs.map((arg) => scrubPayload(arg, plainLogging));
344342
const payload: unknown =
345343
payloadArgs.length == 0 ? undefined : payloadArgs.length == 1 ? payloadArgs[0] : payloadArgs;
346-
const logItem = {
344+
const logItem: LogItem = {
347345
// undefined fields get eliminated in JSON.stringify()
348346
...reportedErrorEvent,
349347
component,
@@ -355,6 +353,11 @@ function makeLogItem(
355353
payload,
356354
loggedViaConsole: calledViaConsole ? true : undefined,
357355
};
356+
if (logItemHook) {
357+
try {
358+
logItemHook(logItem);
359+
} catch (err) {}
360+
}
358361
if (plainLogging) {
359362
return `[${logItem.severity}] [${logItem.component}] ${logItem.message}
360363
${JSON.stringify(payload || "", undefined, " ")}
@@ -411,15 +414,17 @@ function makeReportedErrorEvent(error: Error | undefined): {} {
411414

412415
type LogItem = {
413416
component?: string;
414-
severity?: string;
417+
severity: string;
415418
time?: string;
419+
context?: LogContext;
416420
environment?: string;
417421
region?: string;
418422
message?: string;
419423
messageStub?: string;
420424
errorStub?: string;
421425
error?: unknown;
422426
payload?: unknown;
427+
loggedViaConsole?: boolean;
423428
};
424429

425430
function makeLogItemStub(logItem: LogItem): LogItem {

components/server/src/init.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import express from "express";
5555
import { Container } from "inversify";
5656
import { Server } from "./server";
5757
import { log, LogrusLogLevel } from "@gitpod/gitpod-protocol/lib/util/logging";
58+
import { installLogCountMetric } from "@gitpod/gitpod-protocol/lib/util/logging-node";
5859
import { TracingManager } from "@gitpod/gitpod-protocol/lib/util/tracing";
5960
import { TypeORM } from "@gitpod/gitpod-db/lib";
6061
import { dbConnectionsEnqueued, dbConnectionsFree, dbConnectionsTotal } from "./prometheus-metrics";
@@ -66,6 +67,7 @@ if (process.env.NODE_ENV === "development") {
6667

6768
log.enableJSONLogging("server", process.env.VERSION, LogrusLogLevel.getFromEnv());
6869
installCtxLogAugmenter();
70+
installLogCountMetric();
6971

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

components/ws-manager-bridge/src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Container } from "inversify";
88
import * as express from "express";
99
import * as prometheusClient from "prom-client";
1010
import { log, LogrusLogLevel } from "@gitpod/gitpod-protocol/lib/util/logging";
11+
import { installLogCountMetric } from "@gitpod/gitpod-protocol/lib/util/logging-node";
1112
import { DebugApp } from "@gitpod/gitpod-protocol/lib/util/debug-app";
1213
import { TypeORM } from "@gitpod/gitpod-db/lib/typeorm/typeorm";
1314
import { TracingManager } from "@gitpod/gitpod-protocol/lib/util/tracing";
@@ -17,6 +18,7 @@ import { AppClusterWorkspaceInstancesController } from "./app-cluster-instance-c
1718
import { redisMetricsRegistry } from "@gitpod/gitpod-db/lib";
1819

1920
log.enableJSONLogging("ws-manager-bridge", undefined, LogrusLogLevel.getFromEnv());
21+
installLogCountMetric();
2022

2123
export const start = async (container: Container) => {
2224
process.on("uncaughtException", function (err) {

0 commit comments

Comments
 (0)