forked from karlseguin/http.zig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.zig
114 lines (90 loc) · 3.74 KB
/
metrics.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const m = @import("metrics");
// This is an advanced usage of metrics.zig, largely done because we aren't
// using any vectored metrics and thus can do everything at comptime.
var metrics = Metrics{
.connections = m.Counter(usize).Impl.init("httpz_connections", .{}),
.requests = m.Counter(usize).Impl.init("httpz_requests", .{}),
.timeout_active = m.Counter(usize).Impl.init("httpz_timeout_active", .{}),
.timeout_keepalive = m.Counter(usize).Impl.init("httpz_timeout_keepalive", .{}),
.alloc_buffer_empty = m.Counter(u64).Impl.init("httpz_alloc_buffer_empty", .{}),
.alloc_buffer_large = m.Counter(u64).Impl.init("httpz_alloc_buffer_large", .{}),
.alloc_unescape = m.Counter(u64).Impl.init("httpz_alloc_unescape", .{}),
.internal_error = m.Counter(usize).Impl.init("httpz_internal_error", .{}),
.invalid_request = m.Counter(usize).Impl.init("httpz_invalid_request", .{}),
.header_too_big = m.Counter(usize).Impl.init("httpz_header_too_big", .{}),
.body_too_big = m.Counter(usize).Impl.init("httpz_body_too_big", .{}),
};
const Metrics = struct {
// number of connections
connections: m.Counter(usize).Impl,
// number of requests (which can be more than # of connections thanks to
// keepalive)
requests: m.Counter(usize).Impl,
// number of connections that were timed out while service a request
timeout_active: m.Counter(usize).Impl,
// number of connections that were timed out while in keepalive
timeout_keepalive: m.Counter(usize).Impl,
// size, in bytes, of dynamically allocated memory by our buffer pool,
// caused by the large buffer pool being empty.
alloc_buffer_empty: m.Counter(u64).Impl,
// size, in bytes, of dynamically allocated memory by our buffer pool,
// caused by the required memory being larger than the large buffer size
alloc_buffer_large: m.Counter(u64).Impl,
// size, in bytes, of dynamically allocated memory used for unescaping URL
// or form parameters
alloc_unescape: m.Counter(u64).Impl,
// some internal processing error (should be 0!)
internal_error: m.Counter(usize).Impl,
// requests which could not be parsed
invalid_request: m.Counter(usize).Impl,
// requests which were rejected because the header was too big
header_too_big: m.Counter(usize).Impl,
// requests which were rejected because the body was too big
body_too_big: m.Counter(usize).Impl,
};
pub fn write(writer: anytype) !void {
try metrics.connections.write(writer);
try metrics.requests.write(writer);
try metrics.timeout_active.write(writer);
try metrics.timeout_keepalive.write(writer);
try metrics.alloc_buffer_empty.write(writer);
try metrics.alloc_buffer_large.write(writer);
try metrics.alloc_unescape.write(writer);
try metrics.internal_error.write(writer);
try metrics.invalid_request.write(writer);
try metrics.header_too_big.write(writer);
try metrics.body_too_big.write(writer);
}
pub fn allocBufferEmpty(size: u64) void {
metrics.alloc_buffer_empty.incrBy(size);
}
pub fn allocBufferLarge(size: u64) void {
metrics.alloc_buffer_large.incrBy(size);
}
pub fn allocUnescape(size: u64) void {
metrics.alloc_unescape.incrBy(size);
}
pub fn connection() void {
metrics.connections.incr();
}
pub fn request() void {
metrics.requests.incr();
}
pub fn timeoutActive(count: usize) void {
metrics.timeout_active.incrBy(count);
}
pub fn timeoutKeepalive(count: usize) void {
metrics.timeout_keepalive.incrBy(count);
}
pub fn internalError() void {
metrics.internal_error.incr();
}
pub fn invalidRequest() void {
metrics.invalid_request.incr();
}
pub fn headerTooBig() void {
metrics.header_too_big.incr();
}
pub fn bodyTooBig() void {
metrics.body_too_big.incr();
}