Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func main() {
rMetrics := chi.NewRouter()
rMetrics.Use(chiMiddleware.Recoverer)
rMetrics.Method(http.MethodGet, "/metrics", metrics.Handler(slogger,
metrics.NewChromeCollector(upstreamMgr),
metrics.NewChromeCollector(upstreamMgr, slogger),
metrics.NewGPUCollector(),
metrics.NewSystemCollector(),
))
Expand Down
12 changes: 9 additions & 3 deletions server/lib/metrics/chrome.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics
import (
"context"
"errors"
"log/slog"
"math"
"strconv"
"strings"
Expand Down Expand Up @@ -78,10 +79,11 @@ type DevToolsUpstream interface {
type ChromeCollector struct {
upstream DevToolsUpstream
histograms []UMAHistogram
log *slog.Logger
}

func NewChromeCollector(upstream DevToolsUpstream) *ChromeCollector {
return &ChromeCollector{upstream: upstream, histograms: DefaultUMAHistograms}
func NewChromeCollector(upstream DevToolsUpstream, log *slog.Logger) *ChromeCollector {
return &ChromeCollector{upstream: upstream, histograms: DefaultUMAHistograms, log: log}
}

func (c *ChromeCollector) Name() string { return "chrome" }
Expand Down Expand Up @@ -109,9 +111,13 @@ func (c *ChromeCollector) Collect(ctx context.Context, w *Writer) error {
w.Sample("kernel_chromium_pages", nil, float64(pages))
}

// A histogram fetch failure must not discard the up/info/pages
// samples already gathered: log it, skip the UMA family for this
// scrape, and report success for the rest.
fetched, err := c.fetchHistograms(ctx, client)
if err != nil {
return err
c.log.Error("chrome histogram fetch failed", "err", err)
return nil
}
w.Metric("kernel_chromium_uma",
"Chrome UMA histogram, cumulative since browser start, re-bucketed onto fixed bounds. Units follow the UMA definition: PageLoad timings are milliseconds, NumInteractions is a count, CLS is a unitless score, Memory.GPU.* is MB.",
Expand Down
15 changes: 11 additions & 4 deletions server/lib/metrics/chrome_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics
import (
"context"
"encoding/json"
"log/slog"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -91,7 +92,7 @@ func TestChromeCollector(t *testing.T) {
srv := fakeCDP(t)
defer srv.Close()

c := NewChromeCollector(staticUpstream("ws" + strings.TrimPrefix(srv.URL, "http")))
c := NewChromeCollector(staticUpstream("ws"+strings.TrimPrefix(srv.URL, "http")), slog.New(slog.DiscardHandler))
w := &Writer{}
require.NoError(t, c.Collect(context.Background(), w))
out := string(w.Bytes())
Expand Down Expand Up @@ -119,18 +120,24 @@ func TestChromeCollector(t *testing.T) {
func TestChromeCollectorDown(t *testing.T) {
// An unreachable browser is not a collector error: the up sample must
// survive the handler's discard-on-error policy.
c := NewChromeCollector(staticUpstream(""))
c := NewChromeCollector(staticUpstream(""), slog.New(slog.DiscardHandler))
w := &Writer{}
require.NoError(t, c.Collect(context.Background(), w))
assert.Contains(t, string(w.Bytes()), "kernel_chromium_up 0\n")
}

func TestChromeCollectorHistogramFailure(t *testing.T) {
// A histogram fetch failure must not take down the whole chrome
// scrape: up/info/pages survive and only the UMA family is skipped.
srv := fakeCDP(t)
defer srv.Close()

c := NewChromeCollector(staticUpstream("ws" + strings.TrimPrefix(srv.URL, "http")))
c := NewChromeCollector(staticUpstream("ws"+strings.TrimPrefix(srv.URL, "http")), slog.New(slog.DiscardHandler))
c.histograms = []UMAHistogram{{Name: "Fail.Me", Bounds: boundsPageLoadMs}}
w := &Writer{}
require.Error(t, c.Collect(context.Background(), w))
require.NoError(t, c.Collect(context.Background(), w))
out := string(w.Bytes())
assert.Contains(t, out, "kernel_chromium_up 1\n")
assert.Contains(t, out, "kernel_chromium_pages 2\n")
assert.NotContains(t, out, "kernel_chromium_uma")
}
5 changes: 5 additions & 0 deletions server/lib/metrics/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ func (c *SystemCollector) Collect(ctx context.Context, w *Writer) error {
w.Sample("kernel_vm_disk_free_bytes", []Label{{"mount", c.rootPath}}, float64(fs.Bavail)*bsize)
}

// The /proc reads above are microseconds; the PID walk is the only
// part worth gating on an expired scrape deadline.
if ctx.Err() != nil {
return ctx.Err()
}
procs, rssBytes := c.chromiumProcesses()
w.Metric("kernel_chromium_processes", "Number of running Chromium processes (browser, renderers, utilities).", "gauge")
w.Sample("kernel_chromium_processes", nil, float64(procs))
Expand Down
Loading