-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from Syndica/ultd/metrics-and-thread-pool
initial prometheus metrics
- Loading branch information
Showing
12 changed files
with
1,500 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const std = @import("std"); | ||
const mem = std.mem; | ||
const testing = std.testing; | ||
|
||
const Metric = @import("metric.zig").Metric; | ||
|
||
pub const Counter = struct { | ||
const Self = @This(); | ||
|
||
metric: Metric = Metric{ .getResultFn = getResult }, | ||
value: std.atomic.Atomic(u64) = std.atomic.Atomic(u64).init(0), | ||
|
||
pub fn inc(self: *Self) void { | ||
_ = self.value.fetchAdd(1, .Monotonic); | ||
} | ||
|
||
pub fn add(self: *Self, value: anytype) void { | ||
switch (@typeInfo(@TypeOf(value))) { | ||
.Int, .Float, .ComptimeInt, .ComptimeFloat => {}, | ||
else => @compileError("can't add a non-number"), | ||
} | ||
|
||
_ = self.value.fetchAdd(@intCast(value), .Monotonic); | ||
} | ||
|
||
pub fn get(self: *const Self) u64 { | ||
return self.value.load(.Monotonic); | ||
} | ||
|
||
pub fn reset(self: *Self) void { | ||
_ = self.value.store(0, .Monotonic); | ||
} | ||
|
||
fn getResult(metric: *Metric, _: mem.Allocator) Metric.Error!Metric.Result { | ||
const self = @fieldParentPtr(Self, "metric", metric); | ||
return Metric.Result{ .counter = self.get() }; | ||
} | ||
}; | ||
|
||
test "prometheus.counter: inc/add/dec/set/get" { | ||
var buffer = std.ArrayList(u8).init(testing.allocator); | ||
defer buffer.deinit(); | ||
|
||
var counter = Counter{}; | ||
|
||
try testing.expectEqual(@as(u64, 0), counter.get()); | ||
|
||
counter.inc(); | ||
try testing.expectEqual(@as(u64, 1), counter.get()); | ||
|
||
counter.add(200); | ||
try testing.expectEqual(@as(u64, 201), counter.get()); | ||
} | ||
|
||
test "prometheus.counter: concurrent" { | ||
var counter = Counter{}; | ||
|
||
var threads: [4]std.Thread = undefined; | ||
for (&threads) |*thread| { | ||
thread.* = try std.Thread.spawn( | ||
.{}, | ||
struct { | ||
fn run(c: *Counter) void { | ||
var i: usize = 0; | ||
while (i < 20) : (i += 1) { | ||
c.inc(); | ||
} | ||
} | ||
}.run, | ||
.{&counter}, | ||
); | ||
} | ||
|
||
for (&threads) |*thread| thread.join(); | ||
|
||
try testing.expectEqual(@as(u64, 80), counter.get()); | ||
} | ||
|
||
test "prometheus.counter: write" { | ||
var counter = Counter{ .value = .{ .value = 340 } }; | ||
|
||
var buffer = std.ArrayList(u8).init(testing.allocator); | ||
defer buffer.deinit(); | ||
|
||
var metric = &counter.metric; | ||
try metric.write(testing.allocator, buffer.writer(), "mycounter"); | ||
|
||
try testing.expectEqualStrings("mycounter 340\n", buffer.items); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const std = @import("std"); | ||
|
||
const Metric = @import("metric.zig").Metric; | ||
|
||
/// A gauge that stores the value it reports. | ||
/// Read and write operations are atomic and monotonic. | ||
pub fn Gauge(comptime T: type) type { | ||
return struct { | ||
value: std.atomic.Atomic(T) = .{ .value = 0 }, | ||
metric: Metric = .{ .getResultFn = getResult }, | ||
|
||
const Self = @This(); | ||
|
||
pub fn inc(self: *Self) void { | ||
self.value.fetchAdd(1, .Monotonic); | ||
} | ||
|
||
pub fn add(self: *Self, v: T) void { | ||
self.value.fetchAdd(v, .Monotonic); | ||
} | ||
|
||
pub fn dec(self: *Self) void { | ||
self.value.fetchSub(1, .Monotonic); | ||
} | ||
|
||
pub fn sub(self: *Self, v: T) void { | ||
self.value.fetchAdd(v, .Monotonic); | ||
} | ||
|
||
pub fn set(self: *Self, v: T) void { | ||
self.value.store(v, .Monotonic); | ||
} | ||
|
||
pub fn get(self: *Self) T { | ||
return self.value.load(.Monotonic); | ||
} | ||
|
||
fn getResult(metric: *Metric, allocator: std.mem.Allocator) Metric.Error!Metric.Result { | ||
_ = allocator; | ||
|
||
const self = @fieldParentPtr(Self, "metric", metric); | ||
|
||
return switch (T) { | ||
f64 => Metric.Result{ .gauge = self.get() }, | ||
u64 => Metric.Result{ .gauge_int = self.get() }, | ||
else => unreachable, // Gauge Return may only be 'f64' or 'u64' | ||
}; | ||
} | ||
}; | ||
} |
Oops, something went wrong.