[Blockchain Watcher] (METRICS) Create metrics for job executions (#1451)

* Create metrics in pollingJob

* Create metrics in pollingJob

* Add number to metric

---------

Co-authored-by: julian merlo <julianmerlo@julians-MacBook-Pro.local>
This commit is contained in:
Julian 2024-06-03 16:40:18 -03:00 committed by GitHub
parent 7545acb77e
commit b0f4f6e319
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 6 deletions

View File

@ -1,7 +1,8 @@
import { setTimeout } from "timers/promises";
import winston from "winston";
import { Handler } from "../entities";
import { StatRepository } from "../repositories";
import { performance } from "perf_hooks";
import { setTimeout } from "timers/promises";
import { Handler } from "../entities";
import winston from "winston";
const DEFAULT_INTERVAL = 1_000;
@ -19,14 +20,15 @@ export abstract class RunPollingJob {
constructor(id: string, statRepo?: StatRepository, interval: number = DEFAULT_INTERVAL) {
this.interval = interval;
this.id = id;
this.running = true;
this.statRepo = statRepo;
this.running = true;
this.id = id;
}
public async run(handlers: Handler[]): Promise<void> {
this.logger.info("[run] Starting polling job");
await this.preHook();
while (this.running) {
if (!(await this.hasNext())) {
this.logger.info("[run] Finished processing");
@ -38,8 +40,16 @@ export abstract class RunPollingJob {
try {
this.report();
const jobStartTime = performance.now();
items = await this.get();
await Promise.all(handlers.map((handler) => handler(items)));
const jobEndTime = performance.now();
const jobExecutionTime = Number(((jobEndTime - jobStartTime) / 1000).toFixed(2));
this.statRepo?.measure("job_execution_time", jobExecutionTime, { job: this.id });
this.statRepo?.count("job_items_total", { id: this.id }, items.length);
} catch (e: Error | any) {
this.logger.error("[run] Error processing items", e);

View File

@ -98,7 +98,7 @@ export interface MetadataRepository<Metadata> {
export interface StatRepository {
count(id: string, labels: Record<string, any>, increase?: number): void;
measure(id: string, value: bigint, labels: Record<string, any>): void;
measure(id: string, value: bigint | number, labels: Record<string, any>): void;
report: () => Promise<string>;
}