flatten events, include aarch + os, remove eid

This commit is contained in:
Karl Seguin
2025-03-05 22:57:41 +08:00
parent 6b83281539
commit cd33a089d1
6 changed files with 124 additions and 199 deletions

View File

@@ -1,196 +1,127 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const ArenAallocator = std.heap.ArenaAllocator;
const builtin = @import("builtin");
const build_info = @import("build_info");
const Loop = @import("jsruntime").Loop;
const Client = @import("asyncio").Client;
const Thread = std.Thread;
const Allocator = std.mem.Allocator;
const telemetry = @import("telemetry.zig");
const RunMode = @import("../app.zig").RunMode;
const log = std.log.scoped(.telemetry);
const URL = "https://telemetry.lightpanda.io/";
const URL = "https://telemetry.lightpanda.io";
pub const LightPanda = struct {
uri: std.Uri,
io: Client.IO,
client: Client,
pending: List,
running: bool,
thread: ?std.Thread,
allocator: Allocator,
sending_pool: std.heap.MemoryPool(Sending),
client_context_pool: std.heap.MemoryPool(Client.Ctx),
mutex: std.Thread.Mutex,
cond: Thread.Condition,
node_pool: std.heap.MemoryPool(List.Node),
pub fn init(allocator: Allocator, loop: *Loop) !LightPanda {
const List = std.DoublyLinkedList(LightPandaEvent);
pub fn init(allocator: Allocator) !LightPanda {
return .{
.cond = .{},
.mutex = .{},
.pending = .{},
.thread = null,
.running = true,
.allocator = allocator,
.io = Client.IO.init(loop),
.client = .{ .allocator = allocator },
.uri = std.Uri.parse(URL) catch unreachable,
.sending_pool = std.heap.MemoryPool(Sending).init(allocator),
.client_context_pool = std.heap.MemoryPool(Client.Ctx).init(allocator),
.node_pool = std.heap.MemoryPool(List.Node).init(allocator),
};
}
pub fn deinit(self: *LightPanda) void {
self.client.deinit();
self.sending_pool.deinit();
self.client_context_pool.deinit();
if (self.thread) |*thread| {
self.mutex.lock();
self.running = false;
self.mutex.unlock();
self.cond.signal();
thread.join();
}
self.node_pool.deinit();
}
pub fn send(self: *LightPanda, iid: ?[]const u8, eid: []const u8, event: anytype) !void {
var arena = std.heap.ArenaAllocator.init(self.allocator);
errdefer arena.deinit();
const resp_header_buffer = try arena.allocator().alloc(u8, 4096);
const body = try std.json.stringifyAlloc(arena.allocator(), .{
pub fn send(self: *LightPanda, iid: ?[]const u8, run_mode: RunMode, raw_event: telemetry.Event) !void {
const event = LightPandaEvent{
.iid = iid,
.eid = eid,
.event = event,
}, .{});
const sending = try self.sending_pool.create();
errdefer self.sending_pool.destroy(sending);
sending.* = .{
.body = body,
.arena = arena,
.lightpanda = self,
.request = try self.client.create(.POST, self.uri, .{
.server_header_buffer = resp_header_buffer,
}),
.driver = if (std.meta.activeTag(raw_event) == .navigate) "cdp" else null,
.mode = run_mode,
.os = builtin.os.tag,
.arch = builtin.cpu.arch,
.version = build_info.git_commit,
.event = @tagName(std.meta.activeTag(raw_event)),
};
errdefer sending.request.deinit();
const ctx = try self.client_context_pool.create();
errdefer self.client_context_pool.destroy(ctx);
self.mutex.lock();
defer self.mutex.unlock();
if (self.thread == null) {
self.thread = try std.Thread.spawn(.{}, run, .{self});
}
ctx.* = try Client.Ctx.init(&self.io, &sending.request);
ctx.userData = sending;
try self.client.async_open(
.POST,
self.uri,
.{ .server_header_buffer = resp_header_buffer },
ctx,
onRequestConnect,
);
const node = try self.node_pool.create();
errdefer self.node_pool.destroy(node);
node.data = event;
self.pending.append(node);
self.cond.signal();
}
fn handleError(sending: *Sending, ctx: *Client.Ctx, err: anyerror) anyerror!void {
const lightpanda = sending.lightpanda;
ctx.deinit();
lightpanda.client_context_pool.destroy(ctx);
sending.deinit();
lightpanda.sending_pool.destroy(sending);
log.info("request failure: {}", .{err});
}
fn onRequestConnect(ctx: *Client.Ctx, res: anyerror!void) anyerror!void {
const sending: *Sending = @ptrCast(@alignCast(ctx.userData));
res catch |err| return handleError(sending, ctx, err);
ctx.req.transfer_encoding = .{ .content_length = sending.body.len };
return ctx.req.async_send(ctx, onRequestSend) catch |err| {
return handleError(sending, ctx, err);
};
}
fn onRequestSend(ctx: *Client.Ctx, res: anyerror!void) anyerror!void {
const sending: *Sending = @ptrCast(@alignCast(ctx.userData));
res catch |err| return handleError(sending, ctx, err);
return ctx.req.async_writeAll(sending.body, ctx, onRequestWrite) catch |err| {
return handleError(sending, ctx, err);
};
}
fn onRequestWrite(ctx: *Client.Ctx, res: anyerror!void) anyerror!void {
const sending: *Sending = @ptrCast(@alignCast(ctx.userData));
res catch |err| return handleError(sending, ctx, err);
return ctx.req.async_finish(ctx, onRequestFinish) catch |err| {
return handleError(sending, ctx, err);
};
}
fn onRequestFinish(ctx: *Client.Ctx, res: anyerror!void) anyerror!void {
const sending: *Sending = @ptrCast(@alignCast(ctx.userData));
res catch |err| return handleError(sending, ctx, err);
return ctx.req.async_wait(ctx, onRequestWait) catch |err| {
return handleError(sending, ctx, err);
};
}
fn onRequestWait(ctx: *Client.Ctx, res: anyerror!void) anyerror!void {
const sending: *Sending = @ptrCast(@alignCast(ctx.userData));
res catch |err| return handleError(sending, ctx, err);
const lightpanda = sending.lightpanda;
fn run(self: *LightPanda) void {
var arr: std.ArrayListUnmanaged(u8) = .{};
var client = std.http.Client{ .allocator = self.allocator };
defer {
ctx.deinit();
lightpanda.client_context_pool.destroy(ctx);
sending.deinit();
lightpanda.sending_pool.destroy(sending);
arr.deinit(self.allocator);
client.deinit();
}
var buffer: [2048]u8 = undefined;
const reader = ctx.req.reader();
self.mutex.lock();
while (true) {
const n = reader.read(&buffer) catch 0;
if (n == 0) {
break;
while (self.pending.popFirst()) |node| {
self.mutex.unlock();
self.postEvent(&node.data, &client, &arr) catch |err| {
log.warn("Telementry reporting error: {}", .{err});
};
self.mutex.lock();
self.node_pool.destroy(node);
}
if (self.running == false) {
return;
}
self.cond.wait(&self.mutex);
}
if (ctx.req.response.status != .ok) {
log.info("invalid response: {d}", .{@intFromEnum(ctx.req.response.status)});
}
fn postEvent(self: *const LightPanda, event: *const 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 response_header_buffer: [2048]u8 = undefined;
const result = try client.fetch(.{
.method = .POST,
.payload = arr.items,
.response_storage = .ignore,
.location = .{ .uri = self.uri },
.server_header_buffer = &response_header_buffer,
});
if (result.status != .ok) {
log.warn("server error status: {}", .{result.status});
}
}
};
const Sending = struct {
body: []const u8,
request: Client.Request,
lightpanda: *LightPanda,
arena: std.heap.ArenaAllocator,
pub fn deinit(self: *Sending) void {
self.arena.deinit();
self.request.deinit();
}
const LightPandaEvent = struct {
iid: ?[]const u8,
mode: RunMode,
driver: ?[]const u8,
os: std.Target.Os.Tag,
arch: std.Target.Cpu.Arch,
version: []const u8,
event: []const u8,
};
// // wraps a telemetry event so that we can serialize it to plausible's event endpoint
// const EventWrap = struct {
// iid: ?[]const u8,
// eid: []const u8,
// event: *const Event,
// pub fn jsonStringify(self: *const EventWrap, jws: anytype) !void {
// try jws.beginObject();
// try jws.objectField("iid");
// try jws.write(self.iid);
// try jws.objectField("eid");
// try jws.write(self.eid);
// try jws.objectField("event");
// try jws.write(@tagName(self.event.*));
// try jws.objectField("props");
// switch (self.event) {
// inline else => |props| try jws.write(props),
// }
// try jws.endObject();
// }
// };
// const testing = std.testing;
// test "telemetry: lightpanda json event" {
// const json = try std.json.stringifyAlloc(testing.allocator, EventWrap{
// .iid = "1234",
// .eid = "abc!",
// .event = .{ .run = .{ .mode = .serve, .version = "over 9000!" } }
// }, .{});
// defer testing.allocator.free(json);
// try testing.expectEqualStrings(
// \\{"event":"run","iid""1234","eid":"abc!","props":{"version":"over 9000!","mode":"serve"}}
// , json);
// }