File tree Expand file tree Collapse file tree 4 files changed +46
-2
lines changed Expand file tree Collapse file tree 4 files changed +46
-2
lines changed 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 * 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
+ }
Original file line number Diff line number Diff line change @@ -44,6 +44,14 @@ export namespace LogContext {
44
44
}
45
45
}
46
46
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
+
47
55
export interface LogPayload {
48
56
// placeholder to indicate that only dictionary-style objects should be passed as payload
49
57
}
@@ -333,7 +341,7 @@ function makeLogItem(
333
341
payloadArgs = payloadArgs . map ( ( arg ) => scrubPayload ( arg , plainLogging ) ) ;
334
342
const payload : unknown =
335
343
payloadArgs . length == 0 ? undefined : payloadArgs . length == 1 ? payloadArgs [ 0 ] : payloadArgs ;
336
- const logItem = {
344
+ const logItem : LogItem = {
337
345
// undefined fields get eliminated in JSON.stringify()
338
346
...reportedErrorEvent ,
339
347
component,
@@ -345,6 +353,11 @@ function makeLogItem(
345
353
payload,
346
354
loggedViaConsole : calledViaConsole ? true : undefined ,
347
355
} ;
356
+ if ( logItemHook ) {
357
+ try {
358
+ logItemHook ( logItem ) ;
359
+ } catch ( err ) { }
360
+ }
348
361
if ( plainLogging ) {
349
362
return `[${ logItem . severity } ] [${ logItem . component } ] ${ logItem . message }
350
363
${ JSON . stringify ( payload || "" , undefined , " " ) }
@@ -401,15 +414,17 @@ function makeReportedErrorEvent(error: Error | undefined): {} {
401
414
402
415
type LogItem = {
403
416
component ?: string ;
404
- severity ? : string ;
417
+ severity : string ;
405
418
time ?: string ;
419
+ context ?: LogContext ;
406
420
environment ?: string ;
407
421
region ?: string ;
408
422
message ?: string ;
409
423
messageStub ?: string ;
410
424
errorStub ?: string ;
411
425
error ?: unknown ;
412
426
payload ?: unknown ;
427
+ loggedViaConsole ?: boolean ;
413
428
} ;
414
429
415
430
function makeLogItemStub ( logItem : LogItem ) : LogItem {
Original file line number Diff line number Diff line change @@ -55,6 +55,7 @@ import express from "express";
55
55
import { Container } from "inversify" ;
56
56
import { Server } from "./server" ;
57
57
import { log , LogrusLogLevel } from "@gitpod/gitpod-protocol/lib/util/logging" ;
58
+ import { installLogCountMetric } from "@gitpod/gitpod-protocol/lib/util/logging-node" ;
58
59
import { TracingManager } from "@gitpod/gitpod-protocol/lib/util/tracing" ;
59
60
import { TypeORM } from "@gitpod/gitpod-db/lib" ;
60
61
import { dbConnectionsEnqueued , dbConnectionsFree , dbConnectionsTotal } from "./prometheus-metrics" ;
@@ -66,6 +67,7 @@ if (process.env.NODE_ENV === "development") {
66
67
67
68
log . enableJSONLogging ( "server" , process . env . VERSION , LogrusLogLevel . getFromEnv ( ) ) ;
68
69
installCtxLogAugmenter ( ) ;
70
+ installLogCountMetric ( ) ;
69
71
70
72
// eslint-disable-next-line @typescript-eslint/no-floating-promises
71
73
( async ( ) => {
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ import { Container } from "inversify";
8
8
import * as express from "express" ;
9
9
import * as prometheusClient from "prom-client" ;
10
10
import { log , LogrusLogLevel } from "@gitpod/gitpod-protocol/lib/util/logging" ;
11
+ import { installLogCountMetric } from "@gitpod/gitpod-protocol/lib/util/logging-node" ;
11
12
import { DebugApp } from "@gitpod/gitpod-protocol/lib/util/debug-app" ;
12
13
import { TypeORM } from "@gitpod/gitpod-db/lib/typeorm/typeorm" ;
13
14
import { TracingManager } from "@gitpod/gitpod-protocol/lib/util/tracing" ;
@@ -17,6 +18,7 @@ import { AppClusterWorkspaceInstancesController } from "./app-cluster-instance-c
17
18
import { redisMetricsRegistry } from "@gitpod/gitpod-db/lib" ;
18
19
19
20
log . enableJSONLogging ( "ws-manager-bridge" , undefined , LogrusLogLevel . getFromEnv ( ) ) ;
21
+ installLogCountMetric ( ) ;
20
22
21
23
export const start = async ( container : Container ) => {
22
24
process . on ( "uncaughtException" , function ( err ) {
You can’t perform that action at this time.
0 commit comments