better otel i think
Build and Publish / Metadata (push) Successful in 2s
Build and Publish / BuildAndDeployAmd64 (push) Successful in 1m26s

This commit is contained in:
mats committed 2026-09-19 19:14:51 +02:00
1 parent 6edbdc340d
commit 8f670dfa2f
16 files changed
+1129 -675

No files matched your search

+47
View File
@@ -509,3 +509,50 @@ func TestHandleLogsIngestRateLimited(t *testing.T) {
t.Fatalf("expected ingest rate limiting to eventually return 429, last code was %d", lastCode)
}
}
func TestHandleVitalsStoresOnePageVitalsRecord(t *testing.T) {
c := New(noopBroadcaster{}, 100, &mockStore{})
body := `{"service":"shop","session":"s1","pageView":"pv1","url":"https://x/y","nav":"navigate",
"metrics":{"LCP":{"value":3100.4,"attribution":{"target":"img.hero","nested":{"x":1}}},
"CLS":{"value":0.05},"BOGUS":{"value":1},"INP":{"value":-3}}}`
req := httptest.NewRequest(http.MethodPost, "/v1/vitals", strings.NewReader(body))
req.Header.Set("Content-Type", "text/plain;charset=UTF-8")
rr := httptest.NewRecorder()
c.HandleVitals(rr, req)
if rr.Code != http.StatusNoContent {
t.Fatalf("status = %d, body %s", rr.Code, rr.Body.String())
}
vb := vitalsBeacon{}
if err := json.Unmarshal([]byte(body), &vb); err != nil {
t.Fatal(err)
}
logs, ok := vb.toLogs(time.Unix(1, 0))
if !ok {
t.Fatal("expected valid logs")
}
rec := logs.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0)
attrs := rec.Attributes()
if v, _ := attrs.Get("vitals.lcp.rating"); v.Str() != "needs-improvement" {
t.Errorf("lcp rating = %q", v.Str())
}
if v, _ := attrs.Get("vitals.cls"); v.Double() != 0.05 {
t.Errorf("cls = %v (must not be rounded)", v.Double())
}
if _, found := attrs.Get("vitals.inp"); found {
t.Error("negative INP must be dropped")
}
if strings.Contains(rec.Body().Str(), "BOGUS") || strings.Contains(rec.Body().Str(), "nested") {
t.Errorf("unknown metric or non-scalar attribution leaked: %s", rec.Body().Str())
}
}
func TestHandleVitalsRejectsEmpty(t *testing.T) {
c := New(noopBroadcaster{}, 100, &mockStore{})
req := httptest.NewRequest(http.MethodPost, "/v1/vitals", strings.NewReader(`{"metrics":{}}`))
rr := httptest.NewRecorder()
c.HandleVitals(rr, req)
if rr.Code != http.StatusBadRequest {
t.Fatalf("status = %d", rr.Code)
}
}