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