Skip to content

Commit 1a88780

Browse files
authored
Bump prometheus/client_model (#1323)
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 06d4592 commit 1a88780

11 files changed

+442
-82
lines changed

go.mod

+1-1
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

+2-1
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

+33-8
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

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
package prometheus_test
1515

1616
import (
17-
"fmt"
18-
1917
"google.golang.org/protobuf/proto"
2018

2119
dto "github.com/prometheus/client_model/go"
@@ -126,8 +124,8 @@ func ExampleMetricVec() {
126124
if err != nil || len(metricFamilies) != 1 {
127125
panic("unexpected behavior of custom test registry")
128126
}
129-
fmt.Println(metricFamilies[0].String())
127+
printlnNormalized(metricFamilies[0])
130128

131129
// 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 > >
130+
// {"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}}]}
133131
}

prometheus/examples_test.go

+23-16
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,11 @@ func ExampleSummary() {
319319
// internally).
320320
metric := &dto.Metric{}
321321
temps.Write(metric)
322-
fmt.Println(metric.String())
322+
323+
printlnNormalized(metric)
323324

324325
// 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 > >
326+
// {"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}]}}
326327
}
327328

328329
func ExampleSummaryVec() {
@@ -354,10 +355,10 @@ func ExampleSummaryVec() {
354355
if err != nil || len(metricFamilies) != 1 {
355356
panic("unexpected behavior of custom test registry")
356357
}
357-
fmt.Println(metricFamilies[0].String())
358+
printlnNormalized(metricFamilies[0])
358359

359360
// 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 > > >
361+
// {"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}]}}]}
361362
}
362363

363364
func ExampleNewConstSummary() {
@@ -381,10 +382,10 @@ func ExampleNewConstSummary() {
381382
// internally).
382383
metric := &dto.Metric{}
383384
s.Write(metric)
384-
fmt.Println(metric.String())
385+
printlnNormalized(metric)
385386

386387
// 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 > >
388+
// {"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}]}}
388389
}
389390

390391
func ExampleHistogram() {
@@ -404,10 +405,10 @@ func ExampleHistogram() {
404405
// internally).
405406
metric := &dto.Metric{}
406407
temps.Write(metric)
407-
fmt.Println(metric.String())
408+
printlnNormalized(metric)
408409

409410
// 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 > >
411+
// {"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}]}}
411412
}
412413

413414
func ExampleNewConstHistogram() {
@@ -431,10 +432,10 @@ func ExampleNewConstHistogram() {
431432
// internally).
432433
metric := &dto.Metric{}
433434
h.Write(metric)
434-
fmt.Println(metric.String())
435+
printlnNormalized(metric)
435436

436437
// 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 > >
438+
// {"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}]}}
438439
}
439440

440441
func ExampleNewConstHistogram_WithExemplar() {
@@ -469,10 +470,10 @@ func ExampleNewConstHistogram_WithExemplar() {
469470
// internally).
470471
metric := &dto.Metric{}
471472
h.Write(metric)
472-
fmt.Println(metric.String())
473+
printlnNormalized(metric)
473474

474475
// 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 > > > >
476+
// {"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"}}]}}
476477
}
477478

478479
func ExampleAlreadyRegisteredError() {
@@ -567,7 +568,13 @@ temperature_kelvin 4.5
567568

568569
gathering, err = gatherers.Gather()
569570
if err != nil {
570-
fmt.Println(err)
571+
// 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
572+
// We cannot assert it because of https://github.com/golang/protobuf/issues/1121
573+
if strings.HasPrefix(err.Error(), `collected metric "temperature_kelvin" `) {
574+
fmt.Println("Found duplicated metric `temperature_kelvin`")
575+
} else {
576+
fmt.Print(err)
577+
}
571578
}
572579
// Note that still as many metrics as possible are returned:
573580
out.Reset()
@@ -589,7 +596,7 @@ temperature_kelvin 4.5
589596
// temperature_kelvin{location="outside"} 273.14
590597
// temperature_kelvin{location="somewhere else"} 4.5
591598
// ----------
592-
// collected metric "temperature_kelvin" { label:<name:"location" value:"outside" > gauge:<value:265.3 > } was collected before with the same name and label values
599+
// Found duplicated metric `temperature_kelvin`
593600
// # HELP humidity_percent Humidity in %.
594601
// # TYPE humidity_percent gauge
595602
// humidity_percent{location="inside"} 33.2
@@ -625,8 +632,8 @@ func ExampleNewMetricWithTimestamp() {
625632
// internally).
626633
metric := &dto.Metric{}
627634
s.Write(metric)
628-
fmt.Println(metric.String())
635+
printlnNormalized(metric)
629636

630637
// Output:
631-
// gauge:<value:298.15 > timestamp_ms:1257894000012
638+
// {"gauge":{"value":298.15},"timestampMs":"1257894000012"}
632639
}

prometheus/expvar_collector_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,17 @@ func ExampleNewExpvarCollector() {
8181
if !strings.Contains(m.Desc().String(), "expvar_memstats") {
8282
metric.Reset()
8383
m.Write(&metric)
84-
metricStrings = append(metricStrings, metric.String())
84+
metricStrings = append(metricStrings, protoToNormalizedJSON(&metric))
8585
}
8686
}
8787
sort.Strings(metricStrings)
8888
for _, s := range metricStrings {
89-
fmt.Println(strings.TrimRight(s, " "))
89+
fmt.Println(s)
9090
}
9191
// 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 >
92+
// {"label":[{"name":"code","value":"200"},{"name":"method","value":"GET"}],"untyped":{"value":212}}
93+
// {"label":[{"name":"code","value":"200"},{"name":"method","value":"POST"}],"untyped":{"value":11}}
94+
// {"label":[{"name":"code","value":"404"},{"name":"method","value":"GET"}],"untyped":{"value":13}}
95+
// {"label":[{"name":"code","value":"404"},{"name":"method","value":"POST"}],"untyped":{"value":3}}
96+
// {"untyped":{"value":42}}
9797
}

prometheus/gauge_test.go

+13-2
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)