Skip to content

Commit 18f1079

Browse files
committed
depends protobuf defs only in api/trace/ structure
Signed-off-by: drumato <[email protected]>
1 parent 3e49103 commit 18f1079

File tree

7 files changed

+111
-13
lines changed

7 files changed

+111
-13
lines changed

src/api/trace.zig

+43-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ pub const Tracer = @import("trace/tracer.zig").Tracer;
44
pub const TracerProvider = @import("trace/provider.zig").TracerProvider;
55
pub const TracerConfig = @import("trace/config.zig").TracerConfig;
66

7+
const span = @import("trace/span.zig");
8+
pub const Span = span.Span;
9+
pub const SpanKind = span.SpanKind;
10+
pub const Status = span.Status;
11+
pub const Event = @import("trace/event.zig").Event;
12+
pub const Code = @import("trace/code.zig").Code;
13+
pub const Link = @import("trace/link.zig").Link;
14+
15+
test {
16+
_ = @import("trace/code.zig");
17+
_ = @import("trace/config.zig");
18+
_ = @import("trace/event.zig");
19+
_ = @import("trace/link.zig");
20+
_ = @import("trace/provider.zig");
21+
_ = @import("trace/span.zig");
22+
_ = @import("trace/tracer.zig");
23+
}
24+
725
pub const TraceID = struct {
826
value: [16]u8,
927

@@ -15,6 +33,27 @@ pub const TraceID = struct {
1533
};
1634
}
1735

36+
pub fn zero() Self {
37+
return init([16]u8{
38+
0,
39+
0,
40+
0,
41+
0,
42+
0,
43+
0,
44+
0,
45+
0,
46+
0,
47+
0,
48+
0,
49+
0,
50+
0,
51+
0,
52+
0,
53+
0,
54+
});
55+
}
56+
1857
pub fn isValid(self: Self) bool {
1958
for (self.value) |item| {
2059
if (item != 0) {
@@ -37,6 +76,10 @@ pub const SpanID = struct {
3776
};
3877
}
3978

79+
pub fn zero() Self {
80+
return init([8]u8{ 0, 0, 0, 0, 0, 0, 0, 0 });
81+
}
82+
4083
pub fn isValid(self: Self) bool {
4184
for (self.value) |item| {
4285
if (item != 0) {
@@ -48,12 +91,6 @@ pub const SpanID = struct {
4891
}
4992
};
5093

51-
test {
52-
_ = @import("trace/config.zig");
53-
_ = @import("trace/provider.zig");
54-
_ = @import("trace/tracer.zig");
55-
}
56-
5794
test "TraceID isValid" {
5895
try std.testing.expect(TraceID.init([16]u8{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }).isValid());
5996
try std.testing.expect(!TraceID.init([16]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }).isValid());

src/api/trace/code.zig

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub const Code = enum {
2+
Unset,
3+
Error,
4+
Ok,
5+
};

src/api/trace/event.zig

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const attributes = @import("../../attributes.zig");
2+
3+
pub const Event = struct {
4+
name: []const u8,
5+
attributes: []attributes.Attribute,
6+
time_uniX_nano: u64 = 0,
7+
};

src/api/trace/link.zig

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const attributes = @import("../../attributes.zig");
2+
pub const Link = struct {
3+
// TODO
4+
// span_context: trace.SpanContext,
5+
6+
attributes: []attributes.Attribute,
7+
};

src/api/trace/span.zig

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const std = @import("std");
2+
3+
const attribute = @import("../../attributes.zig");
4+
const trace = @import("../trace.zig");
5+
const pb = @import("../../opentelemetry/proto/trace/v1.pb.zig");
6+
7+
/// Span represents a span of a trace.
8+
/// It is the only component that depends protobuf definition.
9+
pub const Span = struct {
10+
trace_id: trace.TraceID = trace.TraceID.zero(),
11+
span_id: trace.SpanID = trace.SpanID.zero(),
12+
// TODO: maybe not string
13+
trace_state: []const u8 = "",
14+
parent_span_id: ?trace.SpanID = null,
15+
flags: u32 = 0,
16+
name: []const u8 = "",
17+
kind: SpanKind = SpanKind.Unspecified,
18+
start_time_unix_nano: u64 = 0,
19+
end_time_unix_nano: u64 = 0,
20+
attributes: []attribute.Attribute = undefined,
21+
// dropped_attributes_count
22+
events: []trace.Event,
23+
// dropped_events_count
24+
links: []trace.Link = undefined,
25+
// dropped_links_count
26+
status: ?Status = null,
27+
};
28+
29+
pub const SpanKind = enum {
30+
Unspecified,
31+
Internal,
32+
Server,
33+
Client,
34+
Producer,
35+
Consumer,
36+
};
37+
38+
pub const Status = struct {
39+
code: trace.Code,
40+
description: []const u8,
41+
};

src/sdk/trace/exporters/stdout_exporter.zig

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const std = @import("std");
2-
const pb = @import("../../../opentelemetry/proto/trace/v1.pb.zig");
3-
const protobuf = @import("protobuf");
2+
3+
const trace = @import("../../../api/trace.zig");
44
const SpanExporter = @import("../span_exporter.zig").SpanExporter;
55

66
/// GenericWriterExporter is the generic SpanExporter that outputs spans to the given writer.
@@ -18,7 +18,7 @@ fn GenericWriterExporter(
1818
};
1919
}
2020

21-
pub fn exportSpans(ctx: *anyopaque, spans: []pb.Span) anyerror!void {
21+
pub fn exportSpans(ctx: *anyopaque, spans: []trace.Span) anyerror!void {
2222
const self: *Self = @ptrCast(@alignCast(ctx));
2323
try std.json.stringify(spans, .{}, self.writer);
2424
}
@@ -51,8 +51,9 @@ test "GenericWriterExporter" {
5151
var inmemory_exporter = InmemoryExporter.init(out_buf.writer());
5252
var exporter = inmemory_exporter.asSpanExporter();
5353

54-
var spans = [_]pb.Span{};
54+
var spans = [_]trace.Span{};
5555
try exporter.exportSpans(spans[0..spans.len]);
5656

57+
std.debug.print("{s}", .{out_buf.items});
5758
// TODO: detailed output test
5859
}

src/sdk/trace/span_exporter.zig

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const pb = @import("../../opentelemetry/proto/trace/v1.pb.zig");
1+
const trace = @import("../../api/trace.zig");
22

33
/// SpanExporter is the interface that provides an
44
pub const SpanExporter = struct {
@@ -15,15 +15,15 @@ pub const SpanExporter = struct {
1515
/// but it is not defined in the OpenTelemetry specification, so for now we don't use it.
1616
exportSpansFn: *const fn (
1717
ctx: *anyopaque,
18-
spans: []pb.Span,
18+
spans: []trace.Span,
1919
) anyerror!void,
2020

2121
shutdownFn: *const fn (ctx: *anyopaque) anyerror!void,
2222
};
2323

2424
pub fn exportSpans(
2525
self: Self,
26-
spans: []pb.Span,
26+
spans: []trace.Span,
2727
) anyerror!void {
2828
return self.vtable.exportSpansFn(self.ptr, spans);
2929
}

0 commit comments

Comments
 (0)