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 @@...@@ -3,7 +3,7 @@
3![loc](https://sloc.xyz/github/nektro/zig-tracer)3![loc](https://sloc.xyz/github/nektro/zig-tracer)
4[![license](https://img.shields.io/github/license/nektro/zig-tracer.svg)](https://github.com/nektro/zig-tracer/blob/master/LICENSE)4[![license](https://img.shields.io/github/license/nektro/zig-tracer.svg)](https://github.com/nektro/zig-tracer/blob/master/LICENSE)
5[![nektro @ github sponsors](https://img.shields.io/badge/sponsors-nektro-purple?logo=github)](https://github.com/sponsors/nektro)5[![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/)
7[![Zigmod](https://img.shields.io/badge/Zigmod-latest-f7a41d)](https://github.com/nektro/zigmod)7[![Zigmod](https://img.shields.io/badge/Zigmod-latest-f7a41d)](https://github.com/nektro/zigmod)
88
9Generic tracing library for Zig, supports multiple backends.9Generic 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...@@ -33,7 +33,7 @@ fn addTest(b: *std.Build, target: std.Build.ResolvedTarget, mode: std.builtin.Op
33 }),33 }),
34 });34 });
35 deps.addAllTo(exe);35 deps.addAllTo(exe);
36 exe.linkLibC();36 exe.root_module.link_libc = true;
37 exe.root_module.addImport("build_options", options.createModule());37 exe.root_module.addImport("build_options", options.createModule());
38 exe.use_llvm = !disable_llvm;38 exe.use_llvm = !disable_llvm;
39 exe.use_lld = !disable_llvm;39 exe.use_lld = !disable_llvm;
src/main.zig+8
...@@ -8,7 +8,15 @@ pub const tracer_backend: tracer.Backend = @enumFromInt(build_options.backend);...@@ -8,7 +8,15 @@ pub const tracer_backend: tracer.Backend = @enumFromInt(build_options.backend);
8pub const otel_service_name = "zig-tracer test";8pub const otel_service_name = "zig-tracer test";
9pub const otel_service_version = build_options.version;9pub const otel_service_version = build_options.version;
1010
11pub var io: std.Io = undefined;
12
11pub fn main() !void {13pub 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
12 try tracer.init(.{});20 try tracer.init(.{});
13 defer tracer.deinit();21 defer tracer.deinit();
1422
src/otel.zig+33-32
...@@ -68,7 +68,7 @@ pub threadlocal var @"url.query": ?[]const u8 = null;...@@ -68,7 +68,7 @@ pub threadlocal var @"url.query": ?[]const u8 = null;
6868
69pub fn init_thread(args: struct { std.mem.Allocator, std.Uri }) !void {69pub fn init_thread(args: struct { std.mem.Allocator, std.Uri }) !void {
70 allocator, traces_endpoint = args;70 allocator, traces_endpoint = args;
71 trace_id = extras.randomBytes(16);71 trace_id = nio.randomBytes(16);
72 prev_span_id = null;72 prev_span_id = null;
73}73}
7474
...@@ -77,31 +77,31 @@ pub fn deinit_thread() void {...@@ -77,31 +77,31 @@ pub fn deinit_thread() void {
77 spans.clearAndFree(allocator);77 spans.clearAndFree(allocator);
78}78}
79fn deinit_thread_inner() !void {79fn deinit_thread_inner() !void {
80 var instrumentation_scope: std.ArrayListUnmanaged(u8) = .empty;80 var instrumentation_scope: nio.AllocatingWriter = .init(allocator);
81 defer instrumentation_scope.deinit(allocator);81 defer instrumentation_scope.deinit();
82 {82 {
83 const w = instrumentation_scope.writer(allocator);83 const w = &instrumentation_scope;
84 try writef_string(w, 1, "github.com/nektro/zig-tracer");84 try writef_string(w, 1, "github.com/nektro/zig-tracer");
85 try writef_string(w, 2, "(hash)");85 try writef_string(w, 2, "(hash)");
86 try writef_len(w, 3, 0);86 try writef_len(w, 3, 0);
87 try writef_varint(w, 4, 0);87 try writef_varint(w, 4, 0);
88 }88 }
89 var scope_spans: std.ArrayListUnmanaged(u8) = .empty;89 var scope_spans: nio.AllocatingWriter = .init(allocator);
90 defer scope_spans.deinit(allocator);90 defer scope_spans.deinit();
91 {91 {
92 const w = scope_spans.writer(allocator);92 const w = &scope_spans;
93 try writef_len(w, 1, instrumentation_scope.items.len);93 try writef_len(w, 1, instrumentation_scope.items.len);
94 try w.writeAll(instrumentation_scope.items);94 try w.writeAll(instrumentation_scope.items);
95 instrumentation_scope.clearAndFree(allocator);95 instrumentation_scope.clearAndFree();
96 for (spans.items) |sp| {96 for (spans.items) |sp| {
97 try writef_len(w, 2, sp.len);97 try writef_len(w, 2, sp.len);
98 try w.writeAll(sp);98 try w.writeAll(sp);
99 }99 }
100 }100 }
101 var resource: std.ArrayListUnmanaged(u8) = .empty;101 var resource: nio.AllocatingWriter = .init(allocator);
102 defer resource.deinit(allocator);102 defer resource.deinit();
103 {103 {
104 const w = resource.writer(allocator);104 const w = &resource;
105 try writef_kv(w, 1, .{105 try writef_kv(w, 1, .{
106 .@"telemetry.sdk.name" = "github.com/nektro/zig-tracer",106 .@"telemetry.sdk.name" = "github.com/nektro/zig-tracer",
107 .@"telemetry.sdk.version" = "(hash)",107 .@"telemetry.sdk.version" = "(hash)",
...@@ -119,27 +119,28 @@ fn deinit_thread_inner() !void {...@@ -119,27 +119,28 @@ fn deinit_thread_inner() !void {
119 });119 });
120 try writef_varint(w, 2, 0);120 try writef_varint(w, 2, 0);
121 }121 }
122 var resource_spans: std.ArrayListUnmanaged(u8) = .empty;122 var resource_spans: nio.AllocatingWriter = .init(allocator);
123 defer resource_spans.deinit(allocator);123 defer resource_spans.deinit();
124 {124 {
125 const w = resource_spans.writer(allocator);125 const w = &resource_spans;
126 try writef_len(w, 1, resource.items.len);126 try writef_len(w, 1, resource.items.len);
127 try w.writeAll(resource.items);127 try w.writeAll(resource.items);
128 resource.clearAndFree(allocator);128 resource.clearAndFree();
129 try writef_len(w, 2, scope_spans.items.len);129 try writef_len(w, 2, scope_spans.items.len);
130 try w.writeAll(scope_spans.items);130 try w.writeAll(scope_spans.items);
131 scope_spans.clearAndFree(allocator);131 scope_spans.clearAndFree();
132 }132 }
133 var export_request: std.ArrayListUnmanaged(u8) = .empty;133 var export_request: nio.AllocatingWriter = .init(allocator);
134 defer export_request.deinit(allocator);134 defer export_request.deinit();
135 {135 {
136 const w = export_request.writer(allocator);136 const w = &export_request;
137 try writef_len(w, 1, resource_spans.items.len);137 try writef_len(w, 1, resource_spans.items.len);
138 try w.writeAll(resource_spans.items);138 try w.writeAll(resource_spans.items);
139 resource_spans.clearAndFree(allocator);139 resource_spans.clearAndFree();
140 }140 }
141 {141 {
142 var client: std.http.Client = .{ .allocator = allocator };142 const io = root.io;
143 var client: std.http.Client = .{ .allocator = allocator, .io = io };
143 defer client.deinit();144 defer client.deinit();
144 var req = try client.request(.POST, traces_endpoint, .{145 var req = try client.request(.POST, traces_endpoint, .{
145 .headers = .{146 .headers = .{
...@@ -155,7 +156,7 @@ fn deinit_thread_inner() !void {...@@ -155,7 +156,7 @@ fn deinit_thread_inner() !void {
155}156}
156157
157pub fn trace_begin(src: std.builtin.SourceLocation, comptime ifmt: []const u8, iargs: anytype) Data {158pub 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);
159 const parent_id = prev_span_id;160 const parent_id = prev_span_id;
160 prev_span_id = span_id;161 prev_span_id = span_id;
161 const fmt = "{s}:{d}:{d} ({s})" ++ ifmt;162 const fmt = "{s}:{d}:{d} ({s})" ++ ifmt;
...@@ -176,8 +177,8 @@ pub fn trace_end(ctx: tracer.Ctx) void {...@@ -176,8 +177,8 @@ pub fn trace_end(ctx: tracer.Ctx) void {
176 prev_span_id = ctx.data.parent_id;177 prev_span_id = ctx.data.parent_id;
177}178}
178fn trace_end_inner(ctx: tracer.Ctx) !void {179fn trace_end_inner(ctx: tracer.Ctx) !void {
179 var temp: std.ArrayListUnmanaged(u8) = .empty;180 var temp: nio.AllocatingWriter = .init(allocator);
180 const w = temp.writer(allocator);181 const w = &temp;
181 defer if (ctx.data.name.len > 0) allocator.free(ctx.data.name);182 defer if (ctx.data.name.len > 0) allocator.free(ctx.data.name);
182 try writef_bytes(w, 1, &trace_id);183 try writef_bytes(w, 1, &trace_id);
183 try writef_bytes(w, 2, &ctx.data.span_id);184 try writef_bytes(w, 2, &ctx.data.span_id);
...@@ -258,13 +259,13 @@ fn writef_len(w: anytype, nr: u64, l: u64) !void {...@@ -258,13 +259,13 @@ fn writef_len(w: anytype, nr: u64, l: u64) !void {
258259
259// https://opentelemetry.io/docs/specs/semconv/registry/attributes/260// https://opentelemetry.io/docs/specs/semconv/registry/attributes/
260fn writef_kv(w: anytype, nr: u64, kvs: anytype) !void {261fn writef_kv(w: anytype, nr: u64, kvs: anytype) !void {
261 var temp: std.ArrayListUnmanaged(u8) = .empty;262 var temp: nio.AllocatingWriter = .init(allocator);
262 defer temp.deinit(allocator);263 defer temp.deinit();
263 const x = temp.writer(allocator);264 const x = &temp;
264265
265 var temp2: std.ArrayListUnmanaged(u8) = .empty;266 var temp2: nio.AllocatingWriter = .init(allocator);
266 defer temp2.deinit(allocator);267 defer temp2.deinit();
267 const y = temp2.writer(allocator);268 const y = &temp2;
268269
269 inline for (comptime std.meta.fields(@TypeOf(kvs))) |field| blk: {270 inline for (comptime std.meta.fields(@TypeOf(kvs))) |field| blk: {
270 const value = @field(kvs, field.name);271 const value = @field(kvs, field.name);
...@@ -275,11 +276,11 @@ fn writef_kv(w: anytype, nr: u64, kvs: anytype) !void {...@@ -275,11 +276,11 @@ fn writef_kv(w: anytype, nr: u64, kvs: anytype) !void {
275 try writef_kv_v(y, value);276 try writef_kv_v(y, value);
276 try writef_len(x, 2, temp2.items.len);277 try writef_len(x, 2, temp2.items.len);
277 try x.writeAll(temp2.items);278 try x.writeAll(temp2.items);
278 temp2.clearRetainingCapacity();279 temp2.items.len = 0;
279280
280 try writef_len(w, nr, temp.items.len);281 try writef_len(w, nr, temp.items.len);
281 try w.writeAll(temp.items);282 try w.writeAll(temp.items);
282 temp.clearRetainingCapacity();283 temp.items.len = 0;
283 }284 }
284}285}
285fn writef_kv_v(w: anytype, v: anytype) !void {286fn writef_kv_v(w: anytype, v: anytype) !void {