LCOV - code coverage report
Current view: top level - disco/metrics - fd_metric_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 107 0.0 %
Date: 2026-10-22 18:10:43 Functions: 0 9 0.0 %

          Line data    Source code
       1             : #include "fd_prometheus.h"
       2             : #include "fd_metrics.h"
       3             : #include "../waker/fd_waker.h"
       4             : #include "../../waltz/http/fd_http_server_private.h"
       5             : #include "../../util/net/fd_ip4.h"
       6             : 
       7             : #include <sys/types.h>
       8             : #include <sys/socket.h> /* SOCK_CLOEXEC, SOCK_NONBLOCK needed for seccomp filter */
       9             : #include <time.h> /* CLOCK_REALTIME for seccomp filter */
      10             : #include <unistd.h>
      11             : #include <string.h>
      12             : 
      13             : #include <linux/futex.h>
      14             : #include "generated/fd_metric_tile_seccomp.h"
      15             : 
      16             : #define FD_HTTP_SERVER_METRICS_MAX_CONNS          128
      17             : #define FD_HTTP_SERVER_METRICS_MAX_REQUEST_LEN    8192
      18             : #define FD_HTTP_SERVER_METRICS_OUTGOING_BUFFER_SZ (32UL<<20UL) /* 32MiB reserved for buffering metrics responses */
      19             : 
      20             : const fd_http_server_params_t METRICS_PARAMS = {
      21             :   .max_connection_cnt    = FD_HTTP_SERVER_METRICS_MAX_CONNS,
      22             :   .max_ws_connection_cnt = 0UL,
      23             :   .max_request_len       = FD_HTTP_SERVER_METRICS_MAX_REQUEST_LEN,
      24             :   .max_ws_recv_frame_len = 0UL,
      25             :   .max_ws_send_frame_cnt = 0UL,
      26             :   .outgoing_buffer_sz    = FD_HTTP_SERVER_METRICS_OUTGOING_BUFFER_SZ,
      27             : };
      28             : 
      29             : typedef struct {
      30             :   fd_topo_t const * topo;
      31             : 
      32             :   fd_http_server_t * metrics_server;
      33             : 
      34             :   ulong   waker_client_idx;
      35             :   ulong * waker_fseq;
      36             : 
      37             :   long boot_ts;
      38             : } fd_metric_ctx_t;
      39             : 
      40             : FD_FN_CONST static inline ulong
      41           0 : scratch_align( void ) {
      42           0 :   return 128UL;
      43           0 : }
      44             : 
      45             : FD_FN_PURE static inline ulong
      46           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
      47           0 :   (void)tile;
      48             : 
      49           0 :   ulong l = FD_LAYOUT_INIT;
      50           0 :   l = FD_LAYOUT_APPEND( l, alignof( fd_metric_ctx_t ), sizeof( fd_metric_ctx_t ) );
      51           0 :   l = FD_LAYOUT_APPEND( l, fd_http_server_align(), fd_http_server_footprint( METRICS_PARAMS ) );
      52           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
      53           0 : }
      54             : 
      55             : static inline void
      56             : before_credit( fd_metric_ctx_t *   ctx,
      57             :                fd_stem_context_t * stem,
      58           0 :                int *               charge_busy ) {
      59           0 :   if( FD_UNLIKELY( fd_fseq_query( ctx->waker_fseq )==1UL ) ) {
      60           0 :     fd_fseq_update( ctx->waker_fseq, 0UL );
      61           0 :     *charge_busy = fd_http_server_epoll_poll( ctx->metrics_server, ULONG_MAX );
      62           0 :     fd_waker_client_rearm( ctx->waker_client_idx );
      63           0 :   } else if( FD_LIKELY( !stem->sleep ) ) {
      64           0 :     fd_log_sleep( (long)1e6 );
      65           0 :   }
      66           0 : }
      67             : 
      68             : static fd_http_server_response_t
      69           0 : metrics_http_request( fd_http_server_request_t const * request ) {
      70           0 :   fd_metric_ctx_t * ctx = (fd_metric_ctx_t *)request->ctx;
      71             : 
      72           0 :   if( FD_UNLIKELY( request->method!=FD_HTTP_SERVER_METHOD_GET ) ) {
      73           0 :     return (fd_http_server_response_t){
      74           0 :       .status = 400,
      75           0 :     };
      76           0 :   }
      77             : 
      78           0 :   if( FD_LIKELY( !strcmp( request->path, "/metrics" ) ) ) {
      79           0 :     fd_prometheus_render_all( ctx->topo, ctx->metrics_server );
      80             : 
      81           0 :     fd_http_server_response_t response = {
      82           0 :       .status       = 200,
      83           0 :       .content_type = "text/plain; version=0.0.4",
      84           0 :     };
      85           0 :     if( FD_UNLIKELY( fd_http_server_stage_body( ctx->metrics_server, &response ) ) ) {
      86           0 :       FD_LOG_WARNING(( "fd_http_server_stage_body failed, metrics response too long" ));
      87           0 :       return (fd_http_server_response_t){
      88           0 :         .status = 500,
      89           0 :       };
      90           0 :     }
      91           0 :     return response;
      92           0 :   } else {
      93           0 :     return (fd_http_server_response_t){
      94           0 :       .status = 404,
      95           0 :     };
      96           0 :   }
      97           0 : }
      98             : 
      99             : static void
     100           0 : metrics_write( fd_metric_ctx_t * ctx ) {
     101           0 :   FD_MGAUGE_SET( METRIC, BOOT_TIME, (ulong)ctx->boot_ts );
     102             : 
     103           0 :   FD_MGAUGE_SET( METRIC, CONN_OPEN, ctx->metrics_server->metrics.connection_cnt );
     104             : 
     105           0 :   FD_MCNT_SET( METRIC, CONN_TX_BYTES, ctx->metrics_server->metrics.bytes_written );
     106           0 :   FD_MCNT_SET( METRIC, CONN_RX_BYTES, ctx->metrics_server->metrics.bytes_read );
     107           0 : }
     108             : 
     109             : static void
     110             : privileged_init( fd_topo_t const *      topo,
     111           0 :                  fd_topo_tile_t const * tile ) {
     112           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     113             : 
     114           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     115           0 :   fd_metric_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_metric_ctx_t ), sizeof( fd_metric_ctx_t ) );
     116             : 
     117           0 :   fd_http_server_t * _metrics = FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(), fd_http_server_footprint( METRICS_PARAMS ) );
     118             : 
     119           0 :   fd_http_server_callbacks_t metrics_callbacks = {
     120           0 :     .request = metrics_http_request,
     121           0 :   };
     122           0 :   ctx->metrics_server = fd_http_server_join( fd_http_server_new( _metrics, METRICS_PARAMS, metrics_callbacks, ctx ) );
     123             : 
     124           0 :   ctx->waker_client_idx = tile->waker_client_idx;
     125           0 :   FD_TEST( ctx->waker_client_idx!=ULONG_MAX );
     126           0 :   fd_http_server_listen( ctx->metrics_server, FD_WAKER_INNER_FD( ctx->waker_client_idx ), tile->metric.prometheus_listen_addr, tile->metric.prometheus_listen_port );
     127           0 : }
     128             : 
     129             : static void
     130             : unprivileged_init( fd_topo_t const *      topo,
     131           0 :                    fd_topo_tile_t const * tile ) {
     132           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     133             : 
     134           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     135           0 :   fd_metric_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_metric_ctx_t ), sizeof( fd_metric_ctx_t ) );
     136             : 
     137           0 :   ctx->topo = topo;
     138           0 :   ctx->boot_ts = fd_log_wallclock();
     139             : 
     140           0 :   FD_TEST( ctx->waker_client_idx!=ULONG_MAX );
     141           0 :   ctx->waker_fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->waker_fseq_obj_id ) );
     142           0 :   FD_TEST( ctx->waker_fseq );
     143             : 
     144           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
     145           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     146           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     147             : 
     148           0 :   FD_LOG_NOTICE(( "prometheus server listening at %shttp://" FD_IP4_ADDR_FMT ":%u/metrics%s", fd_log_style_bold(), FD_IP4_ADDR_FMT_ARGS( tile->metric.prometheus_listen_addr ), tile->metric.prometheus_listen_port, fd_log_style_normal() ));
     149           0 : }
     150             : 
     151             : static ulong
     152             : populate_allowed_seccomp( fd_topo_t const *      topo,
     153             :                           fd_topo_tile_t const * tile,
     154             :                           ulong                  out_cnt,
     155           0 :                           struct sock_filter *   out ) {
     156           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     157           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     158           0 :   fd_metric_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_metric_ctx_t ), sizeof( fd_metric_ctx_t ) );
     159             : 
     160           0 :   uint epoll_inner_fd = (uint)FD_WAKER_INNER_FD( tile->waker_client_idx );
     161           0 :   uint epoll_outer_fd = (uint)FD_WAKER_OUTER_FD;
     162             : 
     163           0 :   populate_sock_filter_policy_fd_metric_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)fd_http_server_fd( ctx->metrics_server ), epoll_inner_fd, epoll_outer_fd );
     164           0 :   return sock_filter_policy_fd_metric_tile_instr_cnt;
     165           0 : }
     166             : 
     167             : static ulong
     168             : populate_allowed_fds( fd_topo_t const *      topo,
     169             :                       fd_topo_tile_t const * tile,
     170             :                       ulong                  out_fds_cnt,
     171           0 :                       int *                  out_fds ) {
     172           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     173           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     174           0 :   fd_metric_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_metric_ctx_t ), sizeof( fd_metric_ctx_t ) );
     175             : 
     176           0 :   if( FD_UNLIKELY( out_fds_cnt<5UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     177             : 
     178           0 :   ulong out_cnt = 0;
     179           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     180           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     181           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     182           0 :   out_fds[ out_cnt++ ] = fd_http_server_fd( ctx->metrics_server ); /* metrics listen socket */
     183           0 :   out_fds[ out_cnt++ ] = FD_WAKER_OUTER_FD; /* waker outer epoll fd (rearm) */
     184           0 :   out_fds[ out_cnt++ ] = FD_WAKER_INNER_FD( tile->waker_client_idx ); /* waker inner epoll fd */
     185           0 :   return out_cnt;
     186           0 : }
     187             : 
     188           0 : #define STEM_BURST (1UL)
     189           0 : #define STEM_LAZY ((long)10e6) /* 10ms */
     190             : 
     191           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_metric_ctx_t
     192           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_metric_ctx_t)
     193             : 
     194           0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
     195           0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
     196             : 
     197             : #include "../stem/fd_stem.c"
     198             : 
     199             : fd_topo_run_tile_t fd_tile_metric = {
     200             :   .name                     = "metric",
     201             :   .rlimit_file_cnt          = FD_HTTP_SERVER_METRICS_MAX_CONNS+5UL, /* pipefd, socket, stderr, logfile, and one spare for new accept() connections */
     202             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     203             :   .populate_allowed_fds     = populate_allowed_fds,
     204             :   .scratch_align            = scratch_align,
     205             :   .scratch_footprint        = scratch_footprint,
     206             :   .privileged_init          = privileged_init,
     207             :   .unprivileged_init        = unprivileged_init,
     208             :   .run                      = stem_run,
     209             : };

Generated by: LCOV version 1.14