mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 15:13:28 +00:00
fix telemetry, link libc and libcpp
This commit is contained in:
@@ -53,6 +53,8 @@ pub fn build(b: *Build) !void {
|
|||||||
.root_source_file = b.path("src/main.zig"),
|
.root_source_file = b.path("src/main.zig"),
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
|
.link_libc = true,
|
||||||
|
.link_libcpp = true,
|
||||||
});
|
});
|
||||||
try addDependencies(b, lightpanda_module, opts);
|
try addDependencies(b, lightpanda_module, opts);
|
||||||
|
|
||||||
|
|||||||
@@ -14,16 +14,14 @@ const URL = "https://telemetry.lightpanda.io";
|
|||||||
const MAX_BATCH_SIZE = 20;
|
const MAX_BATCH_SIZE = 20;
|
||||||
|
|
||||||
pub const LightPanda = struct {
|
pub const LightPanda = struct {
|
||||||
pending: List,
|
|
||||||
running: bool,
|
running: bool,
|
||||||
thread: ?std.Thread,
|
thread: ?std.Thread,
|
||||||
allocator: Allocator,
|
allocator: Allocator,
|
||||||
mutex: std.Thread.Mutex,
|
mutex: std.Thread.Mutex,
|
||||||
cond: Thread.Condition,
|
cond: Thread.Condition,
|
||||||
connection: Http.Connection,
|
connection: Http.Connection,
|
||||||
node_pool: std.heap.MemoryPool(List.Node),
|
pending: std.DoublyLinkedList,
|
||||||
|
mem_pool: std.heap.MemoryPool(LightPandaEvent),
|
||||||
const List = std.DoublyLinkedList(LightPandaEvent);
|
|
||||||
|
|
||||||
pub fn init(app: *App) !LightPanda {
|
pub fn init(app: *App) !LightPanda {
|
||||||
const connection = try app.http.newConnection();
|
const connection = try app.http.newConnection();
|
||||||
@@ -41,7 +39,7 @@ pub const LightPanda = struct {
|
|||||||
.running = true,
|
.running = true,
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.connection = connection,
|
.connection = connection,
|
||||||
.node_pool = std.heap.MemoryPool(List.Node).init(allocator),
|
.mem_pool = std.heap.MemoryPool(LightPandaEvent).init(allocator),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,15 +51,17 @@ pub const LightPanda = struct {
|
|||||||
self.cond.signal();
|
self.cond.signal();
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
self.node_pool.deinit();
|
self.mem_pool.deinit();
|
||||||
self.connection.deinit();
|
self.connection.deinit();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send(self: *LightPanda, iid: ?[]const u8, run_mode: App.RunMode, raw_event: telemetry.Event) !void {
|
pub fn send(self: *LightPanda, iid: ?[]const u8, run_mode: App.RunMode, raw_event: telemetry.Event) !void {
|
||||||
const event = LightPandaEvent{
|
const event = try self.mem_pool.create();
|
||||||
|
event.* = .{
|
||||||
.iid = iid,
|
.iid = iid,
|
||||||
.mode = run_mode,
|
.mode = run_mode,
|
||||||
.event = raw_event,
|
.event = raw_event,
|
||||||
|
.node = .{},
|
||||||
};
|
};
|
||||||
|
|
||||||
self.mutex.lock();
|
self.mutex.lock();
|
||||||
@@ -70,24 +70,20 @@ pub const LightPanda = struct {
|
|||||||
self.thread = try std.Thread.spawn(.{}, run, .{self});
|
self.thread = try std.Thread.spawn(.{}, run, .{self});
|
||||||
}
|
}
|
||||||
|
|
||||||
const node = try self.node_pool.create();
|
self.pending.append(&event.node);
|
||||||
errdefer self.node_pool.destroy(node);
|
|
||||||
node.data = event;
|
|
||||||
self.pending.append(node);
|
|
||||||
self.cond.signal();
|
self.cond.signal();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(self: *LightPanda) void {
|
fn run(self: *LightPanda) void {
|
||||||
var arr: std.ArrayListUnmanaged(u8) = .{};
|
var aw = std.Io.Writer.Allocating.init(self.allocator);
|
||||||
defer arr.deinit(self.allocator);
|
|
||||||
|
|
||||||
var batch: [MAX_BATCH_SIZE]LightPandaEvent = undefined;
|
var batch: [MAX_BATCH_SIZE]*LightPandaEvent = undefined;
|
||||||
self.mutex.lock();
|
self.mutex.lock();
|
||||||
while (true) {
|
while (true) {
|
||||||
while (self.pending.first != null) {
|
while (self.pending.first != null) {
|
||||||
const b = self.collectBatch(&batch);
|
const b = self.collectBatch(&batch);
|
||||||
self.mutex.unlock();
|
self.mutex.unlock();
|
||||||
self.postEvent(b, &arr) catch |err| {
|
self.postEvent(b, &aw) catch |err| {
|
||||||
log.warn(.telemetry, "post error", .{ .err = err });
|
log.warn(.telemetry, "post error", .{ .err = err });
|
||||||
};
|
};
|
||||||
self.mutex.lock();
|
self.mutex.lock();
|
||||||
@@ -99,15 +95,19 @@ pub const LightPanda = struct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn postEvent(self: *const LightPanda, events: []LightPandaEvent, arr: *std.ArrayListUnmanaged(u8)) !void {
|
fn postEvent(self: *LightPanda, events: []*LightPandaEvent, aw: *std.Io.Writer.Allocating) !void {
|
||||||
defer arr.clearRetainingCapacity();
|
defer for (events) |e| {
|
||||||
var writer = arr.writer(self.allocator);
|
self.mem_pool.destroy(e);
|
||||||
|
};
|
||||||
|
|
||||||
|
defer aw.clearRetainingCapacity();
|
||||||
for (events) |event| {
|
for (events) |event| {
|
||||||
try std.json.stringify(event, .{ .emit_null_optional_fields = false }, writer);
|
try std.json.Stringify.value(event, .{ .emit_null_optional_fields = false }, &aw.writer);
|
||||||
try writer.writeByte('\n');
|
try aw.writer.writeByte('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
try self.connection.setBody(arr.items);
|
try self.connection.setBody(aw.written());
|
||||||
|
std.debug.print("{s}\n", .{aw.written()});
|
||||||
const status = try self.connection.request();
|
const status = try self.connection.request();
|
||||||
|
|
||||||
if (status != 200) {
|
if (status != 200) {
|
||||||
@@ -115,13 +115,10 @@ pub const LightPanda = struct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collectBatch(self: *LightPanda, into: []LightPandaEvent) []LightPandaEvent {
|
fn collectBatch(self: *LightPanda, into: []*LightPandaEvent) []*LightPandaEvent {
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
const node_pool = &self.node_pool;
|
|
||||||
while (self.pending.popFirst()) |node| {
|
while (self.pending.popFirst()) |node| {
|
||||||
into[i] = node.data;
|
into[i] = @fieldParentPtr("node", node);
|
||||||
node_pool.destroy(node);
|
|
||||||
|
|
||||||
i += 1;
|
i += 1;
|
||||||
if (i == MAX_BATCH_SIZE) {
|
if (i == MAX_BATCH_SIZE) {
|
||||||
break;
|
break;
|
||||||
@@ -135,6 +132,7 @@ const LightPandaEvent = struct {
|
|||||||
iid: ?[]const u8,
|
iid: ?[]const u8,
|
||||||
mode: App.RunMode,
|
mode: App.RunMode,
|
||||||
event: telemetry.Event,
|
event: telemetry.Event,
|
||||||
|
node: std.DoublyLinkedList.Node,
|
||||||
|
|
||||||
pub fn jsonStringify(self: *const LightPandaEvent, writer: anytype) !void {
|
pub fn jsonStringify(self: *const LightPandaEvent, writer: anytype) !void {
|
||||||
try writer.beginObject();
|
try writer.beginObject();
|
||||||
|
|||||||
Reference in New Issue
Block a user