1
1
import type { Clock } from "@substreams/core/proto" ;
2
2
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" ;
14
4
15
5
// 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 ;
94
7
95
8
/**
96
9
* Default label names for all metrics
@@ -99,77 +12,89 @@ const DEFAULT_LABEL_NAMES = [
99
12
"module_hash" ,
100
13
"contract_address" ,
101
14
"output_module" ,
102
- ] ;
15
+ ] as const ;
103
16
104
17
function calculateHeadBlockTimeDrift ( clock : Clock ) {
105
18
const seconds = Number ( clock . timestamp ?. seconds ) ;
106
19
return Math . round ( new Date ( ) . valueOf ( ) / 1000 - seconds ) ;
107
20
}
108
21
109
22
// 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
+ } ) ;
133
46
134
47
// ------------------------------------------------------------------
135
48
136
49
// Gauges
137
50
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 : [
166
79
"substreams_endpoint" ,
167
80
"start_block_num" ,
168
81
"stop_block_num" ,
169
82
"final_blocks_only" ,
170
83
...DEFAULT_LABEL_NAMES ,
171
84
] ,
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
+ } ) ;
173
98
174
99
// ------------------------------------------------------------------
175
100
@@ -195,14 +120,6 @@ export function onPrometheusMetrics(
195
120
) ;
196
121
197
122
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
-
206
123
sessionGauge ?. set (
207
124
{
208
125
trace_id : String ( session . traceId ) ,
0 commit comments