-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.zig
86 lines (69 loc) · 2.6 KB
/
main.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
const std = @import("std");
const prometheus = @import("prometheus");
fn getRandomString(allocator: std.mem.Allocator, random: std.Random, n: usize) ![]const u8 {
const alphabet = "abcdefghijklmnopqrstuvwxyz";
const items = try allocator.alloc(u8, n);
for (items) |*item| {
const random_pos = random.intRangeLessThan(usize, 0, alphabet.len);
item.* = alphabet[random_pos];
}
return items;
}
pub fn main() anyerror!void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const allocator = arena.allocator();
var prng = std.Random.DefaultPrng.init(@bitCast(std.time.milliTimestamp()));
const random = prng.random();
// Initialize a registry
var registry = try prometheus.Registry(.{}).create(allocator);
defer registry.destroy();
// Get some counters
{
var i: usize = 0;
while (i < 5) : (i += 1) {
const name = try std.fmt.allocPrint(allocator, "http_requests_total{{route=\"/{s}\"}}", .{
try getRandomString(allocator, random, 20),
});
var counter = try registry.getOrCreateCounter(name);
counter.add(random.intRangeAtMost(u64, 0, 450000));
}
}
// Get some gauges sharing the same state.
{
const State = struct {
random: std.Random,
};
var state = State{ .random = random };
var i: usize = 0;
while (i < 5) : (i += 1) {
const name = try std.fmt.allocPrint(allocator, "http_conn_pool_size{{name=\"{s}\"}}", .{
try getRandomString(allocator, random, 5),
});
_ = try registry.getOrCreateGauge(
name,
&state,
struct {
fn get(s: *State) f64 {
const n = s.random.intRangeAtMost(usize, 0, 2000);
const f = s.random.float(f64);
return f * @as(f64, @floatFromInt(n));
}
}.get,
);
}
}
// Get a histogram
{
const name = try std.fmt.allocPrint(allocator, "http_requests_latency{{route=\"/{s}\"}}", .{
try getRandomString(allocator, random, 20),
});
var histogram = try registry.getOrCreateHistogram(name);
var i: usize = 0;
while (i < 200) : (i += 1) {
const duration = random.intRangeAtMost(usize, 0, 10000);
histogram.update(@floatFromInt(duration));
}
}
// Finally serialize the metrics to stdout
try registry.write(allocator, std.io.getStdOut().writer());
}