diff --git a/bun.lockb b/bun.lockb index d8a2163..122b41f 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/src/clickhouse/handleSinkRequest.ts b/src/clickhouse/handleSinkRequest.ts index 33ea00e..155b693 100644 --- a/src/clickhouse/handleSinkRequest.ts +++ b/src/clickhouse/handleSinkRequest.ts @@ -19,7 +19,10 @@ export async function handleSinkRequest({ data, ...metadata }: PayloadBody) { sqlite.triggerTransaction(); } - prometheus.incrementSinkRequests(metadata.manifest.chain, metadata.manifest.moduleHash); + prometheus.sink_requests.inc({ + chain: metadata.manifest.chain, + module_hash: metadata.manifest.moduleHash, + }); bufferedItems++; // EntityChanges @@ -196,7 +199,10 @@ function insertEntityChange( ) ); - prometheus.incrementInsertedEntityChanges(metadata.manifest.chain, metadata.manifest.moduleHash); + prometheus.entity_changes_inserted.inc({ + chain: metadata.manifest.chain, + module_hash: metadata.manifest.moduleHash, + }); } // TODO: implement function diff --git a/src/prometheus.ts b/src/prometheus.ts index 9a410e7..da16250 100644 --- a/src/prometheus.ts +++ b/src/prometheus.ts @@ -1,8 +1,6 @@ import client, { Counter, Gauge } from "prom-client"; export const registry = new client.Registry(); -const entityChangesInsertedMetrics = new Map(); -const sinkRequestMetrics = new Map(); export async function metrics() { const headers = new Headers(); @@ -10,9 +8,9 @@ export async function metrics() { return new Response(await registry.metrics(), { status: 200, headers }); } -export function registerCounter(name: string, help: string) { +export function registerCounter(name: string, help: string, labels?: string[]) { try { - registry.registerMetric(new client.Counter({ name, help })); + registry.registerMetric(new client.Counter({ name, help, labelNames: labels ?? []})); return registry.getSingleMetric(name) as Counter; } catch (error) {} } @@ -26,37 +24,9 @@ export function registerGauge(name: string, help: string) { export const requests = registerCounter("requests", "Total requests")!; export const request_errors = registerCounter("request_errors", "Total failed requests")!; -export const sink_requests = registerCounter("sink_requests", "Total sink requests")!; +export const sink_requests = registerCounter("sink_requests", "Total sink requests", ["chain", "module_hash"])!; -export const entity_changes_inserted = registerCounter("entity_changes_inserted", "Total inserted entity changes")!; +export const entity_changes_inserted = registerCounter("entity_changes_inserted", "Total inserted entity changes", ["chain", "module_hash"])!; export const entity_changes_updated = registerCounter("entity_changes_updated", "Total updated entity changes")!; export const entity_changes_deleted = registerCounter("entity_changes_deleted", "Total deleted entity changes")!; export const entity_changes_unsupported = registerCounter("entity_changes_unsupported", "Total unsupported entity changes")!; - -export function incrementInsertedEntityChanges(chain: string, moduleHash: string) { - entity_changes_inserted.inc(); - - const metricName = `entity_changes_inserted_${chain}_${moduleHash}`; - if (entityChangesInsertedMetrics.has(metricName)) { - entityChangesInsertedMetrics.get(metricName)!.inc(); - return; - } - - const metric = registerCounter(metricName, `Total inserted entity changes for ${chain} and ${moduleHash}`)!; - entityChangesInsertedMetrics.set(metricName, metric); - metric.inc(); -} - -export function incrementSinkRequests(chain: string, moduleHash: string) { - sink_requests.inc(); - - const metricName = `sink_requests_${chain}_${moduleHash}`; - if (sinkRequestMetrics.has(metricName)) { - sinkRequestMetrics.get(metricName)!.inc(); - return; - } - - const metric = registerCounter(metricName, `Total sink requests for ${chain} and ${moduleHash}`)!; - sinkRequestMetrics.set(metricName, metric); - metric.inc(); -}