Skip to content

Commit

Permalink
make http & metrics optional commands
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Feb 27, 2024
1 parent 085b9ca commit 42b7bc7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
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",
"version": "0.15.0",
"version": "0.15.1",
"description": "Substreams Sink",
"type": "module",
"exports": "./dist/index.js",
Expand Down
58 changes: 38 additions & 20 deletions src/commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import "dotenv/config";
import { Command, Option } from "commander";
import { DEFAULT_INACTIVITY_SECONDS, DEFAULT_PARAMS, DEFAULT_VERBOSE, DEFAULT_HOSTNAME, DEFAULT_PORT, DEFAULT_METRICS_LABELS, DEFAULT_COLLECT_DEFAULT_METRICS, DEFAULT_START_BLOCK, DEFAULT_DELAY_BEFORE_START, DEFAULT_HEADERS, DEFAULT_PRODUCTION_MODE, DEFAULT_FINAL_BLOCKS_ONLY } from "./config.js";

import { list } from "./list.js";
import { logger } from "./logger.js";

export interface Package {
name: string;
version: string;
Expand Down Expand Up @@ -38,17 +35,7 @@ export function program(pkg: Package) {
program.name(pkg.name).version(pkg.version, "-v, --version", `version for ${pkg.name}`);
program.command("completion").description("Generate the autocompletion script for the specified shell");
program.command("help").description("Display help for command");
program.command("list")
.showHelpAfterError()
.description("List all compatible output modules for a given Substreams package")
.argument("<spkg>", "URL or IPFS hash of Substreams package")
.action(async spkg => {
const modules = await list(spkg)
logger.info('list', { modules })
process.stdout.write(JSON.stringify(modules) + "\n")
});
program.showHelpAfterError();

return program;
}

Expand All @@ -73,8 +60,30 @@ function handleHeaders(value: string, previous: Headers) {
return headers;
}

export function run(program: Command, pkg: Package) {
return program.command("run")
export function run(program: Command, pkg: Package, options: AddRunOptions = {}) {
return addRunOptions(program.command("run"), pkg, options);
}

export function list(program: Command) {
program.command("list")
.showHelpAfterError()
.description("List all compatible output modules for a given Substreams package")
.argument("<spkg>", "URL or IPFS hash of Substreams package")
.action(async spkg => {
const modules = await list(spkg)
process.stdout.write(JSON.stringify(modules) + "\n");
});

return program;
}

interface AddRunOptions {
http?: boolean;
metrics?: boolean;
}

export function addRunOptions(program: Command, pkg: Package, options: AddRunOptions = {}) {
const command = program
.showHelpAfterError()
.description(pkg.description)
.addOption(new Option("-e --substreams-endpoint <string>", "Substreams gRPC endpoint to stream data from").makeOptionMandatory().env("SUBSTREAMS_ENDPOINT"))
Expand All @@ -89,11 +98,20 @@ export function run(program: Command, pkg: Package) {
.addOption(new Option("--cursor <string>", "Cursor to stream from. Leave blank for no cursor"))
.addOption(new Option("--production-mode <boolean>", "Enable production mode, allows cached Substreams data if available").default(DEFAULT_PRODUCTION_MODE).env("PRODUCTION_MODE"))
.addOption(new Option("--inactivity-seconds <int>", "If set, the sink will stop when inactive for over a certain amount of seconds").default(DEFAULT_INACTIVITY_SECONDS).env("INACTIVITY_SECONDS"))
.addOption(new Option("--hostname <string>", "The process will listen on this hostname for any HTTP and Prometheus metrics requests").default(DEFAULT_HOSTNAME).env("HOSTNAME"))
.addOption(new Option("--port <int>", "The process will listen on this port for any HTTP and Prometheus metrics requests").default(DEFAULT_PORT).env("PORT"))
.addOption(new Option("--metrics-labels [string...]", "To apply generic labels to all default metrics (ex: --labels foo=bar)").default(DEFAULT_METRICS_LABELS).env("METRICS_LABELS").argParser(handleMetricsLabels))
.addOption(new Option("--collect-default-metrics <boolean>", "Collect default metrics").default(DEFAULT_COLLECT_DEFAULT_METRICS).env("COLLECT_DEFAULT_METRICS"))
.addOption(new Option("--headers [string...]", "Set headers that will be sent on every requests (ex: --headers X-HEADER=headerA)").default(DEFAULT_HEADERS).env("HEADERS").argParser(handleHeaders))
.addOption(new Option("--final-blocks-only <boolean>", "Only process blocks that have pass finality, to prevent any reorg and undo signal by staying further away from the chain HEAD").default(DEFAULT_FINAL_BLOCKS_ONLY).env("FINAL_BLOCKS_ONLY"))
.addOption(new Option("--verbose <boolean>", "Enable verbose logging").default(DEFAULT_VERBOSE).env("VERBOSE"));
.addOption(new Option("--verbose <boolean>", "Enable verbose logging").default(DEFAULT_VERBOSE).env("VERBOSE"))

// HTTP and Prometheus metrics options
if ( options.http !== false ) {
command
.addOption(new Option("--hostname <string>", "The process will listen on this hostname for any HTTP and Prometheus metrics requests").default(DEFAULT_HOSTNAME).env("HOSTNAME"))
.addOption(new Option("--port <int>", "The process will listen on this port for any HTTP and Prometheus metrics requests").default(DEFAULT_PORT).env("PORT"))
}
if ( options.metrics !== false ) {
command
.addOption(new Option("--metrics-labels [string...]", "To apply generic labels to all default metrics (ex: --labels foo=bar)").default(DEFAULT_METRICS_LABELS).env("METRICS_LABELS").argParser(handleMetricsLabels))
.addOption(new Option("--collect-default-metrics <boolean>", "Collect default metrics").default(DEFAULT_COLLECT_DEFAULT_METRICS).env("COLLECT_DEFAULT_METRICS"))
}
return command;
}

0 comments on commit 42b7bc7

Please sign in to comment.