LCOV - code coverage report
Current view: top level - app/shared/commands - metrics.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 49 0.0 %
Date: 2026-08-13 04:56:22 Functions: 0 4 0.0 %

          Line data    Source code
       1             : #include "../fd_config.h"
       2             : #include "../fd_action.h"
       3             : #include "../fd_bootinfo.h"
       4             : 
       5             : #include "../../../disco/metrics/fd_prometheus.h"
       6             : #include "../../../waltz/http/fd_http_server_private.h"
       7             : 
       8             : #include <unistd.h>
       9             : #include <errno.h>
      10             : #include <stdlib.h>
      11             : 
      12             : extern action_t * ACTIONS[];
      13             : 
      14             : static void
      15             : metrics_cmd_args( int *    pargc,
      16             :                   char *** pargv,
      17           0 :                   args_t * args ) {
      18           0 :   char const * topo_name = fd_env_strip_cmdline_cstr( pargc, pargv, "--topo", NULL, "" );
      19             : 
      20           0 :   ulong topo_name_len = strlen( topo_name );
      21           0 :   if( FD_UNLIKELY( topo_name_len > sizeof(args->metrics.topo)-1 ) ) FD_LOG_ERR(( "Unknown --topo %s", topo_name ));
      22           0 :   fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( args->metrics.topo ), topo_name, topo_name_len ) );
      23           0 : }
      24             : 
      25             : static void
      26             : reconstruct_topo( config_t *   config,
      27           0 :                   char const * topo_name ) {
      28           0 :   if( !topo_name[0] ) return; /* keep default action topo */
      29             : 
      30           0 :   action_t const * selected = NULL;
      31           0 :   for( action_t ** a=ACTIONS; *a; a++ ) {
      32           0 :     action_t const * action = *a;
      33           0 :     if( 0==strcmp( action->name, topo_name ) ) {
      34           0 :       selected = action;
      35           0 :       break;
      36           0 :     }
      37           0 :   }
      38             : 
      39           0 :   if( !selected       ) FD_LOG_ERR(( "Unknown --topo %s", topo_name ));
      40           0 :   if( !selected->topo ) FD_LOG_ERR(( "Cannot recover topology for --topo %s", topo_name ));
      41             : 
      42           0 :   selected->topo( config );
      43           0 : }
      44             : 
      45             : static void
      46             : metrics_cmd_fn( args_t *   args,
      47           0 :                 config_t * config ) {
      48           0 :   fd_bootinfo_adopt( config );
      49           0 :   reconstruct_topo( config, args->metrics.topo );
      50             : 
      51           0 :   fd_http_server_params_t params = {
      52           0 :     .max_connection_cnt    = 0UL,
      53           0 :     .max_ws_connection_cnt = 0UL,
      54           0 :     .max_request_len       = 0UL,
      55           0 :     .max_ws_recv_frame_len = 0UL,
      56           0 :     .max_ws_send_frame_cnt = 0UL,
      57           0 :     .outgoing_buffer_sz    = (1UL<<28UL), /* 256MiB */
      58           0 :   };
      59             : 
      60           0 :   fd_bootinfo_check_layout( config );
      61           0 :   fd_topo_join_workspaces( &config->topo, FD_SHMEM_JOIN_MODE_READ_ONLY, FD_TOPO_CORE_DUMP_LEVEL_DISABLED );
      62           0 :   fd_topo_fill( &config->topo );
      63             : 
      64           0 :   void * mem = aligned_alloc( fd_http_server_align(), fd_http_server_footprint( params ) );
      65           0 :   FD_TEST( mem );
      66           0 :   fd_http_server_t * http = fd_http_server_new( mem, params, (fd_http_server_callbacks_t){0}, NULL );
      67           0 :   fd_prometheus_render_all( &config->topo, http );
      68             : 
      69           0 :   ulong bytes_written;
      70           0 :   int err = fd_io_write( STDOUT_FILENO, http->oring, http->stage_len, http->stage_len, &bytes_written );
      71           0 :   if( FD_UNLIKELY( err ) ) {
      72           0 :     FD_LOG_ERR(( "write(STDOUT_FILENO,metrics,...) failed: %i-%s", err, fd_io_strerror( err ) ));
      73           0 :   }
      74           0 : }
      75             : 
      76             : static void
      77           0 : metrics_args_help( fd_action_help_t * help ) {
      78           0 :   fd_action_help_arg( help, "--topo", "<command>", "Build the topology from another subcommand (e.g. `gossip`) instead of\n"
      79           0 :                                                    "the default validator topology.  <command> is the name of a subcommand\n"
      80           0 :                                                    "that builds its own topology" );
      81           0 : }
      82             : 
      83             : action_t fd_action_metrics = {
      84             :   .name          = "metrics",
      85             :   .args          = metrics_cmd_args,
      86             :   .fn            = metrics_cmd_fn,
      87             :   .perm          = NULL,
      88             :   .description   = "Print the current validator Prometheus metrics to STDOUT",
      89             :   .detail        = "Connects to a running validator and writes a one-time snapshot of its\n"
      90             :                    "metrics to stdout in Prometheus text exposition format.",
      91             :   .usage         = "metrics [OPTIONS]",
      92             :   .args_help     = metrics_args_help,
      93             :   .is_immediate  = 0,
      94             :   .is_diagnostic = 1,
      95             : };

Generated by: LCOV version 1.14