Skip to content

Commit 7beed9c

Browse files
author
Steven Swartz
committed
Add support for created timestamp on const summaries
Signed-off-by: Steven Swartz <[email protected]>
1 parent dff41aa commit 7beed9c

File tree

3 files changed

+99
-3
lines changed

3 files changed

+99
-3
lines changed

prometheus/examples_test.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,35 @@ func ExampleNewConstSummary() {
405405
// {"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}]}}
406406
}
407407

408+
func ExampleNewConstSummaryWithCreatedTimestamp() {
409+
desc := prometheus.NewDesc(
410+
"http_request_duration_seconds",
411+
"A summary of the HTTP request durations.",
412+
[]string{"code", "method"},
413+
prometheus.Labels{"owner": "example"},
414+
)
415+
416+
// Create a constant summary with created timestamp set
417+
createdTs := time.Unix(1719670764, 123)
418+
s := prometheus.MustNewConstSummaryWithCreatedTimestamp(
419+
desc,
420+
4711, 403.34,
421+
map[float64]float64{0.5: 42.3, 0.9: 323.3},
422+
createdTs,
423+
"200", "get",
424+
)
425+
426+
// Just for demonstration, let's check the state of the summary by
427+
// (ab)using its Write method (which is usually only used by Prometheus
428+
// internally).
429+
metric := &dto.Metric{}
430+
s.Write(metric)
431+
fmt.Println(toNormalizedJSON(metric))
432+
433+
// Output:
434+
// {"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}],"createdTimestamp":"2024-06-29T14:19:24.000000123Z"}}
435+
}
436+
408437
func ExampleHistogram() {
409438
temps := prometheus.NewHistogram(prometheus.HistogramOpts{
410439
Name: "pond_temperature_celsius",
@@ -464,12 +493,12 @@ func ExampleNewConstHistogramWithCreatedTimestamp() {
464493
prometheus.Labels{"owner": "example"},
465494
)
466495

467-
ct := time.Unix(0, 0).UTC()
496+
createdTs := time.Unix(1719670764, 123)
468497
h := prometheus.MustNewConstHistogramWithCreatedTimestamp(
469498
desc,
470499
4711, 403.34,
471500
map[float64]uint64{25: 121, 50: 2403, 100: 3221, 200: 4233},
472-
ct,
501+
createdTs,
473502
"200", "get",
474503
)
475504

@@ -481,7 +510,7 @@ func ExampleNewConstHistogramWithCreatedTimestamp() {
481510
fmt.Println(toNormalizedJSON(metric))
482511

483512
// 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"}}
513+
// {"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":"2024-06-29T14:19:24.000000123Z"}}
485514
}
486515

487516
func ExampleNewConstHistogram_WithExemplar() {

prometheus/summary.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,3 +783,45 @@ func MustNewConstSummary(
783783
}
784784
return m
785785
}
786+
787+
// NewConstSummaryWithCreatedTimestamp does the same thing as NewConstSummary but sets the created timestamp
788+
func NewConstSummaryWithCreatedTimestamp(
789+
desc *Desc,
790+
count uint64,
791+
sum float64,
792+
quantiles map[float64]float64,
793+
ct time.Time,
794+
labelValues ...string,
795+
) (Metric, error) {
796+
if desc.err != nil {
797+
return nil, desc.err
798+
}
799+
if err := validateLabelValues(labelValues, len(desc.variableLabels.names)); err != nil {
800+
return nil, err
801+
}
802+
return &constSummary{
803+
desc: desc,
804+
count: count,
805+
sum: sum,
806+
quantiles: quantiles,
807+
labelPairs: MakeLabelPairs(desc, labelValues),
808+
createdTs: timestamppb.New(ct),
809+
}, nil
810+
}
811+
812+
// MustNewConstSummaryWithCreatedTimestamp is a version of NewConstSummaryWithCreatedTimestamp that panics where
813+
// NewConstSummaryWithCreatedTimestamp would have returned an error.
814+
func MustNewConstSummaryWithCreatedTimestamp(
815+
desc *Desc,
816+
count uint64,
817+
sum float64,
818+
quantiles map[float64]float64,
819+
ct time.Time,
820+
labelValues ...string,
821+
) Metric {
822+
m, err := NewConstSummaryWithCreatedTimestamp(desc, count, sum, quantiles, ct, labelValues...)
823+
if err != nil {
824+
panic(err)
825+
}
826+
return m
827+
}

prometheus/summary_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,28 @@ func TestSummaryVecCreatedTimestampWithDeletes(t *testing.T) {
474474
})
475475
}
476476
}
477+
478+
func TestNewConstSummaryWithCreatedTimestamp(t *testing.T) {
479+
metricDesc := NewDesc(
480+
"sample_value",
481+
"sample value",
482+
nil,
483+
nil,
484+
)
485+
quantiles := map[float64]float64{50: 200.12, 99: 500.342}
486+
createdTs := time.Unix(1719670764, 123)
487+
488+
s, err := NewConstSummaryWithCreatedTimestamp(metricDesc, 100, 200, quantiles, createdTs)
489+
if err != nil {
490+
t.Fatal(err)
491+
}
492+
493+
var metric dto.Metric
494+
if err := s.Write(&metric); err != nil {
495+
t.Fatal(err)
496+
}
497+
498+
if metric.Summary.CreatedTimestamp.AsTime().UnixMicro() != createdTs.UnixMicro() {
499+
t.Errorf("Expected created timestamp %v, got %v", createdTs, &metric.Summary.CreatedTimestamp)
500+
}
501+
}

0 commit comments

Comments
 (0)