diff --git a/package-lock.json b/package-lock.json index 1ad4b78..081b968 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "substreams-sink-prometheus", - "version": "0.7.4", + "version": "0.7.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "substreams-sink-prometheus", - "version": "0.7.4", + "version": "0.7.5", "license": "MIT OR Apache-2.0", "dependencies": { "@substreams/core": "^0.1.10", diff --git a/package.json b/package.json index 80fe691..c807fb7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "substreams-sink-prometheus", - "version": "0.7.4", + "version": "0.7.5", "description": "Substreams Prometheus sink module", "main": "dist/index.js", "type": "module", diff --git a/src/metrics.ts b/src/metrics.ts index 5dc50a8..ca618bc 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -7,8 +7,8 @@ interface PrometheusCounter { counter: { operation: string; value: number; - labels?: any; } + labels?: any; } interface PrometheusGauge { @@ -16,13 +16,13 @@ interface PrometheusGauge { gauge: { operation: string; value: number; - labels?: any; } + labels?: any; } -export function handleOperations(message: JsonObject ): void -export function handleOperations(message: Message ): void -export function handleOperations(message: JsonObject | Message ) { +export function handleOperations(message: JsonObject): void +export function handleOperations(message: Message): void +export function handleOperations(message: JsonObject | Message) { for (const operation of (message as any)?.operations || []) { // convert message to JSON handleOperation(operation.toJson ? operation.toJson() : operation); @@ -38,16 +38,17 @@ export function handleOperation(promOp: any) { export function handleCounter(promOp: PrometheusCounter) { const name = promOp.name; - let { operation, value, labels } = promOp.counter; + let labels = promOp.labels ?? {}; + let { operation, value } = promOp.counter; // register - if ( !prometheus.registry.getSingleMetric(name) ) { + if (!prometheus.registry.getSingleMetric(name)) { prometheus.registerCounter(name, "custom help", Object.keys(labels ?? {})); // TO-DO! } const counter = prometheus.registry.getSingleMetric(promOp.name) as Counter; // provide empty object if no value is provided - if ( labels ) counter.labels(labels); + if (labels) counter.labels(labels); else labels = {}; // handle prometheus metrics @@ -63,17 +64,17 @@ export function handleCounter(promOp: PrometheusCounter) { export function handleGauge(promOp: PrometheusGauge) { const name = promOp.name; - let { operation, value, labels } = promOp.gauge; - if ( !labels ) labels = {}; // provide empty object if no value is provided + let labels = promOp.labels ?? {}; + let { operation, value } = promOp.gauge; // register - if ( !prometheus.registry.getSingleMetric(name) ) { + if (!prometheus.registry.getSingleMetric(name)) { prometheus.registerGauge(name, "custom help", Object.keys(labels)); // TO-DO! } const gauge = prometheus.registry.getSingleMetric(name) as Gauge; // provide empty object if no value is provided - if ( labels ) gauge.labels(labels); + if (labels) gauge.labels(labels); else labels = {}; // handle prometheus metrics