Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit b3f970b

Browse files
committed
refactor prom client setup
1 parent b1f5529 commit b3f970b

File tree

2 files changed

+67
-151
lines changed

2 files changed

+67
-151
lines changed

substream-listener/src/index.mts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { App } from "uWebSockets.js";
33
import { isAddress } from "viem";
44
import { sendWebhook } from "./send-webhook.mjs";
55
import { registry } from "./prometheus.mjs";
6-
import { spawn } from "node:child_process";
76

87
// Creating a new router
98
const router = createRouter({

substream-listener/src/prometheus.mts

Lines changed: 67 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,9 @@
11
import type { Clock } from "@substreams/core/proto";
22
import type { BlockEmitter } from "@substreams/node";
3-
import client, {
4-
Counter,
5-
Gauge,
6-
Summary,
7-
Histogram,
8-
type CounterConfiguration,
9-
type GaugeConfiguration,
10-
type SummaryConfiguration,
11-
type HistogramConfiguration,
12-
} from "prom-client";
13-
import { logger } from "./logger.mjs";
3+
import promClient from "prom-client";
144

155
// Prometheus Exporter
16-
export const registry = new client.Registry();
17-
18-
export function registerCounter(
19-
name: string,
20-
help = "help",
21-
labelNames: string[] = [],
22-
config?: CounterConfiguration<string>,
23-
): Counter | undefined {
24-
try {
25-
const metric = registry.getSingleMetric(name);
26-
if (metric) {
27-
return metric as Counter;
28-
}
29-
registry.registerMetric(new Counter({ name, help, labelNames, ...config }));
30-
return registry.getSingleMetric(name) as Counter;
31-
} catch (e) {
32-
logger.error(e);
33-
}
34-
}
35-
36-
export function registerGauge(
37-
name: string,
38-
help = "help",
39-
labelNames: string[] = [],
40-
config?: GaugeConfiguration<string>,
41-
): Gauge | undefined {
42-
try {
43-
const metric = registry.getSingleMetric(name);
44-
if (metric) {
45-
console.log("metric", metric);
46-
return metric as Gauge;
47-
}
48-
registry.registerMetric(new Gauge({ name, help, labelNames, ...config }));
49-
return registry.getSingleMetric(name) as Gauge;
50-
} catch (e) {
51-
console.error(e);
52-
logger.error(e);
53-
}
54-
}
55-
56-
export function registerSummary(
57-
name: string,
58-
help = "help",
59-
labelNames: string[] = [],
60-
config?: SummaryConfiguration<string>,
61-
): Summary | undefined {
62-
try {
63-
const metric = registry.getSingleMetric(name);
64-
if (metric) {
65-
return metric as Summary;
66-
}
67-
registry.registerMetric(new Summary({ name, help, labelNames, ...config }));
68-
return registry.getSingleMetric(name) as Summary;
69-
} catch (e) {
70-
logger.error(e);
71-
}
72-
}
73-
74-
export function registerHistogram(
75-
name: string,
76-
help = "help",
77-
labelNames: string[] = [],
78-
config?: HistogramConfiguration<string>,
79-
): Histogram | undefined {
80-
try {
81-
const metric = registry.getSingleMetric(name);
82-
if (metric) {
83-
return metric as Histogram;
84-
}
85-
86-
registry.registerMetric(
87-
new Histogram({ name, help, labelNames, ...config }),
88-
);
89-
return registry.getSingleMetric(name) as Histogram;
90-
} catch (e) {
91-
logger.error(e);
92-
}
93-
}
6+
export const registry = promClient.register;
947

958
/**
969
* Default label names for all metrics
@@ -99,77 +12,89 @@ const DEFAULT_LABEL_NAMES = [
9912
"module_hash",
10013
"contract_address",
10114
"output_module",
102-
];
15+
] as const;
10316

10417
function calculateHeadBlockTimeDrift(clock: Clock) {
10518
const seconds = Number(clock.timestamp?.seconds);
10619
return Math.round(new Date().valueOf() / 1000 - seconds);
10720
}
10821

10922
// Counters
110-
export const substreams_sink_progress_message = registerCounter(
111-
"substreams_sink_progress_message",
112-
"The number of progress message received",
113-
["module", ...DEFAULT_LABEL_NAMES],
114-
);
115-
116-
const substreams_sink_data_message = registerCounter(
117-
"substreams_sink_data_message",
118-
"The number of data message received",
119-
DEFAULT_LABEL_NAMES,
120-
);
121-
122-
const substreams_sink_data_message_size_bytes = registerCounter(
123-
"substreams_sink_data_message_size_bytes",
124-
"The total size of in bytes of all data message received",
125-
DEFAULT_LABEL_NAMES,
126-
);
127-
128-
const substreams_sink_undo_message = registerCounter(
129-
"substreams_sink_undo_message",
130-
"The number of block undo message received",
131-
DEFAULT_LABEL_NAMES,
132-
);
23+
export const substreams_sink_progress_message = new promClient.Counter({
24+
name: "substreams_sink_progress_message",
25+
help: "The number of progress message received",
26+
labelNames: ["module", ...DEFAULT_LABEL_NAMES],
27+
});
28+
29+
const substreams_sink_data_message = new promClient.Counter({
30+
name: "substreams_sink_data_message",
31+
help: "The number of data message received",
32+
labelNames: DEFAULT_LABEL_NAMES,
33+
});
34+
35+
const substreams_sink_data_message_size_bytes = new promClient.Counter({
36+
name: "substreams_sink_data_message_size_bytes",
37+
help: "The total size of in bytes of all data message received",
38+
labelNames: DEFAULT_LABEL_NAMES,
39+
});
40+
41+
const substreams_sink_undo_message = new promClient.Counter({
42+
name: "substreams_sink_undo_message",
43+
help: "The number of block undo message received",
44+
labelNames: DEFAULT_LABEL_NAMES,
45+
});
13346

13447
// ------------------------------------------------------------------
13548

13649
// Gauges
13750

138-
const substreams_sink_backprocessing_completion = registerGauge(
139-
"substreams_sink_backprocessing_completion",
140-
"Determines if backprocessing is completed, which is if we receive a first data message",
141-
DEFAULT_LABEL_NAMES,
142-
);
143-
144-
const head_block_number = registerGauge(
145-
"head_block_number",
146-
"Last processed block number",
147-
DEFAULT_LABEL_NAMES,
148-
);
149-
150-
const head_block_time_drift = registerGauge(
151-
"head_block_time_drift",
152-
"Head block time drift in seconds",
153-
DEFAULT_LABEL_NAMES,
154-
);
155-
156-
const head_block_timestamp = registerGauge(
157-
"head_block_timestamp",
158-
"Head block timestamp",
159-
DEFAULT_LABEL_NAMES,
160-
);
161-
162-
const manifest = registerGauge(
163-
"manifest",
164-
"Register the manifest for the substreams sink",
165-
[
51+
const substreams_sink_backprocessing_completion = new promClient.Gauge({
52+
name: "substreams_sink_backprocessing_completion",
53+
help: "Determines if backprocessing is completed, which is if we receive a first data message",
54+
labelNames: DEFAULT_LABEL_NAMES,
55+
});
56+
57+
const head_block_number = new promClient.Gauge({
58+
name: "head_block_number",
59+
help: "Last processed block number",
60+
labelNames: DEFAULT_LABEL_NAMES,
61+
});
62+
63+
const head_block_time_drift = new promClient.Gauge({
64+
name: "head_block_time_drift",
65+
help: "Head block time drift in seconds",
66+
labelNames: DEFAULT_LABEL_NAMES,
67+
});
68+
69+
const head_block_timestamp = new promClient.Gauge({
70+
name: "head_block_timestamp",
71+
help: "Head block timestamp",
72+
labelNames: DEFAULT_LABEL_NAMES,
73+
});
74+
75+
const manifest = new promClient.Gauge({
76+
name: "manifest",
77+
help: "Register the manifest for the substreams sink",
78+
labelNames: [
16679
"substreams_endpoint",
16780
"start_block_num",
16881
"stop_block_num",
16982
"final_blocks_only",
17083
...DEFAULT_LABEL_NAMES,
17184
],
172-
);
85+
});
86+
87+
const sessionGauge = new promClient.Gauge({
88+
name: "session",
89+
help: "Substreams Session",
90+
labelNames: [
91+
"trace_id",
92+
"resolved_start_block",
93+
"linear_handoff_block",
94+
"max_parallel_workers",
95+
...DEFAULT_LABEL_NAMES,
96+
],
97+
});
17398

17499
// ------------------------------------------------------------------
175100

@@ -195,14 +120,6 @@ export function onPrometheusMetrics(
195120
);
196121

197122
emitter.on("session", (session) => {
198-
const sessionGauge = registerGauge("session", "Substreams Session", [
199-
"trace_id",
200-
"resolved_start_block",
201-
"linear_handoff_block",
202-
"max_parallel_workers",
203-
...DEFAULT_LABEL_NAMES,
204-
]);
205-
206123
sessionGauge?.set(
207124
{
208125
trace_id: String(session.traceId),

0 commit comments

Comments
 (0)