mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-16 16:28:58 +00:00
use async-client for telemetry
This commit is contained in:
@@ -2,75 +2,176 @@ const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
const ArenAallocator = std.heap.ArenaAllocator;
|
||||
|
||||
const Event = @import("telemetry.zig").Event;
|
||||
const Loop = @import("jsruntime").Loop;
|
||||
const Client = @import("asyncio").Client;
|
||||
|
||||
const log = std.log.scoped(.telemetry);
|
||||
|
||||
const URL = "https://lightpanda.io/browser-stats";
|
||||
const URL = "https://stats.lightpanda.io";
|
||||
|
||||
pub const Lightpanda = struct {
|
||||
pub const LightPanda = struct {
|
||||
uri: std.Uri,
|
||||
arena: ArenAallocator,
|
||||
client: std.http.Client,
|
||||
headers: [1]std.http.Header,
|
||||
io: Client.IO,
|
||||
client: Client,
|
||||
allocator: Allocator,
|
||||
sending_pool: std.heap.MemoryPool(Sending),
|
||||
client_context_pool: std.heap.MemoryPool(Client.Ctx),
|
||||
|
||||
pub fn init(allocator: Allocator) !Lightpanda {
|
||||
pub fn init(allocator: Allocator, loop: *Loop) !LightPanda {
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.io = Client.IO.init(loop),
|
||||
.client = .{ .allocator = allocator },
|
||||
.arena = std.heap.ArenaAllocator.init(allocator),
|
||||
.uri = std.Uri.parse(URL) catch unreachable,
|
||||
.headers = [1]std.http.Header{
|
||||
.{ .name = "Content-Type", .value = "application/json" },
|
||||
},
|
||||
.sending_pool = std.heap.MemoryPool(Sending).init(allocator),
|
||||
.client_context_pool = std.heap.MemoryPool(Client.Ctx).init(allocator),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Lightpanda) void {
|
||||
self.arena.deinit();
|
||||
pub fn deinit(self: *LightPanda) void {
|
||||
self.client.deinit();
|
||||
self.sending_pool.deinit();
|
||||
self.client_context_pool.deinit();
|
||||
}
|
||||
|
||||
pub fn send(self: *Lightpanda, iid: ?[]const u8, eid: []const u8, events: []Event) !void {
|
||||
_ = self;
|
||||
_ = iid;
|
||||
_ = eid;
|
||||
_ = events;
|
||||
// defer _ = self.arena.reset(.{ .retain_capacity = {} });
|
||||
// const body = try std.json.stringifyAlloc(self.arena.allocator(), PlausibleEvent{ .event = event }, .{});
|
||||
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();
|
||||
|
||||
// var server_headers: [2048]u8 = undefined;
|
||||
// var req = try self.client.open(.POST, self.uri, .{
|
||||
// .redirect_behavior = .not_allowed,
|
||||
// .extra_headers = &self.headers,
|
||||
// .server_header_buffer = &server_headers,
|
||||
// });
|
||||
// req.transfer_encoding = .{ .content_length = body.len };
|
||||
// try req.send();
|
||||
const resp_header_buffer = try arena.allocator().alloc(u8, 4096);
|
||||
const body = try std.json.stringifyAlloc(arena.allocator(), .{
|
||||
.iid = iid,
|
||||
.eid = eid,
|
||||
.event = event,
|
||||
}, .{});
|
||||
|
||||
// try req.writeAll(body);
|
||||
// try req.finish();
|
||||
// try req.wait();
|
||||
const sending = try self.sending_pool.create();
|
||||
errdefer self.sending_pool.destroy(sending);
|
||||
|
||||
// const status = req.response.status;
|
||||
// if (status != .accepted) {
|
||||
// log.warn("telemetry '{s}' event error: {d}", .{ @tagName(event), @intFromEnum(status) });
|
||||
// } else {
|
||||
// log.warn("telemetry '{s}' sent", .{@tagName(event)});
|
||||
// }
|
||||
sending.* = .{
|
||||
.body = body,
|
||||
.arena = arena,
|
||||
.lightpanda = self,
|
||||
.request = try self.client.create(.POST, self.uri, .{
|
||||
.server_header_buffer = resp_header_buffer,
|
||||
}),
|
||||
};
|
||||
errdefer sending.request.deinit();
|
||||
|
||||
const ctx = try self.client_context_pool.create();
|
||||
errdefer self.client_context_pool.destroy(ctx);
|
||||
|
||||
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,
|
||||
);
|
||||
}
|
||||
|
||||
fn handleError(self: *LightPanda, ctx: *Client.Ctx, err: anyerror) anyerror!void {
|
||||
ctx.deinit();
|
||||
self.client_context_pool.destroy(ctx);
|
||||
|
||||
var sending: *Sending = @ptrCast(@alignCast(ctx.userData));
|
||||
sending.deinit();
|
||||
self.sending_pool.destroy(sending);
|
||||
log.info("request failure: {}", .{err});
|
||||
}
|
||||
|
||||
fn onRequestConnect(ctx: *Client.Ctx, res: anyerror!void) anyerror!void {
|
||||
var sending: *Sending = @ptrCast(@alignCast(ctx.userData));
|
||||
res catch |err| return sending.lightpanda.handleError(ctx, err);
|
||||
|
||||
ctx.req.transfer_encoding = .{ .content_length = sending.body.len };
|
||||
return ctx.req.async_send(ctx, onRequestSend) catch |err| {
|
||||
return sending.lightpanda.handleError(ctx, err);
|
||||
};
|
||||
}
|
||||
|
||||
fn onRequestSend(ctx: *Client.Ctx, res: anyerror!void) anyerror!void {
|
||||
var sending: *Sending = @ptrCast(@alignCast(ctx.userData));
|
||||
res catch |err| return sending.lightpanda.handleError(ctx, err);
|
||||
|
||||
return ctx.req.async_writeAll(sending.body, ctx, onRequestWrite) catch |err| {
|
||||
return sending.lightpanda.handleError(ctx, err);
|
||||
};
|
||||
}
|
||||
|
||||
fn onRequestWrite(ctx: *Client.Ctx, res: anyerror!void) anyerror!void {
|
||||
var sending: *Sending = @ptrCast(@alignCast(ctx.userData));
|
||||
res catch |err| return sending.lightpanda.handleError(ctx, err);
|
||||
return ctx.req.async_finish(ctx, onRequestFinish) catch |err| {
|
||||
return sending.lightpanda.handleError(ctx, err);
|
||||
};
|
||||
}
|
||||
|
||||
fn onRequestFinish(ctx: *Client.Ctx, res: anyerror!void) anyerror!void {
|
||||
var sending: *Sending = @ptrCast(@alignCast(ctx.userData));
|
||||
res catch |err| return sending.lightpanda.handleError(ctx, err);
|
||||
return ctx.req.async_wait(ctx, onRequestWait) catch |err| {
|
||||
return sending.lightpanda.handleError(ctx, err);
|
||||
};
|
||||
}
|
||||
|
||||
fn onRequestWait(ctx: *Client.Ctx, res: anyerror!void) anyerror!void {
|
||||
var sending: *Sending = @ptrCast(@alignCast(ctx.userData));
|
||||
res catch |err| return sending.lightpanda.handleError(ctx, err);
|
||||
|
||||
const lightpanda = sending.lightpanda;
|
||||
|
||||
defer {
|
||||
ctx.deinit();
|
||||
lightpanda.client_context_pool.destroy(ctx);
|
||||
|
||||
sending.deinit();
|
||||
lightpanda.sending_pool.destroy(sending);
|
||||
}
|
||||
|
||||
var buffer: [2048]u8 = undefined;
|
||||
const reader = ctx.req.reader();
|
||||
while (true) {
|
||||
const n = reader.read(&buffer) catch 0;
|
||||
if (n == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ctx.req.response.status != .ok) {
|
||||
log.info("invalid response: {d}", .{@intFromEnum(ctx.req.response.status)});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// wraps a telemetry event so that we can serialize it to plausible's event endpoint
|
||||
// const PlausibleEvent = struct {
|
||||
// event: Event,
|
||||
const Sending = struct {
|
||||
body: []const u8,
|
||||
request: Client.Request,
|
||||
lightpanda: *LightPanda,
|
||||
arena: std.heap.ArenaAllocator,
|
||||
|
||||
// pub fn jsonStringify(self: PlausibleEvent, jws: anytype) !void {
|
||||
pub fn deinit(self: *Sending) void {
|
||||
self.arena.deinit();
|
||||
self.request.deinit();
|
||||
}
|
||||
};
|
||||
|
||||
// // 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("name");
|
||||
// try jws.write(@tagName(self.event));
|
||||
// try jws.objectField("url");
|
||||
// try jws.write(EVENT_URL);
|
||||
// try jws.objectField("domain");
|
||||
// try jws.write(DOMAIN_KEY);
|
||||
// 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),
|
||||
@@ -80,11 +181,15 @@ pub const Lightpanda = struct {
|
||||
// };
|
||||
|
||||
// const testing = std.testing;
|
||||
// test "plausible: json event" {
|
||||
// const json = try std.json.stringifyAlloc(testing.allocator, PlausibleEvent{ .event = .{ .run = .{ .mode = .serve, .version = "over 9000!" } } }, .{});
|
||||
// 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(
|
||||
// \\{"name":"run","url":"https://lightpanda.io/browser-stats","domain":"localhost","props":{"version":"over 9000!","mode":"serve"}}
|
||||
// \\{"event":"run","iid""1234","eid":"abc!","props":{"version":"over 9000!","mode":"serve"}}
|
||||
// , json);
|
||||
// }
|
||||
|
||||
@@ -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.*);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user