1 /*
2  * Copyright (c) 2016 Fastly
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 
23 #include "h2o.h"
24 #include <inttypes.h>
25 
26 struct st_events_status_ctx_t {
27     uint64_t emitted_status_errors[H2O_STATUS_ERROR_MAX];
28     uint64_t h2_protocol_level_errors[H2O_HTTP2_ERROR_MAX];
29     uint64_t h2_read_closed;
30     uint64_t h2_write_closed;
31     uint64_t h2_idle_timeout;
32     uint64_t h2_streaming_requests;
33     uint64_t h1_request_timeout;
34     uint64_t h1_request_io_timeout;
35     uint64_t ssl_errors;
36     struct {
37         uint64_t packet_forwarded;
38         uint64_t forwarded_packet_received;
39     } http3;
40     h2o_quic_stats_t quic_stats;
41     pthread_mutex_t mutex;
42 };
43 
events_status_per_thread(void * priv,h2o_context_t * ctx)44 static void events_status_per_thread(void *priv, h2o_context_t *ctx)
45 {
46     size_t i;
47     struct st_events_status_ctx_t *esc = priv;
48 
49     pthread_mutex_lock(&esc->mutex);
50 
51     for (i = 0; i < H2O_STATUS_ERROR_MAX; i++) {
52         esc->emitted_status_errors[i] += ctx->emitted_error_status[i];
53     }
54     esc->ssl_errors += ctx->ssl.errors;
55     for (i = 0; i < H2O_HTTP2_ERROR_MAX; i++) {
56         esc->h2_protocol_level_errors[i] += ctx->http2.events.protocol_level_errors[i];
57     }
58     esc->h2_read_closed += ctx->http2.events.read_closed;
59     esc->h2_write_closed += ctx->http2.events.write_closed;
60     esc->h2_idle_timeout += ctx->http2.events.idle_timeouts;
61     esc->h2_streaming_requests += ctx->http2.events.streaming_requests;
62     esc->h1_request_timeout += ctx->http1.events.request_timeouts;
63     esc->h1_request_io_timeout += ctx->http1.events.request_io_timeouts;
64     esc->http3.packet_forwarded += ctx->http3.events.packet_forwarded;
65     esc->http3.forwarded_packet_received += ctx->http3.events.forwarded_packet_received;
66     esc->quic_stats.packet_received += ctx->quic_stats.packet_received;
67     esc->quic_stats.packet_processed += ctx->quic_stats.packet_processed;
68 #define ACC(fld, _unused) esc->quic_stats.quicly.fld += ctx->quic_stats.quicly.fld;
69     H2O_QUIC_AGGREGATED_STATS_APPLY(ACC);
70 #undef ACC
71 
72     pthread_mutex_unlock(&esc->mutex);
73 }
74 
events_status_init(void)75 static void *events_status_init(void)
76 {
77     struct st_events_status_ctx_t *ret;
78 
79     ret = h2o_mem_alloc(sizeof(*ret));
80     memset(ret, 0, sizeof(*ret));
81     pthread_mutex_init(&ret->mutex, NULL);
82 
83     return ret;
84 }
85 
events_status_final(void * priv,h2o_globalconf_t * gconf,h2o_req_t * req)86 static h2o_iovec_t events_status_final(void *priv, h2o_globalconf_t *gconf, h2o_req_t *req)
87 {
88     struct st_events_status_ctx_t *esc = priv;
89     h2o_iovec_t ret;
90 
91 #define H1_AGG_ERR(status_) esc->emitted_status_errors[H2O_STATUS_ERROR_##status_]
92 #define H2_AGG_ERR(err_) esc->h2_protocol_level_errors[-H2O_HTTP2_ERROR_##err_]
93 #define QUIC_FMT(_unused, label) " \"quic." label "\": %" PRIu64 ",\n"
94 #define QUIC_VAL(fld, _unused) , esc->quic_stats.quicly.fld
95 #define BUFSIZE (8 * 1024)
96     ret.base = h2o_mem_alloc_pool(&req->pool, char, BUFSIZE);
97     /* clang-format off */
98     ret.len = snprintf(ret.base, BUFSIZE, ",\n"
99                                           " \"status-errors.400\": %" PRIu64 ",\n"
100                                           " \"status-errors.403\": %" PRIu64 ",\n"
101                                           " \"status-errors.404\": %" PRIu64 ",\n"
102                                           " \"status-errors.405\": %" PRIu64 ",\n"
103                                           " \"status-errors.416\": %" PRIu64 ",\n"
104                                           " \"status-errors.417\": %" PRIu64 ",\n"
105                                           " \"status-errors.500\": %" PRIu64 ",\n"
106                                           " \"status-errors.502\": %" PRIu64 ",\n"
107                                           " \"status-errors.503\": %" PRIu64 ",\n"
108                                           " \"http1-errors.request-timeout\": %" PRIu64 ",\n"
109                                           " \"http1-errors.request-io-timeout\": %" PRIu64 ",\n"
110                                           " \"http2-errors.protocol\": %" PRIu64 ",\n"
111                                           " \"http2-errors.internal\": %" PRIu64 ",\n"
112                                           " \"http2-errors.flow-control\": %" PRIu64 ",\n"
113                                           " \"http2-errors.settings-timeout\": %" PRIu64 ",\n"
114                                           " \"http2-errors.stream-closed\": %" PRIu64 ",\n"
115                                           " \"http2-errors.frame-size\": %" PRIu64 ",\n"
116                                           " \"http2-errors.refused-stream\": %" PRIu64 ",\n"
117                                           " \"http2-errors.cancel\": %" PRIu64 ",\n"
118                                           " \"http2-errors.compression\": %" PRIu64 ",\n"
119                                           " \"http2-errors.connect\": %" PRIu64 ",\n"
120                                           " \"http2-errors.enhance-your-calm\": %" PRIu64 ",\n"
121                                           " \"http2-errors.inadequate-security\": %" PRIu64 ",\n"
122                                           " \"http2.read-closed\": %" PRIu64 ",\n"
123                                           " \"http2.write-closed\": %" PRIu64 ",\n"
124                                           " \"http2.idle-timeout\": %" PRIu64 ",\n"
125                                           " \"http2.streaming-requests\": %" PRIu64 ",\n"
126                                           " \"http3.packet-forwarded\": %" PRIu64 ",\n"
127                                           " \"http3.forwarded-packet-received\": %" PRIu64 ",\n"
128                                           " \"quic.packet-received\": %" PRIu64 ",\n"
129                                           " \"quic.packet-processed\": %" PRIu64
130                                           ",\n" H2O_QUIC_AGGREGATED_STATS_APPLY(QUIC_FMT) " \"ssl.errors\": %" PRIu64 ",\n"
131                                                                                           " \"memory.mmap_errors\": %zu\n",
132                        H1_AGG_ERR(400), H1_AGG_ERR(403), H1_AGG_ERR(404), H1_AGG_ERR(405), H1_AGG_ERR(416), H1_AGG_ERR(417),
133                        H1_AGG_ERR(500), H1_AGG_ERR(502), H1_AGG_ERR(503), esc->h1_request_timeout, esc->h1_request_io_timeout,
134                        H2_AGG_ERR(PROTOCOL), H2_AGG_ERR(INTERNAL), H2_AGG_ERR(FLOW_CONTROL), H2_AGG_ERR(SETTINGS_TIMEOUT),
135                        H2_AGG_ERR(STREAM_CLOSED), H2_AGG_ERR(FRAME_SIZE), H2_AGG_ERR(REFUSED_STREAM), H2_AGG_ERR(CANCEL),
136                        H2_AGG_ERR(COMPRESSION), H2_AGG_ERR(CONNECT), H2_AGG_ERR(ENHANCE_YOUR_CALM), H2_AGG_ERR(INADEQUATE_SECURITY),
137                        esc->h2_read_closed, esc->h2_write_closed, esc->h2_idle_timeout, esc->h2_streaming_requests,
138                        esc->http3.packet_forwarded, esc->http3.forwarded_packet_received, esc->quic_stats.packet_received, esc->quic_stats.packet_processed
139                        H2O_QUIC_AGGREGATED_STATS_APPLY(QUIC_VAL),
140                        esc->ssl_errors, h2o_mmap_errors);
141     /* clang-format on */
142     assert(ret.len < BUFSIZE);
143 #undef H1_AGG_ERR
144 #undef H2_AGG_ERR
145 #undef QUIC_FMT
146 #undef QUIC_VAL
147 #undef BUFSIZE
148 
149     pthread_mutex_destroy(&esc->mutex);
150     free(esc);
151     return ret;
152 }
153 
154 h2o_status_handler_t h2o_events_status_handler = {
155     {H2O_STRLIT("events")}, events_status_final, events_status_init, events_status_per_thread};
156