use async-client for telemetry

This commit is contained in:
Karl Seguin
2025-03-03 19:32:18 +08:00
parent 53f6e66c23
commit accf2c0e5e
6 changed files with 201 additions and 109 deletions

View File

@@ -2,17 +2,16 @@ const std = @import("std");
const builtin = @import("builtin");
const Allocator = std.mem.Allocator;
const Loop = @import("jsruntime").Loop;
const uuidv4 = @import("../id.zig").uuidv4;
const log = std.log.scoped(.telemetry);
const BATCH_SIZE = 5;
const BATCH_END = BATCH_SIZE - 1;
const ID_FILE = "lightpanda.id";
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").LightPanda;
});
fn TelemetryT(comptime P: type) type {
@@ -25,14 +24,11 @@ fn TelemetryT(comptime P: type) type {
eid: [36]u8,
provider: P,
// batch of events, pending[0..count] are pending
pending: [BATCH_SIZE]Event,
count: usize,
disabled: bool,
const Self = @This();
pub fn init(allocator: Allocator) Self {
pub fn init(allocator: Allocator, loop: *Loop) Self {
const disabled = std.process.hasEnvVarConstant("LIGHTPANDA_DISABLE_TELEMETRY");
var eid: [36]u8 = undefined;
@@ -41,10 +37,8 @@ fn TelemetryT(comptime P: type) type {
return .{
.iid = if (disabled) null else getOrCreateId(),
.eid = eid,
.count = 0,
.pending = undefined,
.disabled = disabled,
.provider = try P.init(allocator),
.provider = try P.init(allocator, loop),
};
}
@@ -56,19 +50,10 @@ fn TelemetryT(comptime P: type) type {
if (self.disabled) {
return;
}
const count = self.count;
self.pending[count] = event;
if (count < BATCH_END) {
self.count = count + 1;
return;
}
const iid: ?[]const u8 = if (self.iid) |*iid| iid else null;
self.provider.send(iid, &self.eid, &self.pending) catch |err| {
self.provider.send(iid, &self.eid, &event) catch |err| {
log.warn("failed to record event: {}", .{err});
};
self.count = 0;
}
};
}
@@ -99,6 +84,7 @@ fn getOrCreateId() ?[36]u8 {
pub const Event = union(enum) {
run: Run,
navigate: void,
flag: []const u8, // used for testing
const Run = struct {
@@ -113,11 +99,11 @@ pub const Event = union(enum) {
};
const NoopProvider = struct {
fn init(_: Allocator) !NoopProvider {
fn init(_: Allocator, _: *Loop) !NoopProvider {
return .{};
}
fn deinit(_: NoopProvider) void {}
pub fn send(_: NoopProvider, _: ?[]const u8, _: []const u8, _: []Event) !void {}
pub fn send(_: NoopProvider, _: ?[]const u8, _: []const u8, _: anytype) !void {}
};
extern fn setenv(name: [*:0]u8, value: [*:0]u8, override: c_int) c_int;
@@ -128,16 +114,16 @@ test "telemetry: disabled by environment" {
defer _ = unsetenv(@constCast("LIGHTPANDA_DISABLE_TELEMETRY"));
const FailingProvider = struct {
fn init(_: Allocator) !@This() {
fn init(_: Allocator, _: *Loop) !@This() {
return .{};
}
fn deinit(_: @This()) void {}
pub fn send(_: @This(), _: ?[]const u8, _: []const u8, _: []Event) !void {
pub fn send(_: @This(), _: ?[]const u8, _: []const u8, _: anytype) !void {
unreachable;
}
};
var telemetry = TelemetryT(FailingProvider).init(testing.allocator);
var telemetry = TelemetryT(FailingProvider).init(testing.allocator, undefined);
defer telemetry.deinit();
telemetry.record(.{ .run = .{ .mode = .serve, .version = "123" } });
}
@@ -156,32 +142,21 @@ test "telemetry: getOrCreateId" {
try testing.expectEqual(false, std.mem.eql(u8, &id1, &id3));
}
test "telemetry: sends batch" {
test "telemetry: sends event to provider" {
defer std.fs.cwd().deleteFile(ID_FILE) catch {};
std.fs.cwd().deleteFile(ID_FILE) catch {};
var telemetry = TelemetryT(MockProvider).init(testing.allocator);
var telemetry = TelemetryT(MockProvider).init(testing.allocator, undefined);
defer telemetry.deinit();
const mock = &telemetry.provider;
telemetry.record(.{ .flag = "1" });
telemetry.record(.{ .flag = "2" });
telemetry.record(.{ .flag = "3" });
telemetry.record(.{ .flag = "4" });
try testing.expectEqual(0, mock.events.items.len);
telemetry.record(.{ .flag = "5" });
try testing.expectEqual(5, mock.events.items.len);
telemetry.record(.{ .flag = "6" });
telemetry.record(.{ .flag = "7" });
telemetry.record(.{ .flag = "8" });
telemetry.record(.{ .flag = "9" });
try testing.expectEqual(5, mock.events.items.len);
telemetry.record(.{ .flag = "a" });
try testing.expectEqual(10, mock.events.items.len);
try testing.expectEqual(3, mock.events.items.len);
for (mock.events.items, 0..) |event, i| {
try testing.expectEqual(i + 1, std.fmt.parseInt(usize, event.flag, 16));
try testing.expectEqual(i + 1, std.fmt.parseInt(usize, event.flag, 10));
}
}
@@ -191,7 +166,7 @@ const MockProvider = struct {
allocator: Allocator,
events: std.ArrayListUnmanaged(Event),
fn init(allocator: Allocator) !@This() {
fn init(allocator: Allocator, _: *Loop) !@This() {
return .{
.iid = null,
.eid = null,
@@ -202,7 +177,7 @@ const MockProvider = struct {
fn deinit(self: *MockProvider) void {
self.events.deinit(self.allocator);
}
pub fn send(self: *MockProvider, iid: ?[]const u8, eid: []const u8, events: []Event) !void {
pub fn send(self: *MockProvider, iid: ?[]const u8, eid: []const u8, events: *const Event) !void {
if (self.iid == null) {
try testing.expectEqual(null, self.eid);
self.iid = iid.?;
@@ -211,6 +186,6 @@ const MockProvider = struct {
try testing.expectEqualStrings(self.iid.?, iid.?);
try testing.expectEqualStrings(self.eid.?, eid);
}
try self.events.appendSlice(self.allocator, events);
try self.events.append(self.allocator, events.*);
}
};