From 2aee346299196b66e7687e31c72eee9abb09261c Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Fri, 21 Mar 2025 22:37:56 +0800 Subject: [PATCH] send telemetry events in batches (up to 20) --- src/telemetry/lightpanda.zig | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/telemetry/lightpanda.zig b/src/telemetry/lightpanda.zig index d9256575..f935d412 100644 --- a/src/telemetry/lightpanda.zig +++ b/src/telemetry/lightpanda.zig @@ -10,6 +10,7 @@ const telemetry = @import("telemetry.zig"); const log = std.log.scoped(.telemetry); const URL = "https://telemetry.lightpanda.io"; +const MAX_BATCH_SIZE = 20; pub const LightPanda = struct { uri: std.Uri, @@ -73,18 +74,18 @@ pub const LightPanda = struct { fn run(self: *LightPanda) void { const client = self.client; var arr: std.ArrayListUnmanaged(u8) = .{}; - defer arr.deinit(self.allocator); + var batch: [MAX_BATCH_SIZE]LightPandaEvent = undefined; self.mutex.lock(); while (true) { - while (self.pending.popFirst()) |node| { + while (self.pending.first != null) { + const b = self.collectBatch(&batch); self.mutex.unlock(); - self.postEvent(&node.data, client, &arr) catch |err| { + self.postEvent(b, client, &arr) catch |err| { log.warn("Telementry reporting error: {}", .{err}); }; self.mutex.lock(); - self.node_pool.destroy(node); } if (self.running == false) { return; @@ -93,9 +94,13 @@ pub const LightPanda = struct { } } - fn postEvent(self: *const LightPanda, event: *const LightPandaEvent, client: *std.http.Client, arr: *std.ArrayListUnmanaged(u8)) !void { + fn postEvent(self: *const LightPanda, events: []LightPandaEvent, client: *std.http.Client, arr: *std.ArrayListUnmanaged(u8)) !void { defer arr.clearRetainingCapacity(); - try std.json.stringify(event, .{ .emit_null_optional_fields = false }, arr.writer(self.allocator)); + var writer = arr.writer(self.allocator); + for (events) |event| { + try std.json.stringify(event, .{ .emit_null_optional_fields = false }, writer); + try writer.writeByte('\n'); + } var response_header_buffer: [2048]u8 = undefined; const result = try client.fetch(.{ @@ -109,6 +114,21 @@ pub const LightPanda = struct { log.warn("server error status: {}", .{result.status}); } } + + fn collectBatch(self: *LightPanda, into: []LightPandaEvent) []LightPandaEvent { + var i: usize = 0; + const node_pool = &self.node_pool; + while (self.pending.popFirst()) |node| { + into[i] = node.data; + node_pool.destroy(node); + + i += 1; + if (i == MAX_BATCH_SIZE) { + break; + } + } + return into[0..i]; + } }; const LightPandaEvent = struct {