Use common network runtime for telemetry messages

This commit is contained in:
Nikolay Govorov
2026-03-12 10:17:26 +00:00
parent 8372b45cc5
commit a6d699ad5d
6 changed files with 285 additions and 155 deletions

View File

@@ -2,140 +2,95 @@ const std = @import("std");
const builtin = @import("builtin");
const build_config = @import("build_config");
const Thread = std.Thread;
const Allocator = std.mem.Allocator;
const log = @import("../log.zig");
const App = @import("../App.zig");
const Config = @import("../Config.zig");
const telemetry = @import("telemetry.zig");
const Runtime = @import("../network/Runtime.zig");
const Connection = @import("../network/http.zig").Connection;
const URL = "https://telemetry.lightpanda.io";
const MAX_BATCH_SIZE = 20;
pub const LightPanda = struct {
running: bool,
thread: ?std.Thread,
allocator: Allocator,
mutex: std.Thread.Mutex,
cond: Thread.Condition,
connection: Connection,
config: *const Config,
pending: std.DoublyLinkedList,
mem_pool: std.heap.MemoryPool(LightPandaEvent),
const LightPanda = @This();
pub fn init(app: *App) !LightPanda {
const connection = try app.network.newConnection();
errdefer connection.deinit();
allocator: Allocator,
runtime: *Runtime,
mutex: std.Thread.Mutex = .{},
try connection.setURL(URL);
try connection.setMethod(.POST);
pcount: usize = 0,
pending: [MAX_BATCH_SIZE * 2]LightPandaEvent = undefined,
const allocator = app.allocator;
return .{
.cond = .{},
.mutex = .{},
.pending = .{},
.thread = null,
.running = true,
.allocator = allocator,
.connection = connection,
.config = app.config,
.mem_pool = std.heap.MemoryPool(LightPandaEvent).init(allocator),
};
}
pub fn init(app: *App) !LightPanda {
return .{
.allocator = app.allocator,
.runtime = &app.network,
};
}
pub fn deinit(self: *LightPanda) void {
if (self.thread) |*thread| {
self.mutex.lock();
self.running = false;
self.mutex.unlock();
self.cond.signal();
thread.join();
}
self.mem_pool.deinit();
self.connection.deinit();
}
pub fn deinit(self: *LightPanda) void {
self.flush();
}
pub fn send(self: *LightPanda, iid: ?[]const u8, run_mode: Config.RunMode, raw_event: telemetry.Event) !void {
const event = try self.mem_pool.create();
event.* = .{
pub fn send(self: *LightPanda, iid: ?[]const u8, run_mode: Config.RunMode, raw_event: telemetry.Event) !void {
const pending_count = blk: {
self.mutex.lock();
defer self.mutex.unlock();
self.pending[self.pcount] = .{
.iid = iid,
.mode = run_mode,
.event = raw_event,
.node = .{},
};
self.pcount += 1;
self.mutex.lock();
defer self.mutex.unlock();
if (self.thread == null) {
self.thread = try std.Thread.spawn(.{}, run, .{self});
}
break :blk self.pcount;
};
self.pending.append(&event.node);
self.cond.signal();
if (pending_count >= MAX_BATCH_SIZE) {
self.flush();
}
}
pub fn flush(self: *LightPanda) void {
self.postEvent() catch |err| {
log.warn(.telemetry, "flush error", .{ .err = err });
};
}
fn postEvent(self: *LightPanda) !void {
var writer = std.Io.Writer.Allocating.init(self.allocator);
defer writer.deinit();
self.mutex.lock();
defer self.mutex.unlock();
const events = self.pending[0..self.pcount];
self.pcount = 0;
if (events.len == 0) return;
for (events) |*event| {
try std.json.Stringify.value(event, .{ .emit_null_optional_fields = false }, &writer.writer);
try writer.writer.writeByte('\n');
}
fn run(self: *LightPanda) void {
var aw = std.Io.Writer.Allocating.init(self.allocator);
defer aw.deinit();
const conn = self.runtime.getConnection() orelse return;
errdefer self.runtime.releaseConnection(conn);
var batch: [MAX_BATCH_SIZE]*LightPandaEvent = undefined;
self.mutex.lock();
while (true) {
while (self.pending.first != null) {
const b = self.collectBatch(&batch);
self.mutex.unlock();
self.postEvent(b, &aw) catch |err| {
log.warn(.telemetry, "post error", .{ .err = err });
};
self.mutex.lock();
}
if (self.running == false) {
return;
}
self.cond.wait(&self.mutex);
}
}
try conn.setURL(URL);
try conn.setMethod(.POST);
try conn.setBody(writer.written());
fn postEvent(self: *LightPanda, events: []*LightPandaEvent, aw: *std.Io.Writer.Allocating) !void {
defer for (events) |e| {
self.mem_pool.destroy(e);
};
defer aw.clearRetainingCapacity();
for (events) |event| {
try std.json.Stringify.value(event, .{ .emit_null_optional_fields = false }, &aw.writer);
try aw.writer.writeByte('\n');
}
try self.connection.setBody(aw.written());
const status = try self.connection.request(&self.config.http_headers);
if (status != 200) {
log.warn(.telemetry, "server error", .{ .status = status });
}
}
fn collectBatch(self: *LightPanda, into: []*LightPandaEvent) []*LightPandaEvent {
var i: usize = 0;
while (self.pending.popFirst()) |node| {
into[i] = @fieldParentPtr("node", node);
i += 1;
if (i == MAX_BATCH_SIZE) {
break;
}
}
return into[0..i];
}
};
self.runtime.submitRequest(conn);
}
const LightPandaEvent = struct {
iid: ?[]const u8,
mode: Config.RunMode,
event: telemetry.Event,
node: std.DoublyLinkedList.Node,
pub fn jsonStringify(self: *const LightPandaEvent, writer: anytype) !void {
try writer.beginObject();

View File

@@ -16,7 +16,7 @@ pub fn isDisabled() bool {
pub const Telemetry = TelemetryT(blk: {
if (builtin.mode == .Debug or builtin.is_test) break :blk NoopProvider;
break :blk @import("lightpanda.zig").LightPanda;
break :blk @import("lightpanda.zig");
});
fn TelemetryT(comptime P: type) type {
@@ -50,6 +50,10 @@ fn TelemetryT(comptime P: type) type {
};
}
pub fn flush(self: *Self) void {
self.provider.flush();
}
pub fn deinit(self: *Self) void {
self.provider.deinit();
}
@@ -118,6 +122,7 @@ const NoopProvider = struct {
fn init(_: *App) !NoopProvider {
return .{};
}
fn flush(_: NoopProvider) void {}
fn deinit(_: NoopProvider) void {}
pub fn send(_: NoopProvider, _: ?[]const u8, _: Config.RunMode, _: Event) !void {}
};
@@ -192,6 +197,7 @@ const MockProvider = struct {
.allocator = app.allocator,
};
}
fn flush(_: *MockProvider) void {}
fn deinit(self: *MockProvider) void {
self.events.deinit(self.allocator);
}