High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
test: the per-instance metrics route that keeps the layer split honest is pinned by nothing, and endpoints.md still documents the superseded proxy behaviour (PIN-177)
Luigi Agosti8809b999b163
diff --git a/docs/endpoints.md b/docs/endpoints.mdindex 24b4b094..9bd8c015 100644--- a/docs/endpoints.md+++ b/docs/endpoints.md@@ -20,7 +20,11 @@ Notes:- in bridge mode, `/health` reports bridge health and tab count- in full server mode, `/health` reports dashboard health, auth state, and instance count-- `/metrics` proxies to the bridge instance (per-instance runtime metrics)+- `/metrics` reports the counters of the process answering it: in full server mode the front+ door's own request counters (auth rejections and unrouted paths included), in bridge mode+ the bridge's. Every response names its `layer`, and the layers are never summed — read one+ instance's counters at `/instances/{id}/metrics`. See+ [reference/metrics.md](reference/metrics.md) for the layer table.- `/api/metrics` in full server mode is a server-level metrics snapshot (aggregated)## Dashboard Auth And Config@@ -709,6 +713,7 @@ GET /instancesGET /instances/{id}GET /instances/tabsGET /instances/metrics+GET /instances/{id}/metricsPOST /instances/startPOST /instances/launchPOST /instances/attachdiff --git a/internal/orchestrator/handlers_instance_metrics_test.go b/internal/orchestrator/handlers_instance_metrics_test.gonew file mode 100644index 00000000..ed954958--- /dev/null+++ b/internal/orchestrator/handlers_instance_metrics_test.go@@ -0,0 +1,111 @@+package orchestrator++import (+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "testing"++ "github.com/pinchtab/pinchtab/internal/bridge"+)++// instanceServingMetrics registers one running instance whose backend answers /metrics with+// the instance layer's own payload, and returns the mux a client would reach plus the paths+// the child was asked for.+func instanceServingMetrics(t *testing.T) (*http.ServeMux, string, *[]string) {+ t.Helper()++ var seen []string+ backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ seen = append(seen, r.URL.Path)+ w.Header().Set("Content-Type", "application/json")+ _, _ = w.Write([]byte(`{"layer":"instance","requests":{"total":3,"failed":0}}`))+ }))+ t.Cleanup(backend.Close)++ o := NewOrchestratorWithRunner(t.TempDir(), &mockRunner{portAvail: true})+ o.client = backend.Client()++ const id = "inst_metrics"+ o.mu.Lock()+ o.instances[id] = &InstanceInternal{+ Instance: bridge.Instance{ID: id, ProfileName: "p1", Status: "running"},+ URL: backend.URL,+ }+ o.mu.Unlock()++ mux := http.NewServeMux()+ o.RegisterHandlers(mux)+ return mux, id, &seen+}++// The layered-metrics fix moved the bare /metrics to the front door, which makes the INSTANCE+// counters unreadable unless this route exists — so deleting its one registration line+// silently reinstates half of the original defect, with the other layer unreachable this+// time. Nothing pinned it: the deletion left internal/server, internal/routes,+// internal/orchestrator and internal/handlers all green.+//+// Registration alone is deliberately NOT what this asserts. The defect being guarded against+// is a route that exists and reaches the wrong place, so presence-of-registration is the one+// property that cannot detect it. This drives the mux and reads the layer out of the body,+// and it also pins the prefix strip: the child must be asked for /metrics, not for the+// caller's /instances/{id}/metrics.+func TestAClientCanReadOneInstancesMetricsThroughTheOrchestrator(t *testing.T) {+ mux, id, seen := instanceServingMetrics(t)++ rec := httptest.NewRecorder()+ mux.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/instances/"+id+"/metrics", nil))++ if rec.Code != http.StatusOK {+ t.Fatalf("GET /instances/%s/metrics = %d, want 200: %s", id, rec.Code, rec.Body.String())+ }++ var payload struct {+ Layer string `json:"layer"`+ Requests struct {+ Total int `json:"total"`+ } `json:"requests"`+ }+ if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {+ t.Fatalf("response is not the instance's JSON: %v: %s", err, rec.Body.String())+ }+ if payload.Layer != "instance" {+ t.Errorf("layer = %q, want %q — the read was answered by some other layer, which is the defect the layer split exists to prevent", payload.Layer, "instance")+ }+ if payload.Requests.Total != 3 {+ t.Errorf("requests.total = %d, want the instance's own 3 — a merged or front-door total means two different things and is unactionable", payload.Requests.Total)+ }++ if len(*seen) != 1 || (*seen)[0] != "/metrics" {+ t.Errorf("the instance was asked for %v, want exactly [/metrics] — the prefix strip is what makes the child answer its own metrics route", *seen)+ }+}++// Conflating the two metrics routes is how the gap hid: the e2e scenario exercises the+// AGGREGATE, which survives deleting the per-instance registration. This states the reason it+// cannot stand in for it. The aggregate does fan out to each running instance's /metrics —+// that is correct, not a leak — but it answers a JSON ARRAY of per-instance memory records,+// so a caller reading it never receives the layer or the request counters, which is what the+// per-instance route exists to deliver.+func TestTheAggregateMetricsRouteCannotStandInForThePerInstanceOne(t *testing.T) {+ mux, _, _ := instanceServingMetrics(t)++ rec := httptest.NewRecorder()+ mux.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/instances/metrics", nil))++ if rec.Code != http.StatusOK {+ t.Fatalf("GET /instances/metrics = %d, want 200: %s", rec.Code, rec.Body.String())+ }++ var records []json.RawMessage+ if err := json.Unmarshal(rec.Body.Bytes(), &records); err != nil {+ t.Fatalf("the aggregate is no longer a JSON array of per-instance records: %v: %s", err, rec.Body.String())+ }++ var perInstance struct {+ Layer string `json:"layer"`+ }+ if err := json.Unmarshal(rec.Body.Bytes(), &perInstance); err == nil && perInstance.Layer != "" {+ t.Errorf("the aggregate now carries layer=%q, so the two routes have become interchangeable and exercising one silently covers for the other", perInstance.Layer)+ }+}