Skip to content

Lightpanda: the headless browser designed for AI and automation

`metrics`: make adblocker verdicts measurable

Halil Durake696a29da7d5
diff --git a/src/Metrics.zig b/src/Metrics.zig
index 0743d3a9c..120299cec 100644
--- a/src/Metrics.zig
+++ b/src/Metrics.zig
@@ -94,6 +94,8 @@ robots_access: CounterEnum("result", enum { allow, deny }) = .{},
cors_check: CounterEnum("result", enum { same_origin, no_cors, simple, preflight }) = .{},
cors_preflight: CounterEnum("result", enum { allowed, blocked }) = .{},
cors_response: CounterEnum("result", enum { allowed, blocked }) = .{},
+adblock_verdicts: CounterEnum("verdict", @import("network/adblock/AdBlocker.zig").Verdict) = .{},
+adblock_rules: GaugeEnum("state", enum { loaded, skipped }) = .{},
// Emitted as each metric's "# HELP" line. A field without an entry is a
// compile error.
@@ -129,6 +131,8 @@ const help = .{
.cors_check = "CORS initial classification: same_origin/no_cors need no CORS handling, simple needs response validation only, preflight needs an OPTIONS round-trip first",
.cors_preflight = "CORS preflight (OPTIONS) results, one per request that required one",
.cors_response = "CORS actual-response validation results",
+ .adblock_verdicts = "Adblocker decisions for evaluated requests, by verdict (none = no filter matched)",
+ .adblock_rules = "Filter-list rules by fate: loaded into the matcher, or skipped as unsupported",
};
pub fn write(self: *const Metrics, writer: *std.Io.Writer) void {
@@ -218,6 +222,10 @@ fn GaugeEnum(comptime label: []const u8, comptime T: type) type {
self.values.getPtr(tag).decr();
}
+ pub fn add(self: *Self, tag: T, n: i64) void {
+ self.values.getPtr(tag).add(n);
+ }
+
fn write(self: *const Self, comptime name: []const u8, comptime help_text: []const u8, writer: *std.Io.Writer) !void {
try writer.writeAll("# HELP " ++ name ++ " " ++ help_text ++ "\n" ++ "# TYPE " ++ name ++ " gauge\n");
inline for (comptime std.enums.values(Tag)) |tag| {
diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zig
index b4a83ade0..ea9b35ffd 100644
--- a/src/network/HttpClient.zig
+++ b/src/network/HttpClient.zig
@@ -415,7 +415,9 @@ fn isAdblocked(self: *const Client, transfer: *const Transfer) bool {
adblockSourceUrl(transfer),
&buffers,
) orelse return false;
- return blocker.match(target) == .blocked;
+ const verdict = blocker.match(target);
+ lp.metrics.adblock_verdicts.incr(verdict);
+ return verdict == .blocked;
}
fn adblockSourceUrl(transfer: *const Transfer) ?[]const u8 {
diff --git a/src/network/adblock/AdBlocker.zig b/src/network/adblock/AdBlocker.zig
index 94a8d9dd6..570604e35 100644
--- a/src/network/adblock/AdBlocker.zig
+++ b/src/network/adblock/AdBlocker.zig
@@ -129,6 +129,8 @@ pub fn fromConfig(allocator: Allocator, config: *const Config) !?AdBlocker {
if (adblocker) |*blocker| {
try blocker.build();
+ lp.metrics.adblock_rules.add(.loaded, @intCast(blocker.rules_loaded));
+ lp.metrics.adblock_rules.add(.skipped, @intCast(blocker.rules_skipped));
log.info(.app, "adblock lists loaded", .{
.loaded = blocker.rules_loaded,
.skipped = blocker.rules_skipped,