LCOV - code coverage report
Current view: top level - app/fdctl/run/tiles - fd_metric.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 10 84 11.9 %
Date: 2024-11-13 11:58:15 Functions: 2 8 25.0 %

          Line data    Source code
       1             : #include "../../../../disco/tiles.h"
       2             : 
       3             : #include <sys/socket.h> /* SOCK_CLOEXEC, SOCK_NONBLOCK needed for seccomp filter */
       4             : #if defined(__aarch64__)
       5             : #include "generated/metric.arm64_seccomp.h"
       6             : #else
       7             : #include "generated/metric_seccomp.h"
       8             : #endif
       9             : 
      10             : #include "../../version.h"
      11             : 
      12             : #include "../../../../disco/keyguard/fd_keyload.h"
      13             : #include "../../../../disco/metrics/fd_prometheus.h"
      14             : #include "../../../../ballet/http/fd_http_server.h"
      15             : #include "../../../../util/net/fd_ip4.h"
      16             : 
      17             : #include <errno.h>
      18             : #include <sys/types.h>
      19             : #include <unistd.h>
      20             : #include <string.h>
      21             : #include <stdio.h>
      22             : 
      23             : #define FD_HTTP_SERVER_METRICS_MAX_CONNS          128
      24             : #define FD_HTTP_SERVER_METRICS_MAX_REQUEST_LEN    1024
      25             : #define FD_HTTP_SERVER_METRICS_OUTGOING_BUFFER_SZ (32UL<<20UL) /* 32MiB reserved for buffering metrics responses */
      26             : 
      27             : const fd_http_server_params_t METRICS_PARAMS = {
      28             :   .max_connection_cnt    = FD_HTTP_SERVER_METRICS_MAX_CONNS,
      29             :   .max_ws_connection_cnt = 0UL,
      30             :   .max_request_len       = FD_HTTP_SERVER_METRICS_MAX_REQUEST_LEN,
      31             :   .max_ws_recv_frame_len = 0UL,
      32             :   .max_ws_send_frame_cnt = 0UL,
      33             :   .outgoing_buffer_sz    = FD_HTTP_SERVER_METRICS_OUTGOING_BUFFER_SZ,
      34             : };
      35             : 
      36             : typedef struct {
      37             :   fd_topo_t * topo;
      38             : 
      39             :   fd_http_server_t * metrics_server;
      40             : } fd_metric_ctx_t;
      41             : 
      42             : FD_FN_CONST static inline ulong
      43           3 : scratch_align( void ) {
      44           3 :   return 128UL;
      45           3 : }
      46             : 
      47             : FD_FN_PURE static inline ulong
      48           3 : scratch_footprint( fd_topo_tile_t const * tile ) {
      49           3 :   (void)tile;
      50             : 
      51           3 :   ulong l = FD_LAYOUT_INIT;
      52           3 :   l = FD_LAYOUT_APPEND( l, alignof( fd_metric_ctx_t ), sizeof( fd_metric_ctx_t ) );
      53           3 :   l = FD_LAYOUT_APPEND( l, fd_http_server_align(), fd_http_server_footprint( METRICS_PARAMS ) );
      54           3 :   return FD_LAYOUT_FINI( l, scratch_align() );
      55           3 : }
      56             : 
      57             : static inline void
      58             : before_credit( fd_metric_ctx_t *   ctx,
      59             :                fd_stem_context_t * stem,
      60           0 :                int *               charge_busy ) {
      61           0 :   (void)stem;
      62             : 
      63           0 :   *charge_busy = fd_http_server_poll( ctx->metrics_server );
      64           0 : }
      65             : 
      66             : static fd_http_server_response_t
      67           0 : metrics_http_request( fd_http_server_request_t const * request ) {
      68           0 :   fd_metric_ctx_t * ctx = (fd_metric_ctx_t *)request->ctx;
      69             : 
      70           0 :   if( FD_UNLIKELY( request->method!=FD_HTTP_SERVER_METHOD_GET ) ) {
      71           0 :     return (fd_http_server_response_t){
      72           0 :       .status = 400,
      73           0 :     };
      74           0 :   }
      75             : 
      76           0 :   if( FD_LIKELY( !strcmp( request->path, "/metrics" ) ) ) {
      77           0 :     fd_prometheus_format( ctx->topo, ctx->metrics_server );
      78             : 
      79           0 :     fd_http_server_response_t response = {
      80           0 :       .status       = 200,
      81           0 :       .content_type = "text/plain; version=0.0.4",
      82           0 :     };
      83           0 :     if( FD_UNLIKELY( fd_http_server_stage_body( ctx->metrics_server, &response ) ) ) {
      84           0 :       FD_LOG_WARNING(( "fd_http_server_stage_body failed, metrics response too long" ));
      85           0 :       return (fd_http_server_response_t){
      86           0 :         .status = 500,
      87           0 :       };
      88           0 :     }
      89           0 :     return response;
      90           0 :   } else {
      91           0 :     return (fd_http_server_response_t){
      92           0 :       .status = 404,
      93           0 :     };
      94           0 :   }
      95           0 : }
      96             : 
      97             : static void
      98             : privileged_init( fd_topo_t *      topo,
      99           0 :                  fd_topo_tile_t * tile ) {
     100           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     101             : 
     102           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     103           0 :   fd_metric_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_metric_ctx_t ), sizeof( fd_metric_ctx_t ) );
     104             : 
     105           0 :   fd_http_server_t * _metrics = FD_SCRATCH_ALLOC_APPEND( l, fd_http_server_align(), fd_http_server_footprint( METRICS_PARAMS ) );
     106             : 
     107           0 :   fd_http_server_callbacks_t metrics_callbacks = {
     108           0 :     .request = metrics_http_request,
     109           0 :   };
     110           0 :   ctx->metrics_server = fd_http_server_join( fd_http_server_new( _metrics, METRICS_PARAMS, metrics_callbacks, ctx ) );
     111           0 :   fd_http_server_listen( ctx->metrics_server, tile->metric.prometheus_listen_addr, tile->metric.prometheus_listen_port );
     112           0 : }
     113             : 
     114             : static void
     115             : unprivileged_init( fd_topo_t *      topo,
     116           0 :                    fd_topo_tile_t * tile ) {
     117           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     118             : 
     119           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     120           0 :   fd_metric_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_metric_ctx_t ), sizeof( fd_metric_ctx_t ) );
     121             : 
     122           0 :   ctx->topo = topo;
     123             : 
     124           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     125           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     126           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     127             : 
     128           0 :   FD_LOG_WARNING(( "Prometheus metrics endpoint listening at http://" FD_IP4_ADDR_FMT ":%u/metrics", FD_IP4_ADDR_FMT_ARGS( tile->metric.prometheus_listen_addr ), tile->metric.prometheus_listen_port ));
     129           0 : }
     130             : 
     131             : static ulong
     132             : populate_allowed_seccomp( fd_topo_t const *      topo,
     133             :                           fd_topo_tile_t const * tile,
     134             :                           ulong                  out_cnt,
     135           0 :                           struct sock_filter *   out ) {
     136           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     137           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     138           0 :   fd_metric_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_metric_ctx_t ), sizeof( fd_metric_ctx_t ) );
     139             : 
     140           0 :   populate_sock_filter_policy_metric( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)fd_http_server_fd( ctx->metrics_server ) );
     141           0 :   return sock_filter_policy_metric_instr_cnt;
     142           0 : }
     143             : 
     144             : static ulong
     145             : populate_allowed_fds( fd_topo_t const *      topo,
     146             :                       fd_topo_tile_t const * tile,
     147             :                       ulong                  out_fds_cnt,
     148           0 :                       int *                  out_fds ) {
     149           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     150           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     151           0 :   fd_metric_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_metric_ctx_t ), sizeof( fd_metric_ctx_t ) );
     152             : 
     153           0 :   if( FD_UNLIKELY( out_fds_cnt<3UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     154             : 
     155           0 :   ulong out_cnt = 0;
     156           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     157           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     158           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     159           0 :   out_fds[ out_cnt++ ] = fd_http_server_fd( ctx->metrics_server ); /* metrics listen socket */
     160           0 :   return out_cnt;
     161           0 : }
     162             : 
     163           0 : #define STEM_BURST (1UL)
     164             : 
     165           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_metric_ctx_t
     166           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_metric_ctx_t)
     167             : 
     168           0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
     169             : 
     170             : #include "../../../../disco/stem/fd_stem.c"
     171             : 
     172             : fd_topo_run_tile_t fd_tile_metric = {
     173             :   .name                     = "metric",
     174             :   .rlimit_file_cnt          = FD_HTTP_SERVER_METRICS_MAX_CONNS+5UL, /* pipefd, socket, stderr, logfile, and one spare for new accept() connections */
     175             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     176             :   .populate_allowed_fds     = populate_allowed_fds,
     177             :   .scratch_align            = scratch_align,
     178             :   .scratch_footprint        = scratch_footprint,
     179             :   .privileged_init          = privileged_init,
     180             :   .unprivileged_init        = unprivileged_init,
     181             :   .run                      = stem_run,
     182             : };

Generated by: LCOV version 1.14