1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const std = @import("std");
const archive_mod = @import("../storage/archive.zig");
const observability = @import("observability.zig");
const segment = @import("../storage/segment.zig");
const Io = std.Io;
const Capture = struct {
body: [128 * 1024]u8 = undefined,
len: usize = 0,
};
const Collector = struct {
fn receive(listener: *Io.net.Server, io: Io, capture: *Capture) !void {
var stream = try listener.accept(io);
defer stream.close(io);
var read_buffer: [4096]u8 = undefined;
var write_buffer: [4096]u8 = undefined;
var reader = stream.reader(io, &read_buffer);
var writer = stream.writer(io, &write_buffer);
var server = std.http.Server.init(&reader.interface, &writer.interface);
var request = try server.receiveHead();
var body_buffer: [4096]u8 = undefined;
const body_reader = request.readerExpectNone(&body_buffer);
var body_writer = std.Io.Writer.fixed(&capture.body);
_ = try body_reader.streamRemaining(&body_writer);
capture.len = body_writer.buffered().len;
try request.respond("", .{ .status = .ok, .keep_alive = false });
}
};
fn event(seq_hint: u64) segment.Event {
return .{
.seq = 0,
.witnessed_at = @intCast(1_700_000_000_000_000 + seq_hint),
.indexed_at = 0,
.kind = .create,
.did = "did:plc:tracecontract",
.collection = "app.bsky.feed.post",
.rkey = "3l3qo2vuowo2b",
.rev = "3l3qo2vutsw2b",
.payload = "\xa1\x64\x74\x65\x78\x74\x62\x68\x69",
};
}
test "real archive boundaries export the upstream span tree offline" {
const testing = std.testing;
var threaded: Io.Threaded = .init(testing.allocator, .{});
defer threaded.deinit();
const io = threaded.io();
var listener = (Io.net.IpAddress{ .ip4 = .loopback(0) }).listen(io, .{ .reuse_address = true }) catch unreachable;
defer listener.deinit(io);
var capture: Capture = .{};
var collector = try io.concurrent(Collector.receive, .{ &listener, io, &capture });
defer _ = collector.cancel(io) catch {};
var endpoint_buffer: [128]u8 = undefined;
const endpoint = try std.fmt.bufPrint(
&endpoint_buffer,
"OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:{d}",
.{listener.socket.address.getPort()},
);
var runtime = try observability.Runtime.init(testing.allocator, .{
.io = io,
.environment = &.{ endpoint, "OTEL_BSP_SCHEDULE_DELAY=60000" },
.service_name = "stream-tracing-contract",
.service_version = "offline",
});
defer runtime.deinit();
var process_batch = observability.Span.start(
testing.allocator,
"ingest/live",
"processBatch",
observability.root_context,
&.{},
);
{
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
var path_buffer: [Io.Dir.max_path_bytes]u8 = undefined;
const path_len = try tmp.dir.realPath(io, &path_buffer);
var archive = try archive_mod.Archive.init(testing.allocator, io, path_buffer[0..path_len]);
defer archive.deinit();
archive.writer.max_events_per_block = 1;
archive.max_events_per_block = 1;
archive.max_segment_bytes = 1;
try archive.enableAsyncFlush(1);
var async_row = [_]segment.Event{event(1)};
_ = try archive.appendBatchTraced(&async_row, 1, process_batch.context());
archive.disableAsyncFlush();
archive.max_segment_bytes = archive_mod.default_max_segment_bytes;
var sync_row = [_]segment.Event{event(2)};
_ = try archive.appendBatchTraced(&sync_row, 2, process_batch.context());
}
process_batch.succeed();
process_batch.deinit();
try testing.expectEqual(@import("otel-api").common.ProcessResult.success, runtime.shutdown(2_000));
try collector.await(io);
const body = capture.body[0..capture.len];
for ([_][]const u8{
"processBatch",
"commitAsyncFlush",
"rotateIfFull",
"rotateLocked",
"flushAndRotateLocked",
"jetstream/ingest/live",
"jetstream/ingest",
}) |needle| try testing.expect(std.mem.indexOf(u8, body, needle) != null);
}