| ... | ... | @@ -12,12 +12,12 @@ const sys = switch (builtin.target.os.tag) { |
| 12 | 12 | else => unreachable, |
| 13 | 13 | }; |
| 14 | 14 | |
| 15 | | // export OTEL_EXPORTER_OTLP_PROTOCOL=grpc |
| 16 | | // export OTEL_EXPORTER_OTLP_PROTOCOL=http/json |
| 17 | 15 | // export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf |
| 18 | 16 | // export OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318 |
| 19 | 17 | // export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:4318/v1/traces |
| 20 | 18 | |
| 19 | // https://github.com/open-telemetry/opentelemetry-proto/tree/v1.10.0/opentelemetry/proto |
| 20 | |
| 21 | 21 | pub fn init(args: struct {}) !void { |
| 22 | 22 | _ = args; |
| 23 | 23 | } |
| ... | ... | @@ -28,76 +28,19 @@ pub fn deinit() void { |
| 28 | 28 | |
| 29 | 29 | threadlocal var allocator: std.mem.Allocator = undefined; |
| 30 | 30 | threadlocal var traces_endpoint: std.Uri = undefined; |
| 31 | | threadlocal var memfd: nfs.File = undefined; |
| 32 | | threadlocal var buffered_writer: nio.BufferedWriter(4096, nfs.File) = undefined; |
| 33 | 31 | threadlocal var trace_id: [16]u8 = undefined; |
| 34 | 32 | threadlocal var prev_span_id: ?[8]u8 = undefined; |
| 35 | 33 | threadlocal var spans: std.ArrayListUnmanaged([]const u8) = .empty; |
| 36 | 34 | |
| 37 | 35 | pub fn init_thread(args: struct { std.mem.Allocator, std.Uri }) !void { |
| 38 | | std.log.debug("called otel init_thread", .{}); |
| 39 | 36 | allocator, traces_endpoint = args; |
| 40 | | memfd = try nfs.memfd_create("zig-tracer", 0); |
| 41 | | buffered_writer = .init(memfd); |
| 42 | 37 | trace_id = extras.randomBytes(16); |
| 43 | 38 | prev_span_id = null; |
| 44 | | |
| 45 | | // try write_tag(1, .varint); |
| 46 | | // try write_varint(150); |
| 47 | | // 0896 01 |
| 48 | | // 1: 150 |
| 49 | | |
| 50 | | // try write_tag(2, .len); |
| 51 | | // try write_varint(7); |
| 52 | | // try buffered_writer.writeAll("testing"); |
| 53 | | // 1207 7465 7374 696e 67 |
| 54 | | // 2: {"testing"} |
| 55 | | |
| 56 | | // try write_tag(3, .len); |
| 57 | | // try write_varint(3); |
| 58 | | // try write_tag(1, .varint); |
| 59 | | // try write_varint(150); |
| 60 | | // 1a03 0896 01 |
| 61 | | // 3: {1: 150} |
| 62 | | |
| 63 | | // try write_tag(4, .len); |
| 64 | | // try write_varint(5); |
| 65 | | // try buffered_writer.writeAll("hello"); |
| 66 | | // try write_tag(6, .varint); |
| 67 | | // try write_varint(3); |
| 68 | | // try write_tag(6, .varint); |
| 69 | | // try write_varint(270); |
| 70 | | // try write_tag(6, .varint); |
| 71 | | // try write_varint(86942); |
| 72 | | // 2205 6865 6c6c 6f30 0330 8e02 309e a705 |
| 73 | | // 4: {"hello"} |
| 74 | | // 6: 3 |
| 75 | | // 6: 270 |
| 76 | | // 6: 86942 |
| 77 | | |
| 78 | | // try write_tag(3, .sgroup); |
| 79 | | // try write_tag(1, .varint); |
| 80 | | // try write_varint(150); |
| 81 | | // try write_tag(3, .egroup); |
| 82 | | // 1b08 9601 1c |
| 83 | | // 3: !{1: 150} |
| 84 | | |
| 85 | | // try write_tag(4, .sgroup); |
| 86 | | // try write_tag(3, .sgroup); |
| 87 | | // try write_tag(1, .varint); |
| 88 | | // try write_varint(150); |
| 89 | | // try write_tag(3, .egroup); |
| 90 | | // try write_tag(4, .egroup); |
| 91 | | // 231b 0896 011c 24 |
| 92 | | // 4: !{3: !{1: 150}} |
| 93 | | |
| 94 | 39 | } |
| 95 | 40 | |
| 96 | 41 | pub fn deinit_thread() void { |
| 97 | 42 | deinit_thread_inner() catch {}; |
| 98 | 43 | spans.clearAndFree(allocator); |
| 99 | | buffered_writer.flush() catch {}; |
| 100 | | memfd.close(); |
| 101 | 44 | } |
| 102 | 45 | fn deinit_thread_inner() !void { |
| 103 | 46 | var instrumentation_scope: std.ArrayListUnmanaged(u8) = .empty; |
| ... | ... | @@ -170,6 +113,7 @@ fn deinit_thread_inner() !void { |
| 170 | 113 | try req.writeAll(export_request.items); |
| 171 | 114 | try req.finish(); |
| 172 | 115 | try req.wait(); |
| 116 | if (req.response.status != .ok) std.log.scoped(.tracer).warn("otel: {s} {d}", .{ &trace_id, req.response.status }); |
| 173 | 117 | } |
| 174 | 118 | } |
| 175 | 119 | |
| ... | ... | @@ -215,112 +159,6 @@ pub const Data = struct { |
| 215 | 159 | time_start_ns: u64, |
| 216 | 160 | }; |
| 217 | 161 | |
| 218 | | // |
| 219 | | // |
| 220 | | |
| 221 | | // https://github.com/open-telemetry/opentelemetry-proto/tree/v1.10.0/opentelemetry/proto |
| 222 | | |
| 223 | | // message ExportTraceServiceRequest { |
| 224 | | // repeated opentelemetry.proto.trace.v1.ResourceSpans resource_spans = 1; |
| 225 | | // } |
| 226 | | |
| 227 | | // message ResourceSpans { |
| 228 | | // reserved 1000; |
| 229 | | // opentelemetry.proto.resource.v1.Resource resource = 1; |
| 230 | | // repeated ScopeSpans scope_spans = 2; |
| 231 | | // string schema_url = 3; |
| 232 | | // } |
| 233 | | |
| 234 | | // message Resource { |
| 235 | | // repeated opentelemetry.proto.common.v1.KeyValue attributes = 1; |
| 236 | | // uint32 dropped_attributes_count = 2; |
| 237 | | // } |
| 238 | | |
| 239 | | // message KeyValue { |
| 240 | | // string key = 1; |
| 241 | | // AnyValue value = 2; |
| 242 | | // } |
| 243 | | |
| 244 | | // message AnyValue { |
| 245 | | // oneof value { |
| 246 | | // string string_value = 1; |
| 247 | | // bool bool_value = 2; |
| 248 | | // int64 int_value = 3; |
| 249 | | // double double_value = 4; |
| 250 | | // ArrayValue array_value = 5; |
| 251 | | // KeyValueList kvlist_value = 6; |
| 252 | | // bytes bytes_value = 7; |
| 253 | | // } |
| 254 | | // } |
| 255 | | |
| 256 | | // message ArrayValue { |
| 257 | | // repeated AnyValue values = 1; |
| 258 | | // } |
| 259 | | |
| 260 | | // message KeyValueList { |
| 261 | | // repeated KeyValue values = 1; |
| 262 | | // } |
| 263 | | |
| 264 | | // message ScopeSpans { |
| 265 | | // opentelemetry.proto.common.v1.InstrumentationScope scope = 1; |
| 266 | | // repeated Span spans = 2; |
| 267 | | // string schema_url = 3; |
| 268 | | // } |
| 269 | | |
| 270 | | // message InstrumentationScope { |
| 271 | | // string name = 1; |
| 272 | | // string version = 2; |
| 273 | | // repeated KeyValue attributes = 3; |
| 274 | | // uint32 dropped_attributes_count = 4; |
| 275 | | // } |
| 276 | | |
| 277 | | // message Span { |
| 278 | | // bytes trace_id = 1; |
| 279 | | // bytes span_id = 2; |
| 280 | | // string trace_state = 3; |
| 281 | | // bytes parent_span_id = 4; |
| 282 | | // fixed32 flags = 16; |
| 283 | | // string name = 5; |
| 284 | | // SpanKind kind = 6; |
| 285 | | // fixed64 start_time_unix_nano = 7; |
| 286 | | // fixed64 end_time_unix_nano = 8; |
| 287 | | // repeated opentelemetry.proto.common.v1.KeyValue attributes = 9; |
| 288 | | // uint32 dropped_attributes_count = 10; |
| 289 | | // repeated Event events = 11; |
| 290 | | // uint32 dropped_events_count = 12; |
| 291 | | // repeated Link links = 13; |
| 292 | | // uint32 dropped_links_count = 14; |
| 293 | | // Status status = 15; |
| 294 | | // } |
| 295 | | |
| 296 | | // enum SpanKind { |
| 297 | | // SPAN_KIND_UNSPECIFIED = 0; |
| 298 | | // SPAN_KIND_INTERNAL = 1; |
| 299 | | // SPAN_KIND_SERVER = 2; |
| 300 | | // SPAN_KIND_CLIENT = 3; |
| 301 | | // SPAN_KIND_PRODUCER = 4; |
| 302 | | // SPAN_KIND_CONSUMER = 5; |
| 303 | | // } |
| 304 | | |
| 305 | | // message Event { |
| 306 | | // fixed64 time_unix_nano = 1; |
| 307 | | // string name = 2; |
| 308 | | // repeated opentelemetry.proto.common.v1.KeyValue attributes = 3; |
| 309 | | // uint32 dropped_attributes_count = 4; |
| 310 | | // } |
| 311 | | |
| 312 | | // message Link { |
| 313 | | // bytes trace_id = 1; |
| 314 | | // bytes span_id = 2; |
| 315 | | // string trace_state = 3; |
| 316 | | // repeated opentelemetry.proto.common.v1.KeyValue attributes = 4; |
| 317 | | // uint32 dropped_attributes_count = 5; |
| 318 | | // fixed32 flags = 6; |
| 319 | | // } |
| 320 | | |
| 321 | | // |
| 322 | | // |
| 323 | | |
| 324 | 162 | const WireType = enum { |
| 325 | 163 | varint, |
| 326 | 164 | i64, |