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