Re-enable telemetry

Start work on supporting navigation events (clicks, form submission).
This commit is contained in:
Karl Seguin
2025-08-01 21:58:24 +08:00
parent 94e8964f69
commit f65a39a3e3
18 changed files with 818 additions and 588 deletions

View File

@@ -7,26 +7,31 @@ const Allocator = std.mem.Allocator;
const log = @import("../log.zig");
const App = @import("../app.zig").App;
const http = @import("../http/client.zig");
const Http = @import("../http/Http.zig");
const telemetry = @import("telemetry.zig");
const URL = "https://telemetry.lightpanda.io";
const MAX_BATCH_SIZE = 20;
pub const LightPanda = struct {
uri: std.Uri,
pending: List,
running: bool,
thread: ?std.Thread,
allocator: Allocator,
mutex: std.Thread.Mutex,
cond: Thread.Condition,
client: *http.Client,
connection: Http.Connection,
node_pool: std.heap.MemoryPool(List.Node),
const List = std.DoublyLinkedList(LightPandaEvent);
pub fn init(app: *App) LightPanda {
pub fn init(app: *App) !LightPanda {
const connection = try app.http.newConnection();
errdefer connection.deinit();
try connection.setURL(URL);
try connection.setMethod(.POST);
const allocator = app.allocator;
return .{
.cond = .{},
@@ -35,8 +40,7 @@ pub const LightPanda = struct {
.thread = null,
.running = true,
.allocator = allocator,
.client = app.http_client,
.uri = std.Uri.parse(URL) catch unreachable,
.connection = connection,
.node_pool = std.heap.MemoryPool(List.Node).init(allocator),
};
}
@@ -50,6 +54,7 @@ pub const LightPanda = struct {
thread.join();
}
self.node_pool.deinit();
self.connection.deinit();
}
pub fn send(self: *LightPanda, iid: ?[]const u8, run_mode: App.RunMode, raw_event: telemetry.Event) !void {
@@ -102,15 +107,11 @@ pub const LightPanda = struct {
try writer.writeByte('\n');
}
var req = try self.client.request(.POST, &self.uri);
defer req.deinit();
req.body = arr.items;
try self.connection.setBody(arr.items);
const status = try self.connection.request();
// drain the response
var res = try req.sendSync(.{});
while (try res.next()) |_| {}
if (res.header.status != 200) {
log.warn(.telemetry, "server error", .{ .status = res.header.status });
if (status != 200) {
log.warn(.telemetry, "server error", .{ .status = status });
}
}

View File

@@ -29,16 +29,19 @@ fn TelemetryT(comptime P: type) type {
const Self = @This();
pub fn init(app: *App, run_mode: App.RunMode) Self {
pub fn init(app: *App, run_mode: App.RunMode) !Self {
const disabled = std.process.hasEnvVarConstant("LIGHTPANDA_DISABLE_TELEMETRY");
if (builtin.mode != .Debug and builtin.is_test == false) {
log.info(.telemetry, "telemetry status", .{ .disabled = disabled });
}
const provider = try P.init(app);
errdefer provider.deinit();
return .{
.disabled = disabled,
.run_mode = run_mode,
.provider = P.init(app),
.provider = provider,
.iid = if (disabled) null else getOrCreateId(app.app_dir_path),
};
}
@@ -134,7 +137,7 @@ pub const Event = union(enum) {
};
const NoopProvider = struct {
fn init(_: *App) NoopProvider {
fn init(_: *App) !NoopProvider {
return .{};
}
fn deinit(_: NoopProvider) void {}
@@ -150,7 +153,7 @@ test "telemetry: disabled by environment" {
defer _ = unsetenv(@constCast("LIGHTPANDA_DISABLE_TELEMETRY"));
const FailingProvider = struct {
fn init(_: *App) @This() {
fn init(_: *App) !@This() {
return .{};
}
fn deinit(_: @This()) void {}
@@ -159,7 +162,7 @@ test "telemetry: disabled by environment" {
}
};
var telemetry = TelemetryT(FailingProvider).init(undefined, .serve);
var telemetry = try TelemetryT(FailingProvider).init(undefined, .serve);
defer telemetry.deinit();
telemetry.record(.{ .run = {} });
}
@@ -186,7 +189,7 @@ test "telemetry: sends event to provider" {
var app = testing.createApp(.{});
defer app.deinit();
var telemetry = TelemetryT(MockProvider).init(app, .serve);
var telemetry = try TelemetryT(MockProvider).init(app, .serve);
defer telemetry.deinit();
const mock = &telemetry.provider;
@@ -206,7 +209,7 @@ const MockProvider = struct {
allocator: Allocator,
events: std.ArrayListUnmanaged(Event),
fn init(app: *App) @This() {
fn init(app: *App) !@This() {
return .{
.iid = null,
.run_mode = null,