Skip to content

Commit

Permalink
feat: Upgrade to Zig 0.13.0
Browse files Browse the repository at this point in the history
Signed-off-by: Naoki Ikeguchi <[email protected]>
  • Loading branch information
siketyan committed Jan 2, 2025
1 parent 465ca75 commit 2d7c157
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 65 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- uses: actions/checkout@v4
- uses: goto-bus-stop/setup-zig@v2
with:
version: master
version: 0.13.0
- run: zig fmt --check *.zig src/*.zig

build:
Expand All @@ -31,7 +31,7 @@ jobs:
submodules: true
- uses: goto-bus-stop/setup-zig@v2
with:
version: master
version: 0.13.0

- name: Build examples
run: zig build run-example-basic
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
zig-*
.zig-cache
.zigmod
deps.zig
12 changes: 6 additions & 6 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
const std = @import("std");

pub fn build(b: *std.build.Builder) void {
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const lib = b.addStaticLibrary(.{
.name = "zig-prometheus",
.root_source_file = .{ .path = "src/main.zig" },
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
b.installArtifact(lib);
const main_tests = b.addTest(.{
.name = "main",
.root_source_file = .{ .path = "src/main.zig" },
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const run_main_tests = b.addRunArtifact(main_tests);

const module = b.addModule("prometheus", .{
.source_file = .{ .path = "src/main.zig" },
.root_source_file = b.path("src/main.zig"),
});

const examples = &[_][]const u8{
Expand All @@ -30,11 +30,11 @@ pub fn build(b: *std.build.Builder) void {
inline for (examples) |name| {
var exe = b.addExecutable(.{
.name = "example-" ++ name,
.root_source_file = .{ .path = "examples/" ++ name ++ "/main.zig" },
.root_source_file = b.path("examples/" ++ name ++ "/main.zig"),
.target = target,
.optimize = optimize,
});
exe.addModule("prometheus", module);
exe.root_module.addImport("prometheus", module);
b.installArtifact(exe);

const run_cmd = b.addRunArtifact(exe);
Expand Down
1 change: 1 addition & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.{
.name = "prometheus",
.version = "0.0.1",
.paths = .{""},
}
6 changes: 3 additions & 3 deletions examples/basic/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const std = @import("std");

const prometheus = @import("prometheus");

fn getRandomString(allocator: std.mem.Allocator, random: std.rand.Random, n: usize) ![]const u8 {
fn getRandomString(allocator: std.mem.Allocator, random: std.Random, n: usize) ![]const u8 {
const alphabet = "abcdefghijklmnopqrstuvwxyz";

const items = try allocator.alloc(u8, n);
Expand All @@ -18,7 +18,7 @@ pub fn main() anyerror!void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const allocator = arena.allocator();

var prng = std.rand.DefaultPrng.init(@bitCast(std.time.milliTimestamp()));
var prng = std.Random.DefaultPrng.init(@bitCast(std.time.milliTimestamp()));
const random = prng.random();

// Initialize a registry
Expand All @@ -41,7 +41,7 @@ pub fn main() anyerror!void {
// Get some gauges sharing the same state.
{
const State = struct {
random: std.rand.Random,
random: std.Random,
};
var state = State{ .random = random };

Expand Down
12 changes: 6 additions & 6 deletions src/Counter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ pub fn init(allocator: mem.Allocator) !*Self {
}

pub fn inc(self: *Self) void {
_ = self.value.fetchAdd(1, .SeqCst);
_ = self.value.fetchAdd(1, .seq_cst);
}

pub fn dec(self: *Self) void {
_ = self.value.fetchSub(1, .SeqCst);
_ = self.value.fetchSub(1, .seq_cst);
}

pub fn add(self: *Self, value: anytype) void {
Expand All @@ -33,11 +33,11 @@ pub fn add(self: *Self, value: anytype) void {
else => @compileError("can't add a non-number"),
}

_ = self.value.fetchAdd(@intCast(value), .SeqCst);
_ = self.value.fetchAdd(@intCast(value), .seq_cst);
}

pub fn get(self: *const Self) u64 {
return self.value.load(.SeqCst);
return self.value.load(.seq_cst);
}

pub fn set(self: *Self, value: anytype) void {
Expand All @@ -46,13 +46,13 @@ pub fn set(self: *Self, value: anytype) void {
else => @compileError("can't set a non-number"),
}

_ = self.value.store(@intCast(value), .SeqCst);
_ = self.value.store(@intCast(value), .seq_cst);
}

fn getResult(metric: *Metric, allocator: mem.Allocator) Metric.Error!Metric.Result {
_ = allocator;

const self = @fieldParentPtr(Self, "metric", metric);
const self: *Self = @fieldParentPtr("metric", metric);

return Metric.Result{ .counter = self.get() };
}
Expand Down
2 changes: 1 addition & 1 deletion src/Gauge.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn Gauge(comptime StateType: type, comptime Return: type) type {
fn getResult(metric: *Metric, allocator: mem.Allocator) Metric.Error!Metric.Result {
_ = allocator;

const self = @fieldParentPtr(Self, "metric", metric);
const self: *Self = @fieldParentPtr("metric", metric);

return switch (Return) {
f64 => Metric.Result{ .gauge = self.get() },
Expand Down
64 changes: 29 additions & 35 deletions src/Histogram.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ const buckets_per_decimal = 18;
const decimal_buckets_count = e10_max - e10_min;
const buckets_count = decimal_buckets_count * buckets_per_decimal;

const lower_bucket_range = blk: {
var buf: [64]u8 = undefined;
break :blk fmt.bufPrint(&buf, "0...{e:.3}", .{math.pow(f64, 10, e10_min)}) catch unreachable;
};
const upper_bucket_range = blk: {
var buf: [64]u8 = undefined;
break :blk fmt.bufPrint(&buf, "{e:.3}...+Inf", .{math.pow(f64, 10, e10_max)}) catch unreachable;
};
const lower_bucket_range = fmt.comptimePrint("0...{e:.3}", .{math.pow(f64, 10, e10_min)});
const upper_bucket_range = fmt.comptimePrint("{e:.3}...+Inf", .{math.pow(f64, 10, e10_max)});

const bucket_ranges: [buckets_count][]const u8 = blk: {
const bucket_multiplier = math.pow(f64, 10.0, 1.0 / @as(f64, buckets_per_decimal));
Expand Down Expand Up @@ -50,16 +44,16 @@ const bucket_ranges: [buckets_count][]const u8 = blk: {
};

test "bucket ranges" {
try testing.expectEqualStrings("0...1.000e-09", lower_bucket_range);
try testing.expectEqualStrings("1.000e+18...+Inf", upper_bucket_range);

try testing.expectEqualStrings("1.000e-09...1.136e-09", bucket_ranges[0]);
try testing.expectEqualStrings("1.136e-09...1.292e-09", bucket_ranges[1]);
try testing.expectEqualStrings("8.799e-09...1.000e-08", bucket_ranges[buckets_per_decimal - 1]);
try testing.expectEqualStrings("1.000e-08...1.136e-08", bucket_ranges[buckets_per_decimal]);
try testing.expectEqualStrings("8.799e-01...1.000e+00", bucket_ranges[buckets_per_decimal * (-e10_min) - 1]);
try testing.expectEqualStrings("1.000e+00...1.136e+00", bucket_ranges[buckets_per_decimal * (-e10_min)]);
try testing.expectEqualStrings("8.799e+17...1.000e+18", bucket_ranges[buckets_per_decimal * (e10_max - e10_min) - 1]);
try testing.expectEqualStrings("0...1.000e-9", lower_bucket_range);
try testing.expectEqualStrings("1.000e18...+Inf", upper_bucket_range);

try testing.expectEqualStrings("1.000e-9...1.136e-9", bucket_ranges[0]);
try testing.expectEqualStrings("1.136e-9...1.292e-9", bucket_ranges[1]);
try testing.expectEqualStrings("8.799e-9...1.000e-8", bucket_ranges[buckets_per_decimal - 1]);
try testing.expectEqualStrings("1.000e-8...1.136e-8", bucket_ranges[buckets_per_decimal]);
try testing.expectEqualStrings("8.799e-1...1.000e0", bucket_ranges[buckets_per_decimal * (-e10_min) - 1]);
try testing.expectEqualStrings("1.000e0...1.136e0", bucket_ranges[buckets_per_decimal * (-e10_min)]);
try testing.expectEqualStrings("8.799e17...1.000e18", bucket_ranges[buckets_per_decimal * (e10_max - e10_min) - 1]);
}

/// Histogram based on https://github.com/VictoriaMetrics/metrics/blob/master/histogram.go.
Expand Down Expand Up @@ -140,7 +134,7 @@ pub const Histogram = struct {
}

fn getResult(metric: *Metric, allocator: mem.Allocator) Metric.Error!Metric.Result {
const self = @fieldParentPtr(Histogram, "metric", metric);
const self: *Histogram = @fieldParentPtr("metric", metric);

// Arbitrary maximum capacity
var buckets = try std.ArrayList(HistogramResult.Bucket).initCapacity(allocator, 16);
Expand Down Expand Up @@ -222,14 +216,14 @@ test "update then write" {
try metric.write(testing.allocator, buffer.writer(), "myhistogram");

const exp =
\\myhistogram_bucket{vmrange="8.799e+01...1.000e+02"} 3
\\myhistogram_bucket{vmrange="1.000e+02...1.136e+02"} 13
\\myhistogram_bucket{vmrange="1.136e+02...1.292e+02"} 16
\\myhistogram_bucket{vmrange="1.292e+02...1.468e+02"} 17
\\myhistogram_bucket{vmrange="1.468e+02...1.668e+02"} 20
\\myhistogram_bucket{vmrange="1.668e+02...1.896e+02"} 23
\\myhistogram_bucket{vmrange="1.896e+02...2.154e+02"} 26
\\myhistogram_bucket{vmrange="2.154e+02...2.448e+02"} 2
\\myhistogram_bucket{vmrange="8.799e1...1.000e2"} 3
\\myhistogram_bucket{vmrange="1.000e2...1.136e2"} 13
\\myhistogram_bucket{vmrange="1.136e2...1.292e2"} 16
\\myhistogram_bucket{vmrange="1.292e2...1.468e2"} 17
\\myhistogram_bucket{vmrange="1.468e2...1.668e2"} 20
\\myhistogram_bucket{vmrange="1.668e2...1.896e2"} 23
\\myhistogram_bucket{vmrange="1.896e2...2.154e2"} 26
\\myhistogram_bucket{vmrange="2.154e2...2.448e2"} 2
\\myhistogram_sum 18900
\\myhistogram_count 120
\\
Expand All @@ -254,14 +248,14 @@ test "update then write with labels" {
try metric.write(testing.allocator, buffer.writer(), "myhistogram{route=\"/api/v2/users\"}");

const exp =
\\myhistogram_bucket{route="/api/v2/users",vmrange="8.799e+01...1.000e+02"} 3
\\myhistogram_bucket{route="/api/v2/users",vmrange="1.000e+02...1.136e+02"} 13
\\myhistogram_bucket{route="/api/v2/users",vmrange="1.136e+02...1.292e+02"} 16
\\myhistogram_bucket{route="/api/v2/users",vmrange="1.292e+02...1.468e+02"} 17
\\myhistogram_bucket{route="/api/v2/users",vmrange="1.468e+02...1.668e+02"} 20
\\myhistogram_bucket{route="/api/v2/users",vmrange="1.668e+02...1.896e+02"} 23
\\myhistogram_bucket{route="/api/v2/users",vmrange="1.896e+02...2.154e+02"} 26
\\myhistogram_bucket{route="/api/v2/users",vmrange="2.154e+02...2.448e+02"} 2
\\myhistogram_bucket{route="/api/v2/users",vmrange="8.799e1...1.000e2"} 3
\\myhistogram_bucket{route="/api/v2/users",vmrange="1.000e2...1.136e2"} 13
\\myhistogram_bucket{route="/api/v2/users",vmrange="1.136e2...1.292e2"} 16
\\myhistogram_bucket{route="/api/v2/users",vmrange="1.292e2...1.468e2"} 17
\\myhistogram_bucket{route="/api/v2/users",vmrange="1.468e2...1.668e2"} 20
\\myhistogram_bucket{route="/api/v2/users",vmrange="1.668e2...1.896e2"} 23
\\myhistogram_bucket{route="/api/v2/users",vmrange="1.896e2...2.154e2"} 26
\\myhistogram_bucket{route="/api/v2/users",vmrange="2.154e2...2.448e2"} 2
\\myhistogram_sum{route="/api/v2/users"} 18900
\\myhistogram_count{route="/api/v2/users"} 120
\\
Expand Down
20 changes: 10 additions & 10 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn Registry(comptime options: RegistryOptions) type {
gop.value_ptr.* = &real_metric.metric;
}

return @fieldParentPtr(Counter, "metric", gop.value_ptr.*);
return @fieldParentPtr("metric", gop.value_ptr.*);
}

pub fn getOrCreateHistogram(self: *Self, name: []const u8) GetMetricError!*Histogram {
Expand All @@ -97,7 +97,7 @@ pub fn Registry(comptime options: RegistryOptions) type {
gop.value_ptr.* = &real_metric.metric;
}

return @fieldParentPtr(Histogram, "metric", gop.value_ptr.*);
return @fieldParentPtr("metric", gop.value_ptr.*);
}

pub fn getOrCreateGauge(
Expand All @@ -122,7 +122,7 @@ pub fn Registry(comptime options: RegistryOptions) type {
gop.value_ptr.* = &real_metric.metric;
}

return @fieldParentPtr(Gauge(@TypeOf(state), f64), "metric", gop.value_ptr.*);
return @fieldParentPtr("metric", gop.value_ptr.*);
}

pub fn getOrCreateGaugeInt(
Expand All @@ -147,7 +147,7 @@ pub fn Registry(comptime options: RegistryOptions) type {
gop.value_ptr.* = &real_metric.metric;
}

return @fieldParentPtr(Gauge(@TypeOf(state), u64), "metric", gop.value_ptr.*);
return @fieldParentPtr("metric", gop.value_ptr.*);
}

pub fn write(self: *Self, allocator: mem.Allocator, writer: anytype) !void {
Expand Down Expand Up @@ -217,9 +217,9 @@ test "registry write" {

const exp1 =
\\http_conn_pool_size 4.000000
\\http_request_size_bucket{vmrange="1.292e+02...1.468e+02"} 1
\\http_request_size_bucket{vmrange="4.642e+02...5.275e+02"} 1
\\http_request_size_bucket{vmrange="1.136e+03...1.292e+03"} 1
\\http_request_size_bucket{vmrange="1.292e2...1.468e2"} 1
\\http_request_size_bucket{vmrange="4.642e2...5.275e2"} 1
\\http_request_size_bucket{vmrange="1.136e3...1.292e3"} 1
\\http_request_size_sum 1870.360000
\\http_request_size_count 3
\\http_requests 2
Expand All @@ -228,9 +228,9 @@ test "registry write" {

const exp2 =
\\http_conn_pool_size{route="/api/v2/users"} 4.000000
\\http_request_size_bucket{route="/api/v2/users",vmrange="1.292e+02...1.468e+02"} 1
\\http_request_size_bucket{route="/api/v2/users",vmrange="4.642e+02...5.275e+02"} 1
\\http_request_size_bucket{route="/api/v2/users",vmrange="1.136e+03...1.292e+03"} 1
\\http_request_size_bucket{route="/api/v2/users",vmrange="1.292e2...1.468e2"} 1
\\http_request_size_bucket{route="/api/v2/users",vmrange="4.642e2...5.275e2"} 1
\\http_request_size_bucket{route="/api/v2/users",vmrange="1.136e3...1.292e3"} 1
\\http_request_size_sum{route="/api/v2/users"} 1870.360000
\\http_request_size_count{route="/api/v2/users"} 3
\\http_requests{route="/api/v2/users"} 2
Expand Down
11 changes: 9 additions & 2 deletions src/metric.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ pub const HistogramResult = struct {
if (@as(f64, @floatFromInt(as_int)) == self.value) {
try fmt.formatInt(as_int, 10, .lower, options, writer);
} else {
try fmt.formatFloatDecimal(self.value, options, writer);
// this buffer should be enough to display all decimal places of a decimal f64 number.
var buf: [512]u8 = undefined;
// FIXME: Use the writer directly once the formatFloat API accepts a writer
const formatted = try fmt.formatFloat(&buf, self.value, .{
.mode = .decimal,
.precision = options.precision,
});
try writer.writeAll(formatted);
}
}
};
Expand All @@ -30,7 +37,7 @@ pub const HistogramResult = struct {
};

pub const Metric = struct {
pub const Error = error{OutOfMemory} || std.os.WriteError || std.http.Server.Response.Writer.Error;
pub const Error = error{OutOfMemory} || std.posix.WriteError || std.http.Server.Response.WriteError;

pub const Result = union(enum) {
const Self = @This();
Expand Down

0 comments on commit 2d7c157

Please sign in to comment.