From 4609f9b890bdf602cf2941c6c64dec3be1ca8a53 Mon Sep 17 00:00:00 2001 From: Drew Nutter Date: Fri, 22 Dec 2023 10:03:17 -0500 Subject: [PATCH] feat(prometheus): expose metrics port config to cli --- src/cmd/cmd.zig | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/cmd/cmd.zig b/src/cmd/cmd.zig index fd653ae86..8b147766b 100644 --- a/src/cmd/cmd.zig +++ b/src/cmd/cmd.zig @@ -34,11 +34,21 @@ var gossip_entrypoints_option = cli.Option{ .value_name = "Entrypoints", }; +var metrics_port_option = cli.Option{ + .long_name = "metrics-port", + .help = "port to expose prometheus metrics via http", + .short_alias = 'm', + .value = cli.OptionValue{ .int = 12345 }, + .required = false, + .value_name = "port_number", +}; + var app = &cli.App{ .name = "sig", .description = "Sig is a Solana client implementation written in Zig.\nThis is still a WIP, PRs welcome.", .version = "0.1.1", .author = "Syndica & Contributors", + .options = &.{&metrics_port_option}, .subcommands = &.{ &cli.Command{ .name = "identity", @@ -79,7 +89,7 @@ fn gossip(_: []const []const u8) !void { // var logger: Logger = .noop; - _ = try spawnMetrics(gpa_allocator, 12345); + _ = try spawnMetrics(gpa_allocator); var my_keypair = try getOrInitIdentity(gpa_allocator, logger); @@ -126,13 +136,13 @@ fn gossip(_: []const []const u8) !void { handle.join(); } -/// Initializes the global registry. -/// Spawns a thread to serve the metrics over http. -/// Returns error if registry was already initialized. +/// Initializes the global registry. Returns error if registry was already initialized. +/// Spawns a thread to serve the metrics over http on the CLI configured port. /// Uses same allocator for both registry and http adapter. -fn spawnMetrics(allocator: std.mem.Allocator, port: u16) !std.Thread { +fn spawnMetrics(allocator: std.mem.Allocator) !std.Thread { + var metrics_port: u16 = @intCast(metrics_port_option.value.int.?); const registry = try global_registry.initialize(Registry(.{}).init, .{allocator}); - return try std.Thread.spawn(.{}, servePrometheus, .{ allocator, registry, port }); + return try std.Thread.spawn(.{}, servePrometheus, .{ allocator, registry, metrics_port }); } pub fn run() !void {