Skip to content

Commit

Permalink
Fixed labels issue with Counter and Gauge
Browse files Browse the repository at this point in the history
  • Loading branch information
chamorin committed Jul 18, 2023
1 parent edb76d8 commit 015c47e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
25 changes: 13 additions & 12 deletions src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ interface PrometheusCounter {
counter: {
operation: string;
value: number;
labels?: any;
}
labels?: any;
}

interface PrometheusGauge {
name: string;
gauge: {
operation: string;
value: number;
labels?: any;
}
labels?: any;
}

export function handleOperations(message: JsonObject ): void
export function handleOperations(message: Message<AnyMessage> ): void
export function handleOperations(message: JsonObject | Message<AnyMessage> ) {
export function handleOperations(message: JsonObject): void
export function handleOperations(message: Message<AnyMessage>): void
export function handleOperations(message: JsonObject | Message<AnyMessage>) {
for (const operation of (message as any)?.operations || []) {
// convert message to JSON
handleOperation(operation.toJson ? operation.toJson() : operation);
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 015c47e

Please sign in to comment.