Coverage Report

Created: 2025-07-12 06:34

/src/h2o/lib/handler/status/events.c
Line
Count
Source (jump to first uncovered line)
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
    struct {
41
        uint64_t idle_closed;
42
        uint64_t num_idle;
43
        uint64_t num_active;
44
        uint64_t num_shutdown;
45
    } connection_stats;
46
    h2o_quic_stats_t quic_stats;
47
    pthread_mutex_t mutex;
48
};
49
50
static void events_status_per_thread(void *priv, h2o_context_t *ctx)
51
0
{
52
0
    size_t i;
53
0
    struct st_events_status_ctx_t *esc = priv;
54
55
0
    pthread_mutex_lock(&esc->mutex);
56
57
0
    for (i = 0; i < H2O_STATUS_ERROR_MAX; i++) {
58
0
        esc->emitted_status_errors[i] += ctx->emitted_error_status[i];
59
0
    }
60
0
    esc->ssl_errors += ctx->ssl.errors;
61
0
    for (i = 0; i < H2O_HTTP2_ERROR_MAX; i++) {
62
0
        esc->h2_protocol_level_errors[i] += ctx->http2.events.protocol_level_errors[i];
63
0
    }
64
0
    esc->h2_read_closed += ctx->http2.events.read_closed;
65
0
    esc->h2_write_closed += ctx->http2.events.write_closed;
66
0
    esc->h2_idle_timeout += ctx->http2.events.idle_timeouts;
67
0
    esc->h2_streaming_requests += ctx->http2.events.streaming_requests;
68
0
    esc->h1_request_timeout += ctx->http1.events.request_timeouts;
69
0
    esc->h1_request_io_timeout += ctx->http1.events.request_io_timeouts;
70
0
    esc->http3.packet_forwarded += ctx->http3.events.packet_forwarded;
71
0
    esc->http3.forwarded_packet_received += ctx->http3.events.forwarded_packet_received;
72
0
    esc->connection_stats.idle_closed += ctx->connection_stats.idle_closed;
73
0
    esc->connection_stats.num_idle += ctx->_conns.num_conns.idle;
74
0
    esc->connection_stats.num_active += ctx->_conns.num_conns.active;
75
0
    esc->connection_stats.num_shutdown += ctx->_conns.num_conns.shutdown;
76
0
    esc->quic_stats.packet_received += ctx->quic_stats.packet_received;
77
0
    esc->quic_stats.packet_processed += ctx->quic_stats.packet_processed;
78
0
    if (esc->quic_stats.num_sentmap_packets_largest < ctx->quic_stats.num_sentmap_packets_largest)
79
0
        esc->quic_stats.num_sentmap_packets_largest = ctx->quic_stats.num_sentmap_packets_largest;
80
0
#define ACC(fld, _unused) esc->quic_stats.quicly.fld += ctx->quic_stats.quicly.fld;
81
0
    H2O_QUIC_AGGREGATED_STATS_APPLY(ACC);
82
0
#undef ACC
83
84
0
    pthread_mutex_unlock(&esc->mutex);
85
0
}
86
87
static void *events_status_init(void)
88
0
{
89
0
    struct st_events_status_ctx_t *ret;
90
91
0
    ret = h2o_mem_alloc(sizeof(*ret));
92
0
    memset(ret, 0, sizeof(*ret));
93
0
    pthread_mutex_init(&ret->mutex, NULL);
94
95
0
    return ret;
96
0
}
97
98
static h2o_iovec_t events_status_final(void *priv, h2o_globalconf_t *gconf, h2o_req_t *req)
99
0
{
100
0
    struct st_events_status_ctx_t *esc = priv;
101
0
    h2o_iovec_t ret;
102
103
0
#define H1_AGG_ERR(status_) esc->emitted_status_errors[H2O_STATUS_ERROR_##status_]
104
0
#define H2_AGG_ERR(err_) esc->h2_protocol_level_errors[-H2O_HTTP2_ERROR_##err_]
105
0
#define QUIC_FMT(_unused, label) " \"quic." label "\": %" PRIu64 ",\n"
106
0
#define QUIC_VAL(fld, _unused) , esc->quic_stats.quicly.fld
107
0
#define BUFSIZE (8 * 1024)
108
0
    ret.base = h2o_mem_alloc_pool(&req->pool, char, BUFSIZE);
109
    /* clang-format off */
110
0
    ret.len = snprintf(ret.base, BUFSIZE, ",\n"
111
0
                                          " \"status-errors.400\": %" PRIu64 ",\n"
112
0
                                          " \"status-errors.401\": %" PRIu64 ",\n"
113
0
                                          " \"status-errors.403\": %" PRIu64 ",\n"
114
0
                                          " \"status-errors.404\": %" PRIu64 ",\n"
115
0
                                          " \"status-errors.405\": %" PRIu64 ",\n"
116
0
                                          " \"status-errors.416\": %" PRIu64 ",\n"
117
0
                                          " \"status-errors.417\": %" PRIu64 ",\n"
118
0
                                          " \"status-errors.421\": %" PRIu64 ",\n"
119
0
                                          " \"status-errors.500\": %" PRIu64 ",\n"
120
0
                                          " \"status-errors.502\": %" PRIu64 ",\n"
121
0
                                          " \"status-errors.503\": %" PRIu64 ",\n"
122
0
                                          " \"http1-errors.request-timeout\": %" PRIu64 ",\n"
123
0
                                          " \"http1-errors.request-io-timeout\": %" PRIu64 ",\n"
124
0
                                          " \"http2-errors.protocol\": %" PRIu64 ",\n"
125
0
                                          " \"http2-errors.internal\": %" PRIu64 ",\n"
126
0
                                          " \"http2-errors.flow-control\": %" PRIu64 ",\n"
127
0
                                          " \"http2-errors.settings-timeout\": %" PRIu64 ",\n"
128
0
                                          " \"http2-errors.stream-closed\": %" PRIu64 ",\n"
129
0
                                          " \"http2-errors.frame-size\": %" PRIu64 ",\n"
130
0
                                          " \"http2-errors.refused-stream\": %" PRIu64 ",\n"
131
0
                                          " \"http2-errors.cancel\": %" PRIu64 ",\n"
132
0
                                          " \"http2-errors.compression\": %" PRIu64 ",\n"
133
0
                                          " \"http2-errors.connect\": %" PRIu64 ",\n"
134
0
                                          " \"http2-errors.enhance-your-calm\": %" PRIu64 ",\n"
135
0
                                          " \"http2-errors.inadequate-security\": %" PRIu64 ",\n"
136
0
                                          " \"http2.read-closed\": %" PRIu64 ",\n"
137
0
                                          " \"http2.write-closed\": %" PRIu64 ",\n"
138
0
                                          " \"http2.idle-timeout\": %" PRIu64 ",\n"
139
0
                                          " \"http2.streaming-requests\": %" PRIu64 ",\n"
140
0
                                          " \"http3.packet-forwarded\": %" PRIu64 ",\n"
141
0
                                          " \"http3.forwarded-packet-received\": %" PRIu64 ",\n"
142
0
                                          " \"connections.idle-closed\": %" PRIu64 ",\n"
143
0
                                          " \"connections.idle\": %" PRIu64 ",\n"
144
0
                                          " \"connections.active\": %" PRIu64 ",\n"
145
0
                                          " \"connections.shutdown\": %" PRIu64 ",\n"
146
0
                                          " \"quic.packet-received\": %" PRIu64 ",\n"
147
0
                                          " \"quic.packet-processed\": %" PRIu64 ",\n"
148
0
                                          " \"quic.num-sentmap-packets-largest\": %zu"
149
0
                                          ",\n" H2O_QUIC_AGGREGATED_STATS_APPLY(QUIC_FMT)
150
0
                                          " \"ssl.errors\": %" PRIu64 ",\n"
151
0
                                          " \"memory.mmap_errors\": %zu,\n"
152
0
                                          " \"h2olog.lost\": %zu\n",
153
0
                       H1_AGG_ERR(400), H1_AGG_ERR(401), H1_AGG_ERR(403), H1_AGG_ERR(404), H1_AGG_ERR(405), H1_AGG_ERR(416),
154
0
                       H1_AGG_ERR(417), H1_AGG_ERR(421), H1_AGG_ERR(500), H1_AGG_ERR(502), H1_AGG_ERR(503),
155
0
                       esc->h1_request_timeout, esc->h1_request_io_timeout,
156
0
                       H2_AGG_ERR(PROTOCOL), H2_AGG_ERR(INTERNAL), H2_AGG_ERR(FLOW_CONTROL), H2_AGG_ERR(SETTINGS_TIMEOUT),
157
0
                       H2_AGG_ERR(STREAM_CLOSED), H2_AGG_ERR(FRAME_SIZE), H2_AGG_ERR(REFUSED_STREAM), H2_AGG_ERR(CANCEL),
158
0
                       H2_AGG_ERR(COMPRESSION), H2_AGG_ERR(CONNECT), H2_AGG_ERR(ENHANCE_YOUR_CALM), H2_AGG_ERR(INADEQUATE_SECURITY),
159
0
                       esc->h2_read_closed, esc->h2_write_closed, esc->h2_idle_timeout, esc->h2_streaming_requests,
160
0
                       esc->http3.packet_forwarded, esc->http3.forwarded_packet_received,
161
0
                       esc->connection_stats.idle_closed, esc->connection_stats.num_idle, esc->connection_stats.num_active,
162
0
                       esc->connection_stats.num_shutdown, esc->quic_stats.packet_received, esc->quic_stats.packet_processed,
163
0
                       esc->quic_stats.num_sentmap_packets_largest
164
0
                       H2O_QUIC_AGGREGATED_STATS_APPLY(QUIC_VAL),
165
0
                       esc->ssl_errors, h2o_mmap_errors, ptls_log_num_lost());
166
    /* clang-format on */
167
0
    assert(ret.len < BUFSIZE);
168
0
#undef H1_AGG_ERR
169
0
#undef H2_AGG_ERR
170
0
#undef QUIC_FMT
171
0
#undef QUIC_VAL
172
0
#undef BUFSIZE
173
174
0
    pthread_mutex_destroy(&esc->mutex);
175
0
    free(esc);
176
0
    return ret;
177
0
}
178
179
h2o_status_handler_t h2o_events_status_handler = {
180
    {H2O_STRLIT("events")}, events_status_final, events_status_init, events_status_per_thread};