Skip to content

Lightpanda: the headless browser designed for AI and automation

metrics: cdp_* -> serve_* capture cdp vs bidi as serve_* labels

Karl Seguindeb1b936a104
diff --git a/src/Metrics.zig b/src/Metrics.zig
index be178323b..ecd6df925 100644
--- a/src/Metrics.zig
+++ b/src/Metrics.zig
@@ -20,12 +20,13 @@ const std = @import("std");
const lp = @import("lightpanda");
const Metrics = @This();
+const Driver = @import("server/Handshake.zig").Driver;
-cdp_connections: Counter = .{},
-cdp_connection_limit: Counter = .{},
-cdp_active_connections: Gauge = .{},
-cdp_commands: Counter = .{},
-cdp_unknown_commands: Counter = .{},
+serve_connections: CounterEnum("driver", Driver) = .{},
+serve_connection_limit: Counter = .{},
+serve_active_connections: GaugeEnum("driver", Driver) = .{},
+serve_commands: CounterEnum("driver", Driver) = .{},
+serve_unknown_commands: CounterEnum("driver", Driver) = .{},
js_heap_limits: Counter = .{},
script_errors: Counter = .{},
js_errors: CounterEnum("kind", enum { js_exception, other }) = .{},
@@ -91,11 +92,11 @@ robots_access: CounterEnum("result", enum { allow, deny }) = .{},
// Emitted as each metric's "# HELP" line. A field without an entry is a
// compile error.
const help = .{
- .cdp_connections = "CDP websocket connections accepted",
- .cdp_connection_limit = "Connections rejected because --cdp-max-connections was reached",
- .cdp_active_connections = "Currently connected CDP clients",
- .cdp_commands = "CDP commands dispatched",
- .cdp_unknown_commands = "CDP commands rejected for an unknown domain or method",
+ .serve_connections = "Websocket connections accepted, by driver protocol",
+ .serve_connection_limit = "Connections rejected because --cdp-max-connections was reached (counted before the handshake, so no driver label)",
+ .serve_active_connections = "Currently connected clients, by driver protocol",
+ .serve_commands = "Commands dispatched, by driver protocol",
+ .serve_unknown_commands = "Commands rejected for an unknown domain, module or method, by driver protocol",
.js_heap_limits = "Pages terminated for reaching the V8 heap limit",
.script_errors = "Scripts that failed to evaluate, e.g. an uncaught top-level exception",
.js_errors = "Uncaught JS errors (script exceptions, listener/callback throws, unhandled promise rejections); kind=js_exception is a thrown JS value, other is an internal failure (e.g. compilation error, terminated execution)",
@@ -104,7 +105,7 @@ const help = .{
.arena_inflight = "Arenas currently checked out of the pool. Above the bucket's max, every acquisition is a miss and every release is discarded",
.arena_memory_bytes = "Backing memory held by pooled arenas, including capacity retained on the free list",
.navigate = "Navigations by initiating frame type",
- .js_heap_physical_bytes = "V8 heap physical size summed over every live isolate (one per CDP connection).",
+ .js_heap_physical_bytes = "V8 heap physical size summed over every live isolate (one per connection).",
.js_heap_size_bytes = "V8 heap physical size, sampled when a page is closed",
.http_requests = "HTTP requests submitted, by dispatch mode (excludes internal requests like robots.txt)",
.http_status = "Final HTTP response status category (redirects counted once, at the final hop)",
diff --git a/src/server/Server.zig b/src/server/Server.zig
index 6ba28752c..97491bb10 100644
--- a/src/server/Server.zig
+++ b/src/server/Server.zig
@@ -601,7 +601,7 @@ fn spawnWorker(self: *Server, socket: posix.socket_t) !void {
while (current < self.max_connections) {
current = self.active_conns.cmpxchgWeak(current, current + 1, .monotonic, .monotonic) orelse break;
} else {
- lp.metrics.cdp_connection_limit.incr();
+ lp.metrics.serve_connection_limit.incr();
return error.MaxConnectionsReached;
}
errdefer _ = self.active_conns.fetchSub(1, .monotonic);
@@ -650,11 +650,9 @@ fn serveCDP(self: *Server, socket: posix.socket_t, active_conns_early_release: *
};
defer cdp.deinit();
- // Only CDP connections are counted: the metric names are part of the
- // scrape contract, and there are no bidi_* equivalents yet.
- lp.metrics.cdp_connections.incr();
- lp.metrics.cdp_active_connections.incr();
- defer lp.metrics.cdp_active_connections.decr();
+ lp.metrics.serve_connections.incr(.cdp);
+ lp.metrics.serve_active_connections.incr(.cdp);
+ defer lp.metrics.serve_active_connections.decr(.cdp);
self.serve(.init(.{ .cdp = cdp }), active_conns_early_release);
}
@@ -673,6 +671,10 @@ fn serveBiDi(self: *Server, socket: posix.socket_t, active_conns_early_release:
};
defer bidi.deinit();
+ lp.metrics.serve_connections.incr(.bidi);
+ lp.metrics.serve_active_connections.incr(.bidi);
+ defer lp.metrics.serve_active_connections.decr(.bidi);
+
self.serve(.init(.{ .bidi = bidi }), active_conns_early_release);
}
@@ -1386,7 +1388,7 @@ test "server: get /metrics" {
try testing.expect(std.mem.startsWith(u8, res, "HTTP/1.1 200 OK\r\n"));
try testing.expect(std.mem.indexOf(u8, res, "Content-Type: text/plain; version=0.0.4") != null);
try testing.expect(std.mem.indexOf(u8, res, "build_info{version=") != null);
- try testing.expect(std.mem.indexOf(u8, res, "# TYPE cdp_connections_total counter") != null);
+ try testing.expect(std.mem.indexOf(u8, res, "# TYPE serve_connections_total counter") != null);
}
fn assertHTTPError(
diff --git a/src/server/bidi/BiDi.zig b/src/server/bidi/BiDi.zig
index 917b27626..f83136508 100644
--- a/src/server/bidi/BiDi.zig
+++ b/src/server/bidi/BiDi.zig
@@ -17,6 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
+const lp = @import("lightpanda");
const App = @import("../../App.zig");
const uuidv4 = @import("../../id.zig").uuidv4;
@@ -160,8 +161,12 @@ pub fn onMessage(self: *BiDi, data: []const u8) anyerror!void {
return self.sendError(id, "invalid argument", "missing command method");
};
+ lp.metrics.serve_commands.incr(.bidi);
self.dispatch(arena, id, method, data) catch |err| switch (err) {
- error.UnknownCommand => try self.sendError(id, "unknown command", method),
+ error.UnknownCommand => {
+ lp.metrics.serve_unknown_commands.incr(.bidi);
+ try self.sendError(id, "unknown command", method);
+ },
// Command.params already answered the client.
error.InvalidParams => {},
else => return err,
diff --git a/src/server/cdp/CDP.zig b/src/server/cdp/CDP.zig
index e8c40df4c..1efc61efb 100644
--- a/src/server/cdp/CDP.zig
+++ b/src/server/cdp/CDP.zig
@@ -206,7 +206,7 @@ pub fn dispatch(self: *CDP, arena: Allocator, sender: Command.Sender, str: []con
// keeping `str` and the backing storage for `input`'s string slices
// alive for the duration of the call.
fn dispatchParsed(self: *CDP, arena: Allocator, sender: Command.Sender, str: []const u8, input: InputMessage) !void {
- lp.metrics.cdp_commands.incr();
+ lp.metrics.serve_commands.incr(.cdp);
var command = Command{
.input = .{
@@ -240,7 +240,7 @@ fn dispatchParsed(self: *CDP, arena: Allocator, sender: Command.Sender, str: []c
dispatchCommand(&command, input.method) catch |err| {
switch (err) {
error.InvalidMethod, error.UnknownDomain, error.UnknownMethod => {
- lp.metrics.cdp_unknown_commands.incr();
+ lp.metrics.serve_unknown_commands.incr(.cdp);
// Chrome's code and wording; drivers feature-detect on it.
const message = std.fmt.allocPrint(command.arena, "'{s}' wasn't found", .{input.method}) catch return err;
command.sendError(-32601, message, .{}) catch return err;