Merge pull request #484 from lightpanda-io/telemetry_batch
Some checks failed
e2e-test / zig build release (push) Has been cancelled
wpt / web platform tests (push) Has been cancelled
zig-test / zig build dev (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
e2e-test / puppeteer-perf (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled

send telemetry events in batches (up to 20)
This commit is contained in:
Pierre Tachoire
2025-03-23 11:21:56 +01:00
committed by GitHub

View File

@@ -10,6 +10,7 @@ const telemetry = @import("telemetry.zig");
const log = std.log.scoped(.telemetry); const log = std.log.scoped(.telemetry);
const URL = "https://telemetry.lightpanda.io"; const URL = "https://telemetry.lightpanda.io";
const MAX_BATCH_SIZE = 20;
pub const LightPanda = struct { pub const LightPanda = struct {
uri: std.Uri, uri: std.Uri,
@@ -73,18 +74,18 @@ pub const LightPanda = struct {
fn run(self: *LightPanda) void { fn run(self: *LightPanda) void {
const client = self.client; const client = self.client;
var arr: std.ArrayListUnmanaged(u8) = .{}; var arr: std.ArrayListUnmanaged(u8) = .{};
defer arr.deinit(self.allocator); defer arr.deinit(self.allocator);
var batch: [MAX_BATCH_SIZE]LightPandaEvent = undefined;
self.mutex.lock(); self.mutex.lock();
while (true) { while (true) {
while (self.pending.popFirst()) |node| { while (self.pending.first != null) {
const b = self.collectBatch(&batch);
self.mutex.unlock(); self.mutex.unlock();
self.postEvent(&node.data, client, &arr) catch |err| { self.postEvent(b, client, &arr) catch |err| {
log.warn("Telementry reporting error: {}", .{err}); log.warn("Telementry reporting error: {}", .{err});
}; };
self.mutex.lock(); self.mutex.lock();
self.node_pool.destroy(node);
} }
if (self.running == false) { if (self.running == false) {
return; 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(); 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; var response_header_buffer: [2048]u8 = undefined;
const result = try client.fetch(.{ const result = try client.fetch(.{
@@ -109,6 +114,21 @@ pub const LightPanda = struct {
log.warn("server error status: {}", .{result.status}); 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 { const LightPandaEvent = struct {