| 1 |
From: Daniel Swarbrick <[email protected]> |
| 2 |
Date: Thu, 22 Jun 2023 17:08:06 +0200 |
| 3 |
Subject: Fix prometheus tests |
| 4 |
|
| 5 |
Since protobuf APIv2, the text serialization format is not deterministic, |
| 6 |
and thus cannot be relied upon for overly brittle "golden tests". The |
| 7 |
workaround is to unmarshal the golden test "want" string, and compare it |
| 8 |
via proto.Equal(). |
| 9 |
--- |
| 10 |
prometheusmetrics_test.go | 66 ++++++++++++++++++++++++++--------------------- |
| 11 |
1 file changed, 37 insertions(+), 29 deletions(-) |
| 12 |
|
| 13 |
diff --git a/prometheusmetrics_test.go b/prometheusmetrics_test.go |
| 14 |
index 31f9a9d..7d90c43 100644 |
| 15 |
--- a/prometheusmetrics_test.go |
| 16 |
+++ b/prometheusmetrics_test.go |
| 17 |
@@ -6,8 +6,10 @@ import ( |
| 18 |
"time" |
| 19 |
|
| 20 |
"github.com/prometheus/client_golang/prometheus" |
| 21 |
+ dto "github.com/prometheus/client_model/go" |
| 22 |
"github.com/rcrowley/go-metrics" |
| 23 |
- "github.com/stretchr/testify/assert" |
| 24 |
+ "google.golang.org/protobuf/encoding/prototext" |
| 25 |
+ "google.golang.org/protobuf/proto" |
| 26 |
) |
| 27 |
|
| 28 |
func TestPrometheusRegistration(t *testing.T) { |
| 29 |
@@ -68,9 +70,8 @@ func TestPrometheusCounterGetUpdated(t *testing.T) { |
| 30 |
cntr.Inc(13) |
| 31 |
time.Sleep(5 * time.Second) |
| 32 |
metrics, _ := prometheusRegistry.Gather() |
| 33 |
- serialized := fmt.Sprint(metrics[0]) |
| 34 |
expected := fmt.Sprintf("name:\"test_subsys_counter\" help:\"counter\" type:GAUGE metric:<gauge:<value:%d > > ", cntr.Count()) |
| 35 |
- if serialized != expected { |
| 36 |
+ if err := compareProtoAndMetricFamily(expected, metrics[0]); err != nil { |
| 37 |
t.Fatalf("Go-metrics value and prometheus metrics value do not match") |
| 38 |
} |
| 39 |
} |
| 40 |
@@ -89,9 +90,8 @@ func TestPrometheusGaugeGetUpdated(t *testing.T) { |
| 41 |
if len(metrics) == 0 { |
| 42 |
t.Fatalf("prometheus was unable to register the metric") |
| 43 |
} |
| 44 |
- serialized := fmt.Sprint(metrics[0]) |
| 45 |
expected := fmt.Sprintf("name:\"test_subsys_gauge\" help:\"gauge\" type:GAUGE metric:<gauge:<value:%d > > ", gm.Value()) |
| 46 |
- if serialized != expected { |
| 47 |
+ if err := compareProtoAndMetricFamily(expected, metrics[0]); err != nil { |
| 48 |
t.Fatalf("Go-metrics value and prometheus metrics value do not match") |
| 49 |
} |
| 50 |
} |
| 51 |
@@ -122,12 +122,9 @@ func TestPrometheusMeterGetUpdated(t *testing.T) { |
| 52 |
snap.Count(), snap.Rate1(), snap.Rate15(), snap.Rate5(), snap.RateMean(), |
| 53 |
) |
| 54 |
|
| 55 |
- assert.Equal( |
| 56 |
- t, |
| 57 |
- expected, |
| 58 |
- fmt.Sprint(metrics[0]), |
| 59 |
- "Go-metrics value and prometheus metrics value do not match", |
| 60 |
- ) |
| 61 |
+ if err := compareProtoAndMetricFamily(expected, metrics[0]); err != nil { |
| 62 |
+ t.Fatalf("Go-metrics value and prometheus metrics value do not match") |
| 63 |
+ } |
| 64 |
} |
| 65 |
|
| 66 |
func TestPrometheusHistogramGetUpdated(t *testing.T) { |
| 67 |
@@ -149,23 +146,34 @@ func TestPrometheusHistogramGetUpdated(t *testing.T) { |
| 68 |
time.Sleep(5 * time.Second) |
| 69 |
metrics, _ := prometheusRegistry.Gather() |
| 70 |
|
| 71 |
- assert.Equal( |
| 72 |
- t, |
| 73 |
- fmt.Sprintf( |
| 74 |
- "name:\"test_subsys_metric\" help:\"metric\" type:GAUGE metric:<label:<name:\"type\" value:\"count\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"max\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"mean\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"min\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"perc75\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"perc95\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"perc99\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"perc999\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"stddev\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"sum\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"variance\" > gauge:<value:%v > > ", |
| 75 |
- gm.Count(), |
| 76 |
- gm.Max(), |
| 77 |
- gm.Mean(), |
| 78 |
- gm.Min(), |
| 79 |
- gm.Percentile(75), |
| 80 |
- gm.Percentile(95), |
| 81 |
- gm.Percentile(99), |
| 82 |
- gm.Percentile(999), |
| 83 |
- gm.StdDev(), |
| 84 |
- gm.Sum(), |
| 85 |
- gm.Variance(), |
| 86 |
- ), |
| 87 |
- fmt.Sprint(metrics[0]), |
| 88 |
- "Go-metrics value and prometheus metrics value do not match", |
| 89 |
+ expected := fmt.Sprintf( |
| 90 |
+ "name:\"test_subsys_metric\" help:\"metric\" type:GAUGE metric:<label:<name:\"type\" value:\"count\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"max\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"mean\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"min\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"perc75\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"perc95\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"perc99\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"perc999\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"stddev\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"sum\" > gauge:<value:%v > > metric:<label:<name:\"type\" value:\"variance\" > gauge:<value:%v > > ", |
| 91 |
+ gm.Count(), |
| 92 |
+ gm.Max(), |
| 93 |
+ gm.Mean(), |
| 94 |
+ gm.Min(), |
| 95 |
+ gm.Percentile(75), |
| 96 |
+ gm.Percentile(95), |
| 97 |
+ gm.Percentile(99), |
| 98 |
+ gm.Percentile(999), |
| 99 |
+ gm.StdDev(), |
| 100 |
+ gm.Sum(), |
| 101 |
+ gm.Variance(), |
| 102 |
) |
| 103 |
+ |
| 104 |
+ if err := compareProtoAndMetricFamily(expected, metrics[0]); err != nil { |
| 105 |
+ t.Fatalf("Go-metrics value and prometheus metrics value do not match") |
| 106 |
+ } |
| 107 |
+} |
| 108 |
+ |
| 109 |
+// compareProtoAndMetricFamily compares a text proto to a MetricFamily. |
| 110 |
+func compareProtoAndMetricFamily(wantText string, got *dto.MetricFamily) error { |
| 111 |
+ want := &dto.MetricFamily{} |
| 112 |
+ if err := prototext.Unmarshal([]byte(wantText), want); err != nil { |
| 113 |
+ return fmt.Errorf("unexpected error unmarshaling MetricFamily text %v", wantText) |
| 114 |
+ } |
| 115 |
+ if !proto.Equal(want, got) { |
| 116 |
+ return fmt.Errorf("wanted MetricFamily %v, got %v.", want, got) |
| 117 |
+ } |
| 118 |
+ return nil |
| 119 |
} |