Skip to content

Commit c4f69f0

Browse files
committed
Merge remote-tracking branch 'prometheus/main' into add-unit
2 parents 2e9a8fa + 80d3f0b commit c4f69f0

File tree

6 files changed

+62
-3
lines changed

6 files changed

+62
-3
lines changed

api/prometheus/v1/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func ExampleAPI_queryRangeWithBasicAuth() {
135135
client, err := api.NewClient(api.Config{
136136
Address: "http://demo.robustperception.io:9090",
137137
// We can use amazing github.com/prometheus/common/config helper!
138-
RoundTripper: config.NewBasicAuthRoundTripper("me", "definitely_me", "", api.DefaultRoundTripper),
138+
RoundTripper: config.NewBasicAuthRoundTripper("me", "definitely_me", "", "", api.DefaultRoundTripper),
139139
})
140140
if err != nil {
141141
fmt.Printf("Error creating client: %v\n", err)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/davecgh/go-spew v1.1.1
99
github.com/json-iterator/go v1.1.12
1010
github.com/prometheus/client_model v0.5.0
11-
github.com/prometheus/common v0.44.0
11+
github.com/prometheus/common v0.45.0
1212
github.com/prometheus/procfs v0.12.0
1313
golang.org/x/sys v0.13.0
1414
google.golang.org/protobuf v1.31.0

prometheus/examples_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,3 +655,27 @@ func ExampleNewMetricWithTimestamp() {
655655
// Output:
656656
// {"gauge":{"value":298.15},"timestampMs":"1257894000012"}
657657
}
658+
659+
func ExampleNewConstMetricWithCreatedTimestamp() {
660+
// Here we have a metric that is reported by an external system.
661+
// Besides providing the value, the external system also provides the
662+
// timestamp when the metric was created.
663+
desc := prometheus.NewDesc(
664+
"time_since_epoch_seconds",
665+
"Current epoch time in seconds.",
666+
nil, nil,
667+
)
668+
669+
timeSinceEpochReportedByExternalSystem := time.Date(2009, time.November, 10, 23, 0, 0, 12345678, time.UTC)
670+
epoch := time.Unix(0, 0).UTC()
671+
s := prometheus.MustNewConstMetricWithCreatedTimestamp(
672+
desc, prometheus.CounterValue, float64(timeSinceEpochReportedByExternalSystem.Unix()), epoch,
673+
)
674+
675+
metric := &dto.Metric{}
676+
s.Write(metric)
677+
fmt.Println(toNormalizedJSON(metric))
678+
679+
// Output:
680+
// {"counter":{"value":1257894000,"createdTimestamp":"1970-01-01T00:00:00Z"}}
681+
}

prometheus/gen_go_collector_metrics_set.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"github.com/prometheus/client_golang/prometheus"
3232
"github.com/prometheus/client_golang/prometheus/internal"
3333

34-
"github.com/hashicorp/go-version"
34+
version "github.com/hashicorp/go-version"
3535
)
3636

3737
func main() {

prometheus/testutil/testutil.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import (
4747
"github.com/davecgh/go-spew/spew"
4848
dto "github.com/prometheus/client_model/go"
4949
"github.com/prometheus/common/expfmt"
50+
"google.golang.org/protobuf/proto"
5051

5152
"github.com/prometheus/client_golang/prometheus"
5253
"github.com/prometheus/client_golang/prometheus/internal"
@@ -230,6 +231,20 @@ func convertReaderToMetricFamily(reader io.Reader) ([]*dto.MetricFamily, error)
230231
return nil, fmt.Errorf("converting reader to metric families failed: %w", err)
231232
}
232233

234+
// The text protocol handles empty help fields inconsistently. When
235+
// encoding, any non-nil value, include the empty string, produces a
236+
// "# HELP" line. But when decoding, the help field is only set to a
237+
// non-nil value if the "# HELP" line contains a non-empty value.
238+
//
239+
// Because metrics in a registry always have non-nil help fields, populate
240+
// any nil help fields in the parsed metrics with the empty string so that
241+
// when we compare text encodings, the results are consistent.
242+
for _, metric := range notNormalized {
243+
if metric.Help == nil {
244+
metric.Help = proto.String("")
245+
}
246+
}
247+
233248
return internal.NormalizeMetricFamilies(notNormalized), nil
234249
}
235250

prometheus/testutil/testutil_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,26 @@ func TestCollectAndCompareNoLabel(t *testing.T) {
168168
}
169169
}
170170

171+
func TestCollectAndCompareNoHelp(t *testing.T) {
172+
const metadata = `
173+
# TYPE some_total counter
174+
`
175+
176+
c := prometheus.NewCounter(prometheus.CounterOpts{
177+
Name: "some_total",
178+
})
179+
c.Inc()
180+
181+
expected := `
182+
183+
some_total 1
184+
`
185+
186+
if err := CollectAndCompare(c, strings.NewReader(metadata+expected), "some_total"); err != nil {
187+
t.Errorf("unexpected collecting result:\n%s", err)
188+
}
189+
}
190+
171191
func TestCollectAndCompareHistogram(t *testing.T) {
172192
inputs := []struct {
173193
name string

0 commit comments

Comments
 (0)