Skip to content

Commit

Permalink
feat(relay): Add performance prometheus metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
kkirkov committed Oct 9, 2024
1 parent b266124 commit 2f65ced
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
2 changes: 2 additions & 0 deletions beacon-light-client/solidity/tasks/start-publishing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
accountBalanceGauge,
initPrometheusSetup,
registerGaugesForStartPublishing,
startResourceMetricsUpdate,
} from '@dendreth/utils/ts-utils/prometheus-utils';

const logger = getGenericLogger();
Expand Down Expand Up @@ -78,6 +79,7 @@ task('start-publishing', 'Run relayer')

initPrometheusSetup(args.prometheusPort, networkName);
registerGaugesForStartPublishing();
startResourceMetricsUpdate(networkName);
}

const currentConfig = await getNetworkConfig(args.followNetwork);
Expand Down
60 changes: 60 additions & 0 deletions libs/typescript/ts-utils/prometheus-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,63 @@ export const numberOfProofPublished = new client.Counter({
help: 'The number of proofs published(since last restart)',
labelNames: ['network'],
});

const gaugeCpuUser = new client.Gauge({
name: 'process_cpu_user_seconds_total',
help: 'Total user CPU time spent by the process in seconds.',
labelNames: ['label'], // Adding label
});

const gaugeCpuSystem = new client.Gauge({
name: 'process_cpu_system_seconds_total',
help: 'Total system CPU time spent by the process in seconds.',
labelNames: ['label'], // Adding label
});

const gaugeMemoryUsage = new client.Gauge({
name: 'process_memory_bytes',
help: 'Memory usage of the process in bytes.',
labelNames: ['label'], // Adding label
});

const gaugeHeapTotal = new client.Gauge({
name: 'process_heap_total_bytes',
help: 'Total heap memory allocated by the process in bytes.',
labelNames: ['label'], // Adding label
});

const gaugeHeapUsed = new client.Gauge({
name: 'process_heap_used_bytes',
help: 'Heap memory used by the process in bytes.',
labelNames: ['label'], // Adding label
});

const gaugeRss = new client.Gauge({
name: 'process_rss_bytes',
help: 'Resident set size (RSS) memory used by the process in bytes.',
labelNames: ['label'], // Adding label
});

export function startResourceMetricsUpdate(label: string) {
register.registerMetric(gaugeCpuUser);
register.registerMetric(gaugeCpuSystem);
register.registerMetric(gaugeMemoryUsage);
register.registerMetric(gaugeHeapTotal);
register.registerMetric(gaugeRss);

// Set an interval to regularly update the resource metrics
setInterval(() => {
const memoryUsage = process.memoryUsage();
const cpuUsage = process.cpuUsage();

// Update CPU metrics
gaugeCpuUser.labels(label).set(cpuUsage.user / 1e6); // Convert from microseconds to seconds
gaugeCpuSystem.labels(label).set(cpuUsage.system / 1e6); // Convert from microseconds to seconds

// Update memory metrics
gaugeMemoryUsage.labels(label).set(memoryUsage.rss); // Resident set size (RSS)
gaugeHeapTotal.labels(label).set(memoryUsage.heapTotal); // Total heap allocated
gaugeHeapUsed.labels(label).set(memoryUsage.heapUsed); // Heap memory in use
gaugeRss.labels(label).set(memoryUsage.rss); // RSS memory in bytes
}, 5000); // Update metrics every 5 seconds
}
6 changes: 5 additions & 1 deletion relay/workers/poll-updates/poll-updates-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import doUpdate from '@/workers/poll-updates/do_update';
import { getBeaconApi } from '@/implementations/beacon-api';
import { checkConfig } from '@dendreth/utils/ts-utils/common-utils';
import { getGenericLogger } from '@dendreth/utils/ts-utils/logger';
import { initPrometheusSetup } from '@dendreth/utils/ts-utils/prometheus-utils';
import {
initPrometheusSetup,
startResourceMetricsUpdate,
} from '@dendreth/utils/ts-utils/prometheus-utils';

const logger = getGenericLogger();
initPrometheusSetup();
startResourceMetricsUpdate('poll-update-worker');

(async () => {
const updatePollingConfig = {
Expand Down
2 changes: 2 additions & 0 deletions relay/workers/prover/prover-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import yargs from 'yargs';
import {
initPrometheusSetup,
registerGaugesForProver,
startResourceMetricsUpdate,
} from '@dendreth/utils/ts-utils/prometheus-utils';

(async () => {
Expand All @@ -19,6 +20,7 @@ import {
};
initPrometheusSetup(3000);
registerGaugesForProver();
startResourceMetricsUpdate('proofGenerationWorker');

checkConfig(proverConfig);

Expand Down

0 comments on commit 2f65ced

Please sign in to comment.