Skip to content

Commit c86e95a

Browse files
author
Arthur Silva Sens
committed
Bump prometheus/client_model
By upgrading prometheus/client_model, several test functions had to be re-written due to 2 breaking changes made in protobuf when parsing messages to text: 1. '<' and '>' characters were replaced with '{' and '}' respectively. 2. The text format is non-deterministic. More information in golang/protobuf#1121 Signed-off-by: Arthur Silva Sens <[email protected]>
1 parent e6d9092 commit c86e95a

11 files changed

+467
-79
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/cespare/xxhash/v2 v2.2.0
88
github.com/davecgh/go-spew v1.1.1
99
github.com/json-iterator/go v1.1.12
10-
github.com/prometheus/client_model v0.3.0
10+
github.com/prometheus/client_model v0.4.0
1111
github.com/prometheus/common v0.42.0
1212
github.com/prometheus/procfs v0.11.1
1313
golang.org/x/sys v0.10.0

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
151151
github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y=
152152
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
153153
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
154-
github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4=
155154
github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w=
155+
github.com/prometheus/client_model v0.4.0 h1:5lQXD3cAg1OXBf4Wq03gTrXHeaV0TQvGfUooCfx1yqY=
156+
github.com/prometheus/client_model v0.4.0/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU=
156157
github.com/prometheus/common v0.37.0/go.mod h1:phzohg0JFMnBEFGxTDbfu3QyL5GI8gTQJFhYO5B3mfA=
157158
github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI1YM=
158159
github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc=

prometheus/counter_test.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,15 @@ func TestCounterAdd(t *testing.T) {
6161
m := &dto.Metric{}
6262
counter.Write(m)
6363

64-
if expected, got := `label:<name:"a" value:"1" > label:<name:"b" value:"2" > counter:<value:67.42 > `, m.String(); expected != got {
65-
t.Errorf("expected %q, got %q", expected, got)
64+
expected := &dto.Metric{
65+
Label: []*dto.LabelPair{
66+
{Name: proto.String("a"), Value: proto.String("1")},
67+
{Name: proto.String("b"), Value: proto.String("2")},
68+
},
69+
Counter: &dto.Counter{Value: proto.Float64(67.42)},
70+
}
71+
if !proto.Equal(expected, m) {
72+
t.Errorf("expected %q, got %q", expected, m)
6673
}
6774
}
6875

@@ -164,8 +171,14 @@ func TestCounterAddInf(t *testing.T) {
164171
m := &dto.Metric{}
165172
counter.Write(m)
166173

167-
if expected, got := `counter:<value:inf > `, m.String(); expected != got {
168-
t.Errorf("expected %q, got %q", expected, got)
174+
expected := &dto.Metric{
175+
Counter: &dto.Counter{
176+
Value: proto.Float64(math.Inf(1)),
177+
},
178+
}
179+
180+
if !proto.Equal(expected, m) {
181+
t.Errorf("expected %q, got %q", expected, m)
169182
}
170183
}
171184

@@ -188,8 +201,14 @@ func TestCounterAddLarge(t *testing.T) {
188201
m := &dto.Metric{}
189202
counter.Write(m)
190203

191-
if expected, got := fmt.Sprintf("counter:<value:%0.16e > ", large), m.String(); expected != got {
192-
t.Errorf("expected %q, got %q", expected, got)
204+
expected := &dto.Metric{
205+
Counter: &dto.Counter{
206+
Value: proto.Float64(large),
207+
},
208+
}
209+
210+
if !proto.Equal(expected, m) {
211+
t.Errorf("expected %q, got %q", expected, m)
193212
}
194213
}
195214

@@ -210,8 +229,14 @@ func TestCounterAddSmall(t *testing.T) {
210229
m := &dto.Metric{}
211230
counter.Write(m)
212231

213-
if expected, got := fmt.Sprintf("counter:<value:%0.0e > ", small), m.String(); expected != got {
214-
t.Errorf("expected %q, got %q", expected, got)
232+
expected := &dto.Metric{
233+
Counter: &dto.Counter{
234+
Value: proto.Float64(small),
235+
},
236+
}
237+
238+
if !proto.Equal(expected, m) {
239+
t.Errorf("expected %q, got %q", expected, m)
215240
}
216241
}
217242

prometheus/example_metricvec_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package prometheus_test
1616
import (
1717
"fmt"
1818

19+
"google.golang.org/protobuf/encoding/protojson"
1920
"google.golang.org/protobuf/proto"
2021

2122
dto "github.com/prometheus/client_model/go"
@@ -126,8 +127,12 @@ func ExampleMetricVec() {
126127
if err != nil || len(metricFamilies) != 1 {
127128
panic("unexpected behavior of custom test registry")
128129
}
129-
fmt.Println(metricFamilies[0].String())
130+
metricAsJSON, err := protojson.Marshal(metricFamilies[0])
131+
if err != nil {
132+
panic("error marshling metricFamily")
133+
}
134+
fmt.Println(removeBrittleJSON(metricAsJSON))
130135

131136
// Output:
132-
// name:"library_version_info" help:"Versions of the libraries used in this binary." type:GAUGE metric:<label:<name:"library" value:"k8s.io/client-go" > label:<name:"version" value:"0.18.8" > gauge:<value:1 > > metric:<label:<name:"library" value:"prometheus/client_golang" > label:<name:"version" value:"1.7.1" > gauge:<value:1 > >
137+
// {"name":"library_version_info","help":"Versions of the libraries used in this binary.","type":"GAUGE","metric":[{"label":[{"name":"library","value":"k8s.io/client-go"},{"name":"version","value":"0.18.8"}],"gauge":{"value":1}},{"label":[{"name":"library","value":"prometheus/client_golang"},{"name":"version","value":"1.7.1"}],"gauge":{"value":1}}]}
133138
}

prometheus/examples_test.go

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
dto "github.com/prometheus/client_model/go"
2727
"github.com/prometheus/common/expfmt"
28+
"google.golang.org/protobuf/encoding/protojson"
2829

2930
"github.com/prometheus/client_golang/prometheus"
3031
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -319,10 +320,15 @@ func ExampleSummary() {
319320
// internally).
320321
metric := &dto.Metric{}
321322
temps.Write(metric)
322-
fmt.Println(metric.String())
323+
324+
metricAsJSON, err := protojson.Marshal(metric)
325+
if err != nil {
326+
panic("error marshling metric")
327+
}
328+
fmt.Println(removeBrittleJSON(metricAsJSON))
323329

324330
// Output:
325-
// summary:<sample_count:1000 sample_sum:29969.50000000001 quantile:<quantile:0.5 value:31.1 > quantile:<quantile:0.9 value:41.3 > quantile:<quantile:0.99 value:41.9 > >
331+
// {"summary":{"sampleCount":"1000","sampleSum":29969.50000000001,"quantile":[{"quantile":0.5,"value":31.1},{"quantile":0.9,"value":41.3},{"quantile":0.99,"value":41.9}]}}
326332
}
327333

328334
func ExampleSummaryVec() {
@@ -354,10 +360,14 @@ func ExampleSummaryVec() {
354360
if err != nil || len(metricFamilies) != 1 {
355361
panic("unexpected behavior of custom test registry")
356362
}
357-
fmt.Println(metricFamilies[0].String())
363+
metricAsJSON, err := protojson.Marshal(metricFamilies[0])
364+
if err != nil {
365+
panic("error marshling metric")
366+
}
367+
fmt.Println(removeBrittleJSON(metricAsJSON))
358368

359369
// Output:
360-
// name:"pond_temperature_celsius" help:"The temperature of the frog pond." type:SUMMARY metric:<label:<name:"species" value:"leiopelma-hochstetteri" > summary:<sample_count:0 sample_sum:0 quantile:<quantile:0.5 value:nan > quantile:<quantile:0.9 value:nan > quantile:<quantile:0.99 value:nan > > > metric:<label:<name:"species" value:"lithobates-catesbeianus" > summary:<sample_count:1000 sample_sum:31956.100000000017 quantile:<quantile:0.5 value:32.4 > quantile:<quantile:0.9 value:41.4 > quantile:<quantile:0.99 value:41.9 > > > metric:<label:<name:"species" value:"litoria-caerulea" > summary:<sample_count:1000 sample_sum:29969.50000000001 quantile:<quantile:0.5 value:31.1 > quantile:<quantile:0.9 value:41.3 > quantile:<quantile:0.99 value:41.9 > > >
370+
// {"name":"pond_temperature_celsius","help":"The temperature of the frog pond.","type":"SUMMARY","metric":[{"label":[{"name":"species","value":"leiopelma-hochstetteri"}],"summary":{"sampleCount":"0","sampleSum":0,"quantile":[{"quantile":0.5,"value":"NaN"},{"quantile":0.9,"value":"NaN"},{"quantile":0.99,"value":"NaN"}]}},{"label":[{"name":"species","value":"lithobates-catesbeianus"}],"summary":{"sampleCount":"1000","sampleSum":31956.100000000017,"quantile":[{"quantile":0.5,"value":32.4},{"quantile":0.9,"value":41.4},{"quantile":0.99,"value":41.9}]}},{"label":[{"name":"species","value":"litoria-caerulea"}],"summary":{"sampleCount":"1000","sampleSum":29969.50000000001,"quantile":[{"quantile":0.5,"value":31.1},{"quantile":0.9,"value":41.3},{"quantile":0.99,"value":41.9}]}}]}
361371
}
362372

363373
func ExampleNewConstSummary() {
@@ -381,10 +391,14 @@ func ExampleNewConstSummary() {
381391
// internally).
382392
metric := &dto.Metric{}
383393
s.Write(metric)
384-
fmt.Println(metric.String())
394+
metricAsJSON, err := protojson.Marshal(metric)
395+
if err != nil {
396+
panic("error marshling metric")
397+
}
398+
fmt.Println(removeBrittleJSON(metricAsJSON))
385399

386400
// Output:
387-
// label:<name:"code" value:"200" > label:<name:"method" value:"get" > label:<name:"owner" value:"example" > summary:<sample_count:4711 sample_sum:403.34 quantile:<quantile:0.5 value:42.3 > quantile:<quantile:0.9 value:323.3 > >
401+
// {"label":[{"name":"code","value":"200"},{"name":"method","value":"get"},{"name":"owner","value":"example"}],"summary":{"sampleCount":"4711","sampleSum":403.34,"quantile":[{"quantile":0.5,"value":42.3},{"quantile":0.9,"value":323.3}]}}
388402
}
389403

390404
func ExampleHistogram() {
@@ -404,10 +418,14 @@ func ExampleHistogram() {
404418
// internally).
405419
metric := &dto.Metric{}
406420
temps.Write(metric)
407-
fmt.Println(metric.String())
421+
metricAsJSON, err := protojson.Marshal(metric)
422+
if err != nil {
423+
panic("error marshling metric")
424+
}
425+
fmt.Println(removeBrittleJSON(metricAsJSON))
408426

409427
// Output:
410-
// histogram:<sample_count:1000 sample_sum:29969.50000000001 bucket:<cumulative_count:192 upper_bound:20 > bucket:<cumulative_count:366 upper_bound:25 > bucket:<cumulative_count:501 upper_bound:30 > bucket:<cumulative_count:638 upper_bound:35 > bucket:<cumulative_count:816 upper_bound:40 > >
428+
// {"histogram":{"sampleCount":"1000","sampleSum":29969.50000000001,"bucket":[{"cumulativeCount":"192","upperBound":20},{"cumulativeCount":"366","upperBound":25},{"cumulativeCount":"501","upperBound":30},{"cumulativeCount":"638","upperBound":35},{"cumulativeCount":"816","upperBound":40}]}}
411429
}
412430

413431
func ExampleNewConstHistogram() {
@@ -431,10 +449,14 @@ func ExampleNewConstHistogram() {
431449
// internally).
432450
metric := &dto.Metric{}
433451
h.Write(metric)
434-
fmt.Println(metric.String())
452+
metricAsJSON, err := protojson.Marshal(metric)
453+
if err != nil {
454+
panic("error marshling metric")
455+
}
456+
fmt.Println(removeBrittleJSON(metricAsJSON))
435457

436458
// Output:
437-
// label:<name:"code" value:"200" > label:<name:"method" value:"get" > label:<name:"owner" value:"example" > histogram:<sample_count:4711 sample_sum:403.34 bucket:<cumulative_count:121 upper_bound:25 > bucket:<cumulative_count:2403 upper_bound:50 > bucket:<cumulative_count:3221 upper_bound:100 > bucket:<cumulative_count:4233 upper_bound:200 > >
459+
// {"label":[{"name":"code","value":"200"},{"name":"method","value":"get"},{"name":"owner","value":"example"}],"histogram":{"sampleCount":"4711","sampleSum":403.34,"bucket":[{"cumulativeCount":"121","upperBound":25},{"cumulativeCount":"2403","upperBound":50},{"cumulativeCount":"3221","upperBound":100},{"cumulativeCount":"4233","upperBound":200}]}}
438460
}
439461

440462
func ExampleNewConstHistogram_WithExemplar() {
@@ -469,10 +491,14 @@ func ExampleNewConstHistogram_WithExemplar() {
469491
// internally).
470492
metric := &dto.Metric{}
471493
h.Write(metric)
472-
fmt.Println(metric.String())
494+
metricAsJSON, err := protojson.Marshal(metric)
495+
if err != nil {
496+
panic("error marshling metric")
497+
}
498+
fmt.Println(removeBrittleJSON(metricAsJSON))
473499

474500
// Output:
475-
// label:<name:"code" value:"200" > label:<name:"method" value:"get" > label:<name:"owner" value:"example" > histogram:<sample_count:4711 sample_sum:403.34 bucket:<cumulative_count:121 upper_bound:25 exemplar:<label:<name:"testName" value:"testVal" > value:24 timestamp:<seconds:1136214245 > > > bucket:<cumulative_count:2403 upper_bound:50 exemplar:<label:<name:"testName" value:"testVal" > value:42 timestamp:<seconds:1136214245 > > > bucket:<cumulative_count:3221 upper_bound:100 exemplar:<label:<name:"testName" value:"testVal" > value:89 timestamp:<seconds:1136214245 > > > bucket:<cumulative_count:4233 upper_bound:200 exemplar:<label:<name:"testName" value:"testVal" > value:157 timestamp:<seconds:1136214245 > > > >
501+
// {"label":[{"name":"code","value":"200"},{"name":"method","value":"get"},{"name":"owner","value":"example"}],"histogram":{"sampleCount":"4711","sampleSum":403.34,"bucket":[{"cumulativeCount":"121","upperBound":25,"exemplar":{"label":[{"name":"testName","value":"testVal"}],"value":24,"timestamp":"2006-01-02T15:04:05Z"}},{"cumulativeCount":"2403","upperBound":50,"exemplar":{"label":[{"name":"testName","value":"testVal"}],"value":42,"timestamp":"2006-01-02T15:04:05Z"}},{"cumulativeCount":"3221","upperBound":100,"exemplar":{"label":[{"name":"testName","value":"testVal"}],"value":89,"timestamp":"2006-01-02T15:04:05Z"}},{"cumulativeCount":"4233","upperBound":200,"exemplar":{"label":[{"name":"testName","value":"testVal"}],"value":157,"timestamp":"2006-01-02T15:04:05Z"}}]}}
476502
}
477503

478504
func ExampleAlreadyRegisteredError() {
@@ -567,7 +593,13 @@ temperature_kelvin 4.5
567593

568594
gathering, err = gatherers.Gather()
569595
if err != nil {
570-
fmt.Println(err)
596+
// We expect error collected metric "temperature_kelvin" { label:<name:"location" value:"outside" > gauge:<value:265.3 > } was collected before with the same name and label values
597+
// We cannot assert it because of https://github.com/golang/protobuf/issues/1121
598+
if strings.HasPrefix(err.Error(), `collected metric "temperature_kelvin" `) {
599+
fmt.Println("Found duplicated metric `temperature_kelvin`")
600+
} else {
601+
fmt.Print(err)
602+
}
571603
}
572604
// Note that still as many metrics as possible are returned:
573605
out.Reset()
@@ -589,7 +621,7 @@ temperature_kelvin 4.5
589621
// temperature_kelvin{location="outside"} 273.14
590622
// temperature_kelvin{location="somewhere else"} 4.5
591623
// ----------
592-
// collected metric "temperature_kelvin" { label:<name:"location" value:"outside" > gauge:<value:265.3 > } was collected before with the same name and label values
624+
// Found duplicated metric `temperature_kelvin`
593625
// # HELP humidity_percent Humidity in %.
594626
// # TYPE humidity_percent gauge
595627
// humidity_percent{location="inside"} 33.2
@@ -625,8 +657,12 @@ func ExampleNewMetricWithTimestamp() {
625657
// internally).
626658
metric := &dto.Metric{}
627659
s.Write(metric)
628-
fmt.Println(metric.String())
660+
metricAsJSON, err := protojson.Marshal(metric)
661+
if err != nil {
662+
panic("error marshling metric")
663+
}
664+
fmt.Println(removeBrittleJSON(metricAsJSON))
629665

630666
// Output:
631-
// gauge:<value:298.15 > timestamp_ms:1257894000012
667+
// {"gauge":{"value":298.15},"timestampMs":"1257894000012"}
632668
}

prometheus/expvar_collector_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"strings"
2121

2222
dto "github.com/prometheus/client_model/go"
23+
"google.golang.org/protobuf/encoding/protojson"
2324

2425
"github.com/prometheus/client_golang/prometheus"
2526
)
@@ -81,17 +82,21 @@ func ExampleNewExpvarCollector() {
8182
if !strings.Contains(m.Desc().String(), "expvar_memstats") {
8283
metric.Reset()
8384
m.Write(&metric)
84-
metricStrings = append(metricStrings, metric.String())
85+
metricAsJSON, err := protojson.Marshal(&metric)
86+
if err != nil {
87+
panic("error marshling metric")
88+
}
89+
metricStrings = append(metricStrings, removeBrittleJSON(metricAsJSON))
8590
}
8691
}
8792
sort.Strings(metricStrings)
8893
for _, s := range metricStrings {
8994
fmt.Println(strings.TrimRight(s, " "))
9095
}
9196
// Output:
92-
// label:<name:"code" value:"200" > label:<name:"method" value:"GET" > untyped:<value:212 >
93-
// label:<name:"code" value:"200" > label:<name:"method" value:"POST" > untyped:<value:11 >
94-
// label:<name:"code" value:"404" > label:<name:"method" value:"GET" > untyped:<value:13 >
95-
// label:<name:"code" value:"404" > label:<name:"method" value:"POST" > untyped:<value:3 >
96-
// untyped:<value:42 >
97+
// {"label":[{"name":"code","value":"200"},{"name":"method","value":"GET"}],"untyped":{"value":212}}
98+
// {"label":[{"name":"code","value":"200"},{"name":"method","value":"POST"}],"untyped":{"value":11}}
99+
// {"label":[{"name":"code","value":"404"},{"name":"method","value":"GET"}],"untyped":{"value":13}}
100+
// {"label":[{"name":"code","value":"404"},{"name":"method","value":"POST"}],"untyped":{"value":3}}
101+
// {"untyped":{"value":42}}
97102
}

prometheus/gauge_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
dto "github.com/prometheus/client_model/go"
25+
"google.golang.org/protobuf/proto"
2526
)
2627

2728
func listenGaugeStream(vals, result chan float64, done chan struct{}) {
@@ -177,8 +178,18 @@ func TestGaugeFunc(t *testing.T) {
177178
m := &dto.Metric{}
178179
gf.Write(m)
179180

180-
if expected, got := `label:<name:"a" value:"1" > label:<name:"b" value:"2" > gauge:<value:3.1415 > `, m.String(); expected != got {
181-
t.Errorf("expected %q, got %q", expected, got)
181+
expected := &dto.Metric{
182+
Label: []*dto.LabelPair{
183+
{Name: proto.String("a"), Value: proto.String("1")},
184+
{Name: proto.String("b"), Value: proto.String("2")},
185+
},
186+
Gauge: &dto.Gauge{
187+
Value: proto.Float64(3.1415),
188+
},
189+
}
190+
191+
if !proto.Equal(expected, m) {
192+
t.Errorf("expected %q, got %q", expected, m)
182193
}
183194
}
184195

0 commit comments

Comments
 (0)