LCOV - code coverage report
Current view: top level - app/shared/commands - metrics.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 42 0.0 %
Date: 2025-10-13 04:42:14 Functions: 0 3 0.0 %

          Line data    Source code
       1             : #include "../fd_config.h"
       2             : #include "../fd_action.h"
       3             : 
       4             : #include "../../../disco/metrics/fd_prometheus.h"
       5             : #include "../../../waltz/http/fd_http_server_private.h"
       6             : 
       7             : #include <unistd.h>
       8             : #include <errno.h>
       9             : #include <stdlib.h>
      10             : 
      11             : extern action_t * ACTIONS[];
      12             : 
      13             : static void
      14             : metrics_cmd_args( int *    pargc,
      15             :                   char *** pargv,
      16           0 :                   args_t * args ) {
      17           0 :   char const * topo_name = fd_env_strip_cmdline_cstr( pargc, pargv, "--topo", NULL, "" );
      18             : 
      19           0 :   ulong topo_name_len = strlen( topo_name );
      20           0 :   if( FD_UNLIKELY( topo_name_len > sizeof(args->metrics.topo)-1 ) ) FD_LOG_ERR(( "Unknown --topo %s", topo_name ));
      21           0 :   fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( args->metrics.topo ), topo_name, topo_name_len ) );
      22           0 : }
      23             : 
      24             : static void
      25             : reconstruct_topo( config_t *   config,
      26           0 :                   char const * topo_name ) {
      27           0 :   if( !topo_name[0] ) return; /* keep default action topo */
      28             : 
      29           0 :   action_t const * selected = NULL;
      30           0 :   for( action_t ** a=ACTIONS; a; a++ ) {
      31           0 :     action_t const * action = *a;
      32           0 :     if( 0==strcmp( action->name, topo_name ) ) {
      33           0 :       selected = action;
      34           0 :       break;
      35           0 :     }
      36           0 :   }
      37             : 
      38           0 :   if( !selected       ) FD_LOG_ERR(( "Unknown --topo %s", topo_name ));
      39           0 :   if( !selected->topo ) FD_LOG_ERR(( "Cannot recover topology for --topo %s", topo_name ));
      40             : 
      41           0 :   selected->topo( config );
      42           0 : }
      43             : 
      44             : static void
      45             : metrics_cmd_fn( args_t *   args,
      46           0 :                 config_t * config ) {
      47           0 :   reconstruct_topo( config, args->metrics.topo );
      48             : 
      49           0 :   fd_http_server_params_t params = {
      50           0 :     .max_connection_cnt    = 0UL,
      51           0 :     .max_ws_connection_cnt = 0UL,
      52           0 :     .max_request_len       = 0UL,
      53           0 :     .max_ws_recv_frame_len = 0UL,
      54           0 :     .max_ws_send_frame_cnt = 0UL,
      55           0 :     .outgoing_buffer_sz    = (1UL<<28UL), /* 256MiB */
      56           0 :   };
      57             : 
      58           0 :   fd_topo_join_workspaces( &config->topo, FD_SHMEM_JOIN_MODE_READ_ONLY );
      59           0 :   fd_topo_fill( &config->topo );
      60             : 
      61           0 :   void * mem = aligned_alloc( fd_http_server_align(), fd_http_server_footprint( params ) );
      62           0 :   FD_TEST( mem );
      63           0 :   fd_http_server_t * http = fd_http_server_new( mem, params, (fd_http_server_callbacks_t){0}, NULL );
      64           0 :   fd_prometheus_render_all( &config->topo, http );
      65             : 
      66           0 :   ulong bytes_written;
      67           0 :   int err = fd_io_write( STDOUT_FILENO, http->oring, http->stage_len, http->stage_len, &bytes_written );
      68           0 :   if( FD_UNLIKELY( err ) ) {
      69           0 :     FD_LOG_ERR(( "write(STDOUT_FILENO,metrics,...) failed: %i-%s", err, fd_io_strerror( err ) ));
      70           0 :   }
      71           0 : }
      72             : 
      73             : action_t fd_action_metrics = {
      74             :   .name          = "metrics",
      75             :   .args          = metrics_cmd_args,
      76             :   .fn            = metrics_cmd_fn,
      77             :   .perm          = NULL,
      78             :   .description   = "Print the current validator Prometheus metrics to STDOUT",
      79             :   .is_immediate  = 0,
      80             :   .is_diagnostic = 1,
      81             : };

Generated by: LCOV version 1.14