authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-06-04 20:04:25 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-06-04 20:04:25 -07:00
log04c3ad0fbdfb57dea27da0c8cbb75e2539837c92
tree831c8670abfc4529b3eb672bde2a92610b65eec0
parentafa0f6df50960f3772b4e0b311ff86521da3e6d5
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

update to zig 0.16.0


4 files changed, 43 insertions(+), 34 deletions(-)

README.md+1-1
......@@ -3,7 +3,7 @@
33![loc](https://sloc.xyz/github/nektro/zig-tracer)
44[![license](https://img.shields.io/github/license/nektro/zig-tracer.svg)](https://github.com/nektro/zig-tracer/blob/master/LICENSE)
55[![nektro @ github sponsors](https://img.shields.io/badge/sponsors-nektro-purple?logo=github)](https://github.com/sponsors/nektro)
6[![Zig](https://img.shields.io/badge/Zig-0.15-f7a41d)](https://ziglang.org/)
6[![Zig](https://img.shields.io/badge/Zig-0.16-f7a41d)](https://ziglang.org/)
77[![Zigmod](https://img.shields.io/badge/Zigmod-latest-f7a41d)](https://github.com/nektro/zigmod)
88
99Generic tracing library for Zig, supports multiple backends.
build.zig+1-1
......@@ -33,7 +33,7 @@ fn addTest(b: *std.Build, target: std.Build.ResolvedTarget, mode: std.builtin.Op
3333 }),
3434 });
3535 deps.addAllTo(exe);
36 exe.linkLibC();
36 exe.root_module.link_libc = true;
3737 exe.root_module.addImport("build_options", options.createModule());
3838 exe.use_llvm = !disable_llvm;
3939 exe.use_lld = !disable_llvm;
src/main.zig+8
......@@ -8,7 +8,15 @@ pub const tracer_backend: tracer.Backend = @enumFromInt(build_options.backend);
88pub const otel_service_name = "zig-tracer test";
99pub const otel_service_version = build_options.version;
1010
11pub var io: std.Io = undefined;
12
1113pub fn main() !void {
14 const allocator = std.heap.c_allocator;
15
16 var threaded: std.Io.Threaded = .init(allocator, .{});
17 defer threaded.deinit();
18 io = threaded.io();
19
1220 try tracer.init(.{});
1321 defer tracer.deinit();
1422
src/otel.zig+33-32
......@@ -68,7 +68,7 @@ pub threadlocal var @"url.query": ?[]const u8 = null;
6868
6969pub fn init_thread(args: struct { std.mem.Allocator, std.Uri }) !void {
7070 allocator, traces_endpoint = args;
71 trace_id = extras.randomBytes(16);
71 trace_id = nio.randomBytes(16);
7272 prev_span_id = null;
7373}
7474
......@@ -77,31 +77,31 @@ pub fn deinit_thread() void {
7777 spans.clearAndFree(allocator);
7878}
7979fn deinit_thread_inner() !void {
80 var instrumentation_scope: std.ArrayListUnmanaged(u8) = .empty;
81 defer instrumentation_scope.deinit(allocator);
80 var instrumentation_scope: nio.AllocatingWriter = .init(allocator);
81 defer instrumentation_scope.deinit();
8282 {
83 const w = instrumentation_scope.writer(allocator);
83 const w = &instrumentation_scope;
8484 try writef_string(w, 1, "github.com/nektro/zig-tracer");
8585 try writef_string(w, 2, "(hash)");
8686 try writef_len(w, 3, 0);
8787 try writef_varint(w, 4, 0);
8888 }
89 var scope_spans: std.ArrayListUnmanaged(u8) = .empty;
90 defer scope_spans.deinit(allocator);
89 var scope_spans: nio.AllocatingWriter = .init(allocator);
90 defer scope_spans.deinit();
9191 {
92 const w = scope_spans.writer(allocator);
92 const w = &scope_spans;
9393 try writef_len(w, 1, instrumentation_scope.items.len);
9494 try w.writeAll(instrumentation_scope.items);
95 instrumentation_scope.clearAndFree(allocator);
95 instrumentation_scope.clearAndFree();
9696 for (spans.items) |sp| {
9797 try writef_len(w, 2, sp.len);
9898 try w.writeAll(sp);
9999 }
100100 }
101 var resource: std.ArrayListUnmanaged(u8) = .empty;
102 defer resource.deinit(allocator);
101 var resource: nio.AllocatingWriter = .init(allocator);
102 defer resource.deinit();
103103 {
104 const w = resource.writer(allocator);
104 const w = &resource;
105105 try writef_kv(w, 1, .{
106106 .@"telemetry.sdk.name" = "github.com/nektro/zig-tracer",
107107 .@"telemetry.sdk.version" = "(hash)",
......@@ -119,27 +119,28 @@ fn deinit_thread_inner() !void {
119119 });
120120 try writef_varint(w, 2, 0);
121121 }
122 var resource_spans: std.ArrayListUnmanaged(u8) = .empty;
123 defer resource_spans.deinit(allocator);
122 var resource_spans: nio.AllocatingWriter = .init(allocator);
123 defer resource_spans.deinit();
124124 {
125 const w = resource_spans.writer(allocator);
125 const w = &resource_spans;
126126 try writef_len(w, 1, resource.items.len);
127127 try w.writeAll(resource.items);
128 resource.clearAndFree(allocator);
128 resource.clearAndFree();
129129 try writef_len(w, 2, scope_spans.items.len);
130130 try w.writeAll(scope_spans.items);
131 scope_spans.clearAndFree(allocator);
131 scope_spans.clearAndFree();
132132 }
133 var export_request: std.ArrayListUnmanaged(u8) = .empty;
134 defer export_request.deinit(allocator);
133 var export_request: nio.AllocatingWriter = .init(allocator);
134 defer export_request.deinit();
135135 {
136 const w = export_request.writer(allocator);
136 const w = &export_request;
137137 try writef_len(w, 1, resource_spans.items.len);
138138 try w.writeAll(resource_spans.items);
139 resource_spans.clearAndFree(allocator);
139 resource_spans.clearAndFree();
140140 }
141141 {
142 var client: std.http.Client = .{ .allocator = allocator };
142 const io = root.io;
143 var client: std.http.Client = .{ .allocator = allocator, .io = io };
143144 defer client.deinit();
144145 var req = try client.request(.POST, traces_endpoint, .{
145146 .headers = .{
......@@ -155,7 +156,7 @@ fn deinit_thread_inner() !void {
155156}
156157
157158pub fn trace_begin(src: std.builtin.SourceLocation, comptime ifmt: []const u8, iargs: anytype) Data {
158 const span_id = extras.randomBytes(8);
159 const span_id = nio.randomBytes(8);
159160 const parent_id = prev_span_id;
160161 prev_span_id = span_id;
161162 const fmt = "{s}:{d}:{d} ({s})" ++ ifmt;
......@@ -176,8 +177,8 @@ pub fn trace_end(ctx: tracer.Ctx) void {
176177 prev_span_id = ctx.data.parent_id;
177178}
178179fn trace_end_inner(ctx: tracer.Ctx) !void {
179 var temp: std.ArrayListUnmanaged(u8) = .empty;
180 const w = temp.writer(allocator);
180 var temp: nio.AllocatingWriter = .init(allocator);
181 const w = &temp;
181182 defer if (ctx.data.name.len > 0) allocator.free(ctx.data.name);
182183 try writef_bytes(w, 1, &trace_id);
183184 try writef_bytes(w, 2, &ctx.data.span_id);
......@@ -258,13 +259,13 @@ fn writef_len(w: anytype, nr: u64, l: u64) !void {
258259
259260// https://opentelemetry.io/docs/specs/semconv/registry/attributes/
260261fn writef_kv(w: anytype, nr: u64, kvs: anytype) !void {
261 var temp: std.ArrayListUnmanaged(u8) = .empty;
262 defer temp.deinit(allocator);
263 const x = temp.writer(allocator);
262 var temp: nio.AllocatingWriter = .init(allocator);
263 defer temp.deinit();
264 const x = &temp;
264265
265 var temp2: std.ArrayListUnmanaged(u8) = .empty;
266 defer temp2.deinit(allocator);
267 const y = temp2.writer(allocator);
266 var temp2: nio.AllocatingWriter = .init(allocator);
267 defer temp2.deinit();
268 const y = &temp2;
268269
269270 inline for (comptime std.meta.fields(@TypeOf(kvs))) |field| blk: {
270271 const value = @field(kvs, field.name);
......@@ -275,11 +276,11 @@ fn writef_kv(w: anytype, nr: u64, kvs: anytype) !void {
275276 try writef_kv_v(y, value);
276277 try writef_len(x, 2, temp2.items.len);
277278 try x.writeAll(temp2.items);
278 temp2.clearRetainingCapacity();
279 temp2.items.len = 0;
279280
280281 try writef_len(w, nr, temp.items.len);
281282 try w.writeAll(temp.items);
282 temp.clearRetainingCapacity();
283 temp.items.len = 0;
283284 }
284285}
285286fn writef_kv_v(w: anytype, v: anytype) !void {