Skip to content

Commit

Permalink
add labels
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Apr 28, 2023
1 parent e18ef28 commit 018d8f1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
11 changes: 9 additions & 2 deletions bin/cli.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
#!/usr/bin/env node

import { cli } from "substreams-sink";
import { action, DEFAULT_ADDRESS, DEFAULT_PORT } from "../index.js"
import { action, DEFAULT_ADDRESS, DEFAULT_COLLECT_DEFAULT_METRICS, DEFAULT_PORT } from "../index.js"
import pkg from "../package.json";

const program = cli.program(pkg);
const command = cli.run(program, pkg);
command.option('-p --port <int>', 'Listens on port number.', String(DEFAULT_PORT));
command.option('-a --address <string>', 'Prometheus address to connect.', DEFAULT_ADDRESS);
command.option('-l --labels [...string]', "To apply generic labels to all default metrics (ex: --labels foo=bar)", handleLabels, {})
command.option('--collect-default-metrics <boolean>', "Collect default metrics", DEFAULT_COLLECT_DEFAULT_METRICS);
command.action(action);
program.parse();
program.parse();

function handleLabels(value: string, previous: {}) {
const params = new URLSearchParams(value);
return { ...previous, ...Object.fromEntries(params) };
}
8 changes: 6 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createHash, download } from "substreams";
import { run, logger, RunOptions } from "substreams-sink";
import pkg from "./package.json";
import { collectDefaultMetrics, listen } from "./src/server";
import { collectDefaultMetrics, listen, setDefaultLabels } from "./src/server";
import { handleClock, handleManifest, handleOperations } from "./src/metrics";
export * from "./src/metrics";
export * from "./src/server";
Expand All @@ -13,10 +13,13 @@ export { logger };
export const DEFAULT_ADDRESS = 'localhost';
export const DEFAULT_PORT = 9102;
export const TYPE_NAME = "pinax.substreams.sink.prometheus.v1.PrometheusOperations"
export const DEFAULT_COLLECT_DEFAULT_METRICS = true;

export interface ActionOptions extends RunOptions {
address: string;
port: number;
labels: string;
collectDefaultMetrics: boolean;
}

export async function action(manifest: string, moduleName: string, options: ActionOptions) {
Expand All @@ -26,8 +29,9 @@ export async function action(manifest: string, moduleName: string, options: Acti
logger.info("download", {manifest, hash});

// Initialize Prometheus server
if ( options.collectDefaultMetrics ) collectDefaultMetrics(options.labels);
if ( options.labels ) setDefaultLabels(options.labels);
listen(options.port, options.address);
collectDefaultMetrics();

// Run Substreams
const substreams = run(spkg, moduleName, options);
Expand Down
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.4.4",
"version": "0.4.5",
"description": "Substreams Prometheus sink module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
9 changes: 7 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import { DEFAULT_ADDRESS } from "../index.js";
export const register = new client.Registry();

// Collect default metrics
export function collectDefaultMetrics() {
client.collectDefaultMetrics({ register });
export function collectDefaultMetrics(labels: Object = {}) {
client.collectDefaultMetrics({ register, labels });
}

// Set default labels
export function setDefaultLabels(labels: Object = {}) {
register.setDefaultLabels(labels);
}

// Create a local server to serve Prometheus gauges
Expand Down

0 comments on commit 018d8f1

Please sign in to comment.