Skip to content

Commit 0220baf

Browse files
author
Steven Swartz
committed
Allow creating constant histogram metrics with a created timestamp
Closes prometheus#1535 Signed-off-by: Steven Swartz <[email protected]>
1 parent d038ab9 commit 0220baf

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

prometheus/examples_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,34 @@ func ExampleNewConstHistogram() {
456456
// {"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}]}}
457457
}
458458

459+
func ExampleNewConstHistogramWithCreatedTimestamp() {
460+
desc := prometheus.NewDesc(
461+
"http_request_duration_seconds",
462+
"A histogram of the HTTP request durations.",
463+
[]string{"code", "method"},
464+
prometheus.Labels{"owner": "example"},
465+
)
466+
467+
ct := time.Unix(0, 0).UTC()
468+
h := prometheus.MustNewConstHistogramWithCreatedTimestamp(
469+
desc,
470+
4711, 403.34,
471+
map[float64]uint64{25: 121, 50: 2403, 100: 3221, 200: 4233},
472+
ct,
473+
"200", "get",
474+
)
475+
476+
// Just for demonstration, let's check the state of the histogram by
477+
// (ab)using its Write method (which is usually only used by Prometheus
478+
// internally).
479+
metric := &dto.Metric{}
480+
h.Write(metric)
481+
fmt.Println(toNormalizedJSON(metric))
482+
483+
// Output:
484+
// {"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}],"createdTimestamp":"1970-01-01T00:00:00Z"}}
485+
}
486+
459487
func ExampleNewConstHistogram_WithExemplar() {
460488
desc := prometheus.NewDesc(
461489
"http_request_duration_seconds",

prometheus/histogram.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,48 @@ func MustNewConstHistogram(
13361336
return m
13371337
}
13381338

1339+
// NewConstHistogramWithCreatedTimestamp does the same thing as NewConstHistogram but sets the created timestamp
1340+
func NewConstHistogramWithCreatedTimestamp(
1341+
desc *Desc,
1342+
count uint64,
1343+
sum float64,
1344+
buckets map[float64]uint64,
1345+
ct time.Time,
1346+
labelValues ...string,
1347+
) (Metric, error) {
1348+
if desc.err != nil {
1349+
return nil, desc.err
1350+
}
1351+
if err := validateLabelValues(labelValues, len(desc.variableLabels.names)); err != nil {
1352+
return nil, err
1353+
}
1354+
return &constHistogram{
1355+
desc: desc,
1356+
count: count,
1357+
sum: sum,
1358+
buckets: buckets,
1359+
labelPairs: MakeLabelPairs(desc, labelValues),
1360+
createdTs: timestamppb.New(ct),
1361+
}, nil
1362+
}
1363+
1364+
// MustNewConstHistogramWithCreatedTimestamp is a version of NewConstHistogramWithCreatedTimestamp that panics where
1365+
// NewConstHistogramWithCreatedTimestamp would have returned an error.
1366+
func MustNewConstHistogramWithCreatedTimestamp(
1367+
desc *Desc,
1368+
count uint64,
1369+
sum float64,
1370+
buckets map[float64]uint64,
1371+
ct time.Time,
1372+
labelValues ...string,
1373+
) Metric {
1374+
m, err := NewConstHistogramWithCreatedTimestamp(desc, count, sum, buckets, ct, labelValues...)
1375+
if err != nil {
1376+
panic(err)
1377+
}
1378+
return m
1379+
}
1380+
13391381
type buckSort []*dto.Bucket
13401382

13411383
func (s buckSort) Len() int {

0 commit comments

Comments
 (0)