Skip to content

[ts] Add metric gitpod_logs_total(level) for TS components #19093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions components/gitpod-protocol/src/util/logging-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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 * as prometheusClient from "prom-client";
import { LogHook } from "./logging";

const logsCounter = new prometheusClient.Counter({
name: "gitpod_logs_total",
help: "Total number of logs by level",
labelNames: ["level"],
registers: [prometheusClient.register],
});

export function reportLogCount(level: string) {
logsCounter.inc({ level });
}

export function installLogCountMetric() {
LogHook.setHook((item) => {
reportLogCount((item.severity || "").toLowerCase());
});
}
19 changes: 17 additions & 2 deletions components/gitpod-protocol/src/util/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ export namespace LogContext {
}
}

let logItemHook: LogHook.Hook | undefined = undefined;
export namespace LogHook {
export type Hook = (item: LogItem) => void;
export function setHook(hook: Hook): void {
logItemHook = hook;
}
}

export interface LogPayload {
// placeholder to indicate that only dictionary-style objects should be passed as payload
}
Expand Down Expand Up @@ -333,7 +341,7 @@ function makeLogItem(
payloadArgs = payloadArgs.map((arg) => scrubPayload(arg, plainLogging));
const payload: unknown =
payloadArgs.length == 0 ? undefined : payloadArgs.length == 1 ? payloadArgs[0] : payloadArgs;
const logItem = {
const logItem: LogItem = {
// undefined fields get eliminated in JSON.stringify()
...reportedErrorEvent,
component,
Expand All @@ -345,6 +353,11 @@ function makeLogItem(
payload,
loggedViaConsole: calledViaConsole ? true : undefined,
};
if (logItemHook) {
try {
logItemHook(logItem);
} catch (err) {}
}
if (plainLogging) {
return `[${logItem.severity}] [${logItem.component}] ${logItem.message}
${JSON.stringify(payload || "", undefined, " ")}
Expand Down Expand Up @@ -401,15 +414,17 @@ function makeReportedErrorEvent(error: Error | undefined): {} {

type LogItem = {
component?: string;
severity?: string;
severity: string;
time?: string;
context?: LogContext;
environment?: string;
region?: string;
message?: string;
messageStub?: string;
errorStub?: string;
error?: unknown;
payload?: unknown;
loggedViaConsole?: boolean;
};

function makeLogItemStub(logItem: LogItem): LogItem {
Expand Down
2 changes: 2 additions & 0 deletions components/server/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import express from "express";
import { Container } from "inversify";
import { Server } from "./server";
import { log, LogrusLogLevel } from "@gitpod/gitpod-protocol/lib/util/logging";
import { installLogCountMetric } from "@gitpod/gitpod-protocol/lib/util/logging-node";
import { TracingManager } from "@gitpod/gitpod-protocol/lib/util/tracing";
import { TypeORM } from "@gitpod/gitpod-db/lib";
import { dbConnectionsEnqueued, dbConnectionsFree, dbConnectionsTotal } from "./prometheus-metrics";
Expand All @@ -66,6 +67,7 @@ if (process.env.NODE_ENV === "development") {

log.enableJSONLogging("server", process.env.VERSION, LogrusLogLevel.getFromEnv());
installCtxLogAugmenter();
installLogCountMetric();

// eslint-disable-next-line @typescript-eslint/no-floating-promises
(async () => {
Expand Down
2 changes: 2 additions & 0 deletions components/ws-manager-bridge/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Container } from "inversify";
import * as express from "express";
import * as prometheusClient from "prom-client";
import { log, LogrusLogLevel } from "@gitpod/gitpod-protocol/lib/util/logging";
import { installLogCountMetric } from "@gitpod/gitpod-protocol/lib/util/logging-node";
import { DebugApp } from "@gitpod/gitpod-protocol/lib/util/debug-app";
import { TypeORM } from "@gitpod/gitpod-db/lib/typeorm/typeorm";
import { TracingManager } from "@gitpod/gitpod-protocol/lib/util/tracing";
Expand All @@ -17,6 +18,7 @@ import { AppClusterWorkspaceInstancesController } from "./app-cluster-instance-c
import { redisMetricsRegistry } from "@gitpod/gitpod-db/lib";

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

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