authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-06-30 18:44:57 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-06-30 18:44:57 -07:00
logb40045e5dd6e7c9e7c0f173d33b0b878ebf55c2b
treed0e52b86636ce65532cc36d0945796072e9c8d80
parent04c3ad0fbdfb57dea27da0c8cbb75e2539837c92
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

disable if traces_endpoint isn't passed


1 files changed, 6 insertions(+), 4 deletions(-)

src/otel.zig+6-4
......@@ -51,7 +51,7 @@ pub fn deinit() void {
5151}
5252
5353threadlocal var allocator: std.mem.Allocator = undefined;
54threadlocal var traces_endpoint: std.Uri = undefined;
54threadlocal var traces_endpoint: ?std.Uri = null;
5555pub threadlocal var trace_id: [16]u8 = undefined;
5656threadlocal var prev_span_id: ?[8]u8 = undefined;
5757threadlocal var spans: std.ArrayListUnmanaged([]const u8) = .empty;
......@@ -66,7 +66,7 @@ pub threadlocal var @"http.route": ?[]const u8 = null;
6666pub threadlocal var @"url.path": ?[]const u8 = null;
6767pub 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 {
7070 allocator, traces_endpoint = args;
7171 trace_id = nio.randomBytes(16);
7272 prev_span_id = null;
......@@ -77,6 +77,7 @@ pub fn deinit_thread() void {
7777 spans.clearAndFree(allocator);
7878}
7979fn deinit_thread_inner() !void {
80 if (traces_endpoint == null) return;
8081 var instrumentation_scope: nio.AllocatingWriter = .init(allocator);
8182 defer instrumentation_scope.deinit();
8283 {
......@@ -142,7 +143,7 @@ fn deinit_thread_inner() !void {
142143 const io = root.io;
143144 var client: std.http.Client = .{ .allocator = allocator, .io = io };
144145 defer client.deinit();
145 var req = try client.request(.POST, traces_endpoint, .{
146 var req = try client.request(.POST, traces_endpoint.?, .{
146147 .headers = .{
147148 .content_type = .{ .override = "application/x-protobuf" },
148149 },
......@@ -161,7 +162,7 @@ pub fn trace_begin(src: std.builtin.SourceLocation, comptime ifmt: []const u8, i
161162 prev_span_id = span_id;
162163 const fmt = "{s}:{d}:{d} ({s})" ++ ifmt;
163164 const args = .{ src.file, src.line, src.column, src.fn_name } ++ iargs;
164 const name = std.fmt.allocPrint(allocator, fmt, args) catch "";
165 const name = if (traces_endpoint == null) "" else std.fmt.allocPrint(allocator, fmt, args) catch "";
165166 const time_start_ns: u64 = @intCast(time.nanoTimestamp()); // this will overflow 2554 July 21 23:34:33.709 Z
166167
167168 return .{
......@@ -177,6 +178,7 @@ pub fn trace_end(ctx: tracer.Ctx) void {
177178 prev_span_id = ctx.data.parent_id;
178179}
179180fn trace_end_inner(ctx: tracer.Ctx) !void {
181 if (traces_endpoint == null) return;
180182 var temp: nio.AllocatingWriter = .init(allocator);
181183 const w = &temp;
182184 defer if (ctx.data.name.len > 0) allocator.free(ctx.data.name);