| ... | @@ -51,7 +51,7 @@ pub fn deinit() void { | ... | @@ -51,7 +51,7 @@ pub fn deinit() void { |
| 51 | } | 51 | } |
| 52 | | 52 | |
| 53 | threadlocal var allocator: std.mem.Allocator = undefined; | 53 | threadlocal var allocator: std.mem.Allocator = undefined; |
| 54 | threadlocal var traces_endpoint: std.Uri = undefined; | 54 | threadlocal var traces_endpoint: ?std.Uri = null; |
| 55 | pub threadlocal var trace_id: [16]u8 = undefined; | 55 | pub threadlocal var trace_id: [16]u8 = undefined; |
| 56 | threadlocal var prev_span_id: ?[8]u8 = undefined; | 56 | threadlocal var prev_span_id: ?[8]u8 = undefined; |
| 57 | threadlocal var spans: std.ArrayListUnmanaged([]const u8) = .empty; | 57 | threadlocal var spans: std.ArrayListUnmanaged([]const u8) = .empty; |
| ... | @@ -66,7 +66,7 @@ pub threadlocal var @"http.route": ?[]const u8 = null; | ... | @@ -66,7 +66,7 @@ pub threadlocal var @"http.route": ?[]const u8 = null; |
| 66 | pub threadlocal var @"url.path": ?[]const u8 = null; | 66 | pub threadlocal var @"url.path": ?[]const u8 = null; |
| 67 | pub threadlocal var @"url.query": ?[]const u8 = null; | 67 | pub threadlocal var @"url.query": ?[]const u8 = null; |
| 68 | | 68 | |
| 69 | pub fn init_thread(args: struct { std.mem.Allocator, std.Uri }) !void { | 69 | pub fn init_thread(args: struct { std.mem.Allocator, ?std.Uri }) !void { |
| 70 | allocator, traces_endpoint = args; | 70 | allocator, traces_endpoint = args; |
| 71 | trace_id = nio.randomBytes(16); | 71 | trace_id = nio.randomBytes(16); |
| 72 | prev_span_id = null; | 72 | prev_span_id = null; |
| ... | @@ -77,6 +77,7 @@ pub fn deinit_thread() void { | ... | @@ -77,6 +77,7 @@ pub fn deinit_thread() void { |
| 77 | spans.clearAndFree(allocator); | 77 | spans.clearAndFree(allocator); |
| 78 | } | 78 | } |
| 79 | fn deinit_thread_inner() !void { | 79 | fn deinit_thread_inner() !void { |
| | 80 | if (traces_endpoint == null) return; |
| 80 | var instrumentation_scope: nio.AllocatingWriter = .init(allocator); | 81 | var instrumentation_scope: nio.AllocatingWriter = .init(allocator); |
| 81 | defer instrumentation_scope.deinit(); | 82 | defer instrumentation_scope.deinit(); |
| 82 | { | 83 | { |
| ... | @@ -142,7 +143,7 @@ fn deinit_thread_inner() !void { | ... | @@ -142,7 +143,7 @@ fn deinit_thread_inner() !void { |
| 142 | const io = root.io; | 143 | const io = root.io; |
| 143 | var client: std.http.Client = .{ .allocator = allocator, .io = io }; | 144 | var client: std.http.Client = .{ .allocator = allocator, .io = io }; |
| 144 | defer client.deinit(); | 145 | defer client.deinit(); |
| 145 | var req = try client.request(.POST, traces_endpoint, .{ | 146 | var req = try client.request(.POST, traces_endpoint.?, .{ |
| 146 | .headers = .{ | 147 | .headers = .{ |
| 147 | .content_type = .{ .override = "application/x-protobuf" }, | 148 | .content_type = .{ .override = "application/x-protobuf" }, |
| 148 | }, | 149 | }, |
| ... | @@ -161,7 +162,7 @@ pub fn trace_begin(src: std.builtin.SourceLocation, comptime ifmt: []const u8, i | ... | @@ -161,7 +162,7 @@ pub fn trace_begin(src: std.builtin.SourceLocation, comptime ifmt: []const u8, i |
| 161 | prev_span_id = span_id; | 162 | prev_span_id = span_id; |
| 162 | const fmt = "{s}:{d}:{d} ({s})" ++ ifmt; | 163 | const fmt = "{s}:{d}:{d} ({s})" ++ ifmt; |
| 163 | const args = .{ src.file, src.line, src.column, src.fn_name } ++ iargs; | 164 | 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 ""; |
| 165 | const time_start_ns: u64 = @intCast(time.nanoTimestamp()); // this will overflow 2554 July 21 23:34:33.709 Z | 166 | const time_start_ns: u64 = @intCast(time.nanoTimestamp()); // this will overflow 2554 July 21 23:34:33.709 Z |
| 166 | | 167 | |
| 167 | return .{ | 168 | return .{ |
| ... | @@ -177,6 +178,7 @@ pub fn trace_end(ctx: tracer.Ctx) void { | ... | @@ -177,6 +178,7 @@ pub fn trace_end(ctx: tracer.Ctx) void { |
| 177 | prev_span_id = ctx.data.parent_id; | 178 | prev_span_id = ctx.data.parent_id; |
| 178 | } | 179 | } |
| 179 | fn trace_end_inner(ctx: tracer.Ctx) !void { | 180 | fn trace_end_inner(ctx: tracer.Ctx) !void { |
| | 181 | if (traces_endpoint == null) return; |
| 180 | var temp: nio.AllocatingWriter = .init(allocator); | 182 | var temp: nio.AllocatingWriter = .init(allocator); |
| 181 | const w = &temp; | 183 | const w = &temp; |
| 182 | defer if (ctx.data.name.len > 0) allocator.free(ctx.data.name); | 184 | defer if (ctx.data.name.len > 0) allocator.free(ctx.data.name); |