Skip to content

Agent Mesh

bench: skip non-finite metric samples when parsing exposition output

Anas Khan499f7124cff9
diff --git a/cmd/sam-bench/report_test.go b/cmd/sam-bench/report_test.go
index 456b8e46..bc945a4e 100644
--- a/cmd/sam-bench/report_test.go
+++ b/cmd/sam-bench/report_test.go
@@ -15,6 +15,12 @@
package main
import (
+ "context"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
"strings"
"testing"
@@ -104,6 +110,38 @@ func TestGaugeIgnoresSourcesThatNeverReportedIt(t *testing.T) {
}
}
+func TestWriteObservationWithNonFiniteMetrics(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
+ _, _ = w.Write([]byte("unavailable NaN\npositive +Inf\nnegative -Inf\nfinite 3\n"))
+ }))
+ defer server.Close()
+
+ metrics, err := scrapeAll(context.Background(), []string{server.URL})
+ if err != nil {
+ t.Fatalf("scrapeAll: %v", err)
+ }
+ path := filepath.Join(t.TempDir(), "observation.json")
+ wantReport := &bench.Report{Requests: 1, Succeeded: 1}
+ if err := write(path, observation{Report: wantReport, After: metrics}); err != nil {
+ t.Fatalf("write: %v", err)
+ }
+
+ data, err := os.ReadFile(path)
+ if err != nil {
+ t.Fatalf("ReadFile: %v", err)
+ }
+ var got observation
+ if err := json.Unmarshal(data, &got); err != nil {
+ t.Fatalf("json.Unmarshal: %v", err)
+ }
+ if got.Report == nil || got.Report.Requests != 1 || got.Report.Succeeded != 1 {
+ t.Errorf("report = %+v, want %+v", got.Report, wantReport)
+ }
+ if values := got.After[server.URL]; len(values) != 1 || values["finite"] != 3 {
+ t.Errorf("metrics = %v, want only finite=3", values)
+ }
+}
+
func TestRenderPutsEveryObservationInTheTable(t *testing.T) {
observations := []observation{
{
diff --git a/internal/bench/scrape.go b/internal/bench/scrape.go
index 259a7a1d..b2db5225 100644
--- a/internal/bench/scrape.go
+++ b/internal/bench/scrape.go
@@ -18,6 +18,7 @@ import (
"bufio"
"context"
"fmt"
+ "math"
"net/http"
"sort"
"strconv"
@@ -170,8 +171,8 @@ func parseExposition(r interface{ Read([]byte) (int, error) }) ([]Series, error)
continue
}
value, err := strconv.ParseFloat(fields[0], 64)
- if err != nil {
- // NaN and +Inf are legal values that carry no information here.
+ if err != nil || math.IsNaN(value) || math.IsInf(value, 0) {
+ // NaN and infinity are legal values that carry no information here.
continue
}
diff --git a/internal/bench/scrape_test.go b/internal/bench/scrape_test.go
index d69f5ac7..b3843117 100644
--- a/internal/bench/scrape_test.go
+++ b/internal/bench/scrape_test.go
@@ -16,6 +16,7 @@ package bench
import (
"context"
+ "encoding/json"
"net/http"
"net/http/httptest"
"strings"
@@ -85,6 +86,27 @@ func TestParseExpositionKeepsCommasInsideLabelValues(t *testing.T) {
}
}
+func TestParseExpositionNonFinite(t *testing.T) {
+ series, err := parseExposition(strings.NewReader("unavailable NaN\npositive +Inf\nnegative -Inf\nfinite 3\nzero 0\nfinite_negative -2.5\n"))
+ if err != nil {
+ t.Fatalf("parseExposition: %v", err)
+ }
+
+ metrics := (&Snapshot{Series: series}).Flatten()
+ want := map[string]float64{"finite": 3, "zero": 0, "finite_negative": -2.5}
+ if len(metrics) != len(want) {
+ t.Fatalf("metrics = %v, want %v", metrics, want)
+ }
+ for name, value := range want {
+ if got, ok := metrics[name]; !ok || got != value {
+ t.Errorf("metric %q = %v (found %v), want %v", name, got, ok, value)
+ }
+ }
+ if _, err := json.Marshal(metrics); err != nil {
+ t.Fatalf("json.Marshal: %v", err)
+ }
+}
+
func TestScrapeReadsALiveEndpoint(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("sam_node_requests_in_flight 3\n"))