Skip to content

Commit

Permalink
(tests): Update examples with protojson
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Silva Sens <[email protected]>
  • Loading branch information
Arthur Silva Sens committed Aug 11, 2023
1 parent 9e85129 commit 3029f04
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
9 changes: 7 additions & 2 deletions prometheus/example_metricvec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package prometheus_test
import (
"fmt"

"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"

dto "github.com/prometheus/client_model/go"
Expand Down Expand Up @@ -126,8 +127,12 @@ func ExampleMetricVec() {
if err != nil || len(metricFamilies) != 1 {
panic("unexpected behavior of custom test registry")
}
fmt.Println(metricFamilies[0].String())
metricAsJson, err := protojson.Marshal(metricFamilies[0])

Check warning on line 130 in prometheus/example_metricvec_test.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var metricAsJson should be metricAsJSON (revive)
if err != nil {
panic("error marshling metricFamily")
}
fmt.Println(string(metricAsJson))

// Output:
// 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 > >
// {"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}}]}
}
58 changes: 44 additions & 14 deletions prometheus/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"google.golang.org/protobuf/encoding/protojson"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -319,10 +320,15 @@ func ExampleSummary() {
// internally).
metric := &dto.Metric{}
temps.Write(metric)
fmt.Println(metric.String())

metricAsJson, err := protojson.Marshal(metric)

Check warning on line 324 in prometheus/examples_test.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var metricAsJson should be metricAsJSON (revive)
if err != nil {
panic("error marshling metric")
}
fmt.Println(string(metricAsJson))

// Output:
// 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 > >
// {"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}]}}
}

func ExampleSummaryVec() {
Expand Down Expand Up @@ -354,10 +360,14 @@ func ExampleSummaryVec() {
if err != nil || len(metricFamilies) != 1 {
panic("unexpected behavior of custom test registry")
}
fmt.Println(metricFamilies[0].String())
metricAsJson, err := protojson.Marshal(metricFamilies[0])

Check warning on line 363 in prometheus/examples_test.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var metricAsJson should be metricAsJSON (revive)
if err != nil {
panic("error marshling metric")
}
fmt.Println(string(metricAsJson))

// Output:
// 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 > > >
// {"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}]}}]}
}

func ExampleNewConstSummary() {
Expand All @@ -381,10 +391,14 @@ func ExampleNewConstSummary() {
// internally).
metric := &dto.Metric{}
s.Write(metric)
fmt.Println(metric.String())
metricAsJson, err := protojson.Marshal(metric)

Check warning on line 394 in prometheus/examples_test.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var metricAsJson should be metricAsJSON (revive)
if err != nil {
panic("error marshling metric")
}
fmt.Println(string(metricAsJson))

// Output:
// 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 > >
// {"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}]}}
}

func ExampleHistogram() {
Expand All @@ -404,10 +418,14 @@ func ExampleHistogram() {
// internally).
metric := &dto.Metric{}
temps.Write(metric)
fmt.Println(metric.String())
metricAsJson, err := protojson.Marshal(metric)

Check warning on line 421 in prometheus/examples_test.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var metricAsJson should be metricAsJSON (revive)
if err != nil {
panic("error marshling metric")
}
fmt.Println(string(metricAsJson))

// Output:
// 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 > >
// {"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}]}}
}

func ExampleNewConstHistogram() {
Expand All @@ -431,10 +449,14 @@ func ExampleNewConstHistogram() {
// internally).
metric := &dto.Metric{}
h.Write(metric)
fmt.Println(metric.String())
metricAsJson, err := protojson.Marshal(metric)

Check warning on line 452 in prometheus/examples_test.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var metricAsJson should be metricAsJSON (revive)
if err != nil {
panic("error marshling metric")
}
fmt.Println(string(metricAsJson))

// Output:
// 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 > >
// {"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}]}}
}

func ExampleNewConstHistogram_WithExemplar() {
Expand Down Expand Up @@ -469,10 +491,14 @@ func ExampleNewConstHistogram_WithExemplar() {
// internally).
metric := &dto.Metric{}
h.Write(metric)
fmt.Println(metric.String())
metricAsJson, err := protojson.Marshal(metric)

Check warning on line 494 in prometheus/examples_test.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var metricAsJson should be metricAsJSON (revive)
if err != nil {
panic("error marshling metric")
}
fmt.Println(string(metricAsJson))

// Output:
// 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 > > > >
// {"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"}}]}}
}

func ExampleAlreadyRegisteredError() {
Expand Down Expand Up @@ -625,8 +651,12 @@ func ExampleNewMetricWithTimestamp() {
// internally).
metric := &dto.Metric{}
s.Write(metric)
fmt.Println(metric.String())
metricAsJson, err := protojson.Marshal(metric)

Check warning on line 654 in prometheus/examples_test.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var metricAsJson should be metricAsJSON (revive)
if err != nil {
panic("error marshling metric")
}
fmt.Println(string(metricAsJson))

// Output:
// gauge:<value:298.15 > timestamp_ms:1257894000012
// {"gauge":{"value":298.15}, "timestampMs":"1257894000012"}
}

0 comments on commit 3029f04

Please sign in to comment.