Skip to content

Commit

Permalink
feat: Expose log count metric from Runner (#684)
Browse files Browse the repository at this point in the history
Creates a new `winston` transport method to count logs by level, and
exposes a new prometheus metric to record this value.

Additionally, metrics recorded on the main thread were not captured.
This was not an issue as the majority of metrics were done within the
worker. But since we also log on main thread, this PR updates metrics
aggregation to expose main thread metrics.
  • Loading branch information
morgsmccauley authored Apr 22, 2024
1 parent c262f9f commit 328c704
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
3 changes: 2 additions & 1 deletion runner/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"redis": "^4.6.7",
"verror": "^1.10.1",
"vm2": "^3.9.19",
"winston": "^3.13.0"
"winston": "^3.13.0",
"winston-transport": "^4.7.0"
}
}
12 changes: 12 additions & 0 deletions runner/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import winston from 'winston';
import { LoggingWinston } from '@google-cloud/logging-winston';
import Transport from 'winston-transport';

import { METRICS } from './metrics';

const { format, transports } = winston;

class LogCounter extends Transport {
log (info: { level: string }, callback: () => void): void {
METRICS.LOGS_COUNT.labels({ level: info.level }).inc();

callback();
}
}

const logger = winston.createLogger({
level: 'info',
format: format.combine(
format.timestamp(),
format.errors({ stack: true }),
),
transports: [new LogCounter()],
});

if (process.env.GCP_LOGGING_ENABLED) {
Expand Down
13 changes: 11 additions & 2 deletions runner/src/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express';
import { Gauge, Histogram, Counter, AggregatorRegistry } from 'prom-client';
import { Gauge, Histogram, Counter, AggregatorRegistry, register } from 'prom-client';

import logger from './logger';

Expand Down Expand Up @@ -56,6 +56,12 @@ const EXECUTION_DURATION = new Histogram({
labelNames: ['indexer'],
});

const LOGS_COUNT = new Counter({
name: 'queryapi_runner_logs_count',
help: 'Number of messages logged',
labelNames: ['level'],
});

export const METRICS = {
HEAP_TOTAL_ALLOCATION,
HEAP_USED,
Expand All @@ -66,6 +72,7 @@ export const METRICS = {
UNPROCESSED_STREAM_MESSAGES,
LAST_PROCESSED_BLOCK_HEIGHT,
EXECUTION_DURATION,
LOGS_COUNT
};

const aggregatorRegistry = new AggregatorRegistry();
Expand All @@ -87,7 +94,9 @@ export const startServer = async (): Promise<void> => {
app.get('/metrics', async (_req, res) => {
res.set('Content-Type', aggregatorRegistry.contentType);

const metrics = await AggregatorRegistry.aggregate(Array.from(workerMetrics.values())).metrics();
const mainThreadMetrics = await register.getMetricsAsJSON();
const metrics = await AggregatorRegistry.aggregate([...Array.from(workerMetrics.values()), mainThreadMetrics]).metrics();

res.send(metrics);
});

Expand Down

0 comments on commit 328c704

Please sign in to comment.