Skip to content

Commit

Permalink
used prometheus labels
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienR1 committed Nov 22, 2023
1 parent fc897c2 commit 9dd1326
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 36 deletions.
Binary file modified bun.lockb
Binary file not shown.
10 changes: 8 additions & 2 deletions src/clickhouse/handleSinkRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
38 changes: 4 additions & 34 deletions src/prometheus.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import client, { Counter, Gauge } from "prom-client";

export const registry = new client.Registry();
const entityChangesInsertedMetrics = new Map<string, Counter>();
const sinkRequestMetrics = new Map<string, Counter>();

export async function metrics() {
const headers = new Headers();
headers.set("Content-Type", registry.contentType);
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) {}
}
Expand All @@ -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();
}

0 comments on commit 9dd1326

Please sign in to comment.