From 64bd5685e779bfef0659add87b29f258ee565587 Mon Sep 17 00:00:00 2001 From: Arthur Silva Sens Date: Fri, 11 Aug 2023 18:43:28 -0300 Subject: [PATCH] Backport created timestamp to existing tests Signed-off-by: Arthur Silva Sens --- prometheus/counter_test.go | 18 ++++++++++++++++-- prometheus/examples_test.go | 10 ++++++++++ prometheus/histogram_test.go | 27 +++++++++++++++++++++++++++ prometheus/wrap_test.go | 9 +++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/prometheus/counter_test.go b/prometheus/counter_test.go index 6b850b6c1..d6a69d462 100644 --- a/prometheus/counter_test.go +++ b/prometheus/counter_test.go @@ -26,10 +26,12 @@ import ( ) func TestCounterAdd(t *testing.T) { + now := func() time.Time { ct := timestamppb.Timestamp{Seconds: 1, Nanos: 1}; return ct.AsTime() } counter := NewCounter(CounterOpts{ Name: "test", Help: "test help", ConstLabels: Labels{"a": "1", "b": "2"}, + now: now, }).(*counter) counter.Inc() if expected, got := 0.0, math.Float64frombits(counter.valBits); expected != got { @@ -66,7 +68,10 @@ func TestCounterAdd(t *testing.T) { {Name: proto.String("a"), Value: proto.String("1")}, {Name: proto.String("b"), Value: proto.String("2")}, }, - Counter: &dto.Counter{Value: proto.Float64(67.42)}, + Counter: &dto.Counter{ + Value: proto.Float64(67.42), + CreatedTimestamp: timestamppb.New(now()), + }, } if !proto.Equal(expected, m) { t.Errorf("expected %q, got %q", expected, m) @@ -139,9 +144,11 @@ func expectPanic(t *testing.T, op func(), errorMsg string) { } func TestCounterAddInf(t *testing.T) { + now := func() time.Time { ct := timestamppb.Timestamp{Seconds: 1, Nanos: 1}; return ct.AsTime() } counter := NewCounter(CounterOpts{ Name: "test", Help: "test help", + now: now, }).(*counter) counter.Inc() @@ -173,7 +180,8 @@ func TestCounterAddInf(t *testing.T) { expected := &dto.Metric{ Counter: &dto.Counter{ - Value: proto.Float64(math.Inf(1)), + Value: proto.Float64(math.Inf(1)), + CreatedTimestamp: timestamppb.New(now()), }, } @@ -183,9 +191,11 @@ func TestCounterAddInf(t *testing.T) { } func TestCounterAddLarge(t *testing.T) { + now := func() time.Time { ct := timestamppb.Timestamp{Seconds: 1, Nanos: 1}; return ct.AsTime() } counter := NewCounter(CounterOpts{ Name: "test", Help: "test help", + now: now, }).(*counter) // large overflows the underlying type and should therefore be stored in valBits. @@ -204,6 +214,7 @@ func TestCounterAddLarge(t *testing.T) { expected := &dto.Metric{ Counter: &dto.Counter{ Value: proto.Float64(large), + CreatedTimestamp: timestamppb.New(now()), }, } @@ -213,9 +224,11 @@ func TestCounterAddLarge(t *testing.T) { } func TestCounterAddSmall(t *testing.T) { + now := func() time.Time { ct := timestamppb.Timestamp{Seconds: 1, Nanos: 1}; return ct.AsTime() } counter := NewCounter(CounterOpts{ Name: "test", Help: "test help", + now: now, }).(*counter) small := 0.000000000001 counter.Add(small) @@ -232,6 +245,7 @@ func TestCounterAddSmall(t *testing.T) { expected := &dto.Metric{ Counter: &dto.Counter{ Value: proto.Float64(small), + CreatedTimestamp: timestamppb.New(now()), }, } diff --git a/prometheus/examples_test.go b/prometheus/examples_test.go index 9d918e1fa..76aa0f5a2 100644 --- a/prometheus/examples_test.go +++ b/prometheus/examples_test.go @@ -319,6 +319,8 @@ func ExampleSummary() { // internally). metric := &dto.Metric{} temps.Write(metric) + // We remove CreatedTimestamp just to make sure the assert below works. + metric.Summary.CreatedTimestamp = nil printlnNormalized(metric) @@ -355,6 +357,11 @@ func ExampleSummaryVec() { if err != nil || len(metricFamilies) != 1 { panic("unexpected behavior of custom test registry") } + // We remove CreatedTimestamp just to make sure the assert below works. + for _, m := range metricFamilies[0].Metric { + m.Summary.CreatedTimestamp = nil + } + printlnNormalized(metricFamilies[0]) // Output: @@ -405,6 +412,9 @@ func ExampleHistogram() { // internally). metric := &dto.Metric{} temps.Write(metric) + // We remove CreatedTimestamp just to make sure the assert below works. + metric.Histogram.CreatedTimestamp = nil + printlnNormalized(metric) // Output: diff --git a/prometheus/histogram_test.go b/prometheus/histogram_test.go index 379fcb029..f534455ca 100644 --- a/prometheus/histogram_test.go +++ b/prometheus/histogram_test.go @@ -469,6 +469,7 @@ func TestHistogramExemplar(t *testing.T) { } func TestNativeHistogram(t *testing.T) { + now := func() time.Time { ct := timestamppb.Timestamp{Seconds: 1, Nanos: 1}; return ct.AsTime() } scenarios := []struct { name string observations []float64 // With simulated interval of 1m. @@ -499,6 +500,7 @@ func TestNativeHistogram(t *testing.T) { {CumulativeCount: proto.Uint64(3), UpperBound: proto.Float64(5)}, {CumulativeCount: proto.Uint64(3), UpperBound: proto.Float64(10)}, }, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -510,6 +512,7 @@ func TestNativeHistogram(t *testing.T) { Schema: proto.Int32(3), ZeroThreshold: proto.Float64(2.938735877055719e-39), ZeroCount: proto.Uint64(0), + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -525,6 +528,7 @@ func TestNativeHistogram(t *testing.T) { PositiveSpan: []*dto.BucketSpan{ {Offset: proto.Int32(0), Length: proto.Uint32(0)}, }, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -543,6 +547,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(4), Length: proto.Uint32(1)}, }, PositiveDelta: []int64{1, 0, 0}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -559,6 +564,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(0), Length: proto.Uint32(5)}, }, PositiveDelta: []int64{1, -1, 2, -2, 2}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -582,6 +588,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(-2), Length: proto.Uint32(6)}, }, PositiveDelta: []int64{2, 0, 0, 2, -1, -2}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -603,6 +610,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(-1), Length: proto.Uint32(4)}, }, PositiveDelta: []int64{2, 2, 3, -6}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -619,6 +627,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(0), Length: proto.Uint32(5)}, }, NegativeDelta: []int64{1, -1, 2, -2, 2}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -639,6 +648,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(0), Length: proto.Uint32(5)}, }, PositiveDelta: []int64{1, -1, 2, -2, 2}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -660,6 +670,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(4), Length: proto.Uint32(1)}, }, PositiveDelta: []int64{2}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -676,6 +687,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(0), Length: proto.Uint32(5)}, }, PositiveDelta: []int64{1, -1, 2, -2, 2}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -693,6 +705,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(4092), Length: proto.Uint32(1)}, }, PositiveDelta: []int64{1, -1, 2, -2, 2, -1}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -713,6 +726,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(0), Length: proto.Uint32(5)}, }, PositiveDelta: []int64{1, -1, 2, -2, 2}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -730,6 +744,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(0), Length: proto.Uint32(5)}, }, PositiveDelta: []int64{1, -1, 2, -2, 2}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -747,6 +762,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(0), Length: proto.Uint32(5)}, }, PositiveDelta: []int64{1, 2, -1, -2, 1}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -765,6 +781,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(1), Length: proto.Uint32(7)}, }, PositiveDelta: []int64{1, 1, -2, 2, -2, 0, 1}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -783,6 +800,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(2), Length: proto.Uint32(7)}, }, PositiveDelta: []int64{2, -2, 2, -2, 0, 1, 0}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -802,6 +820,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(7), Length: proto.Uint32(2)}, }, PositiveDelta: []int64{1, 0}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -819,6 +838,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(0), Length: proto.Uint32(5)}, }, NegativeDelta: []int64{1, -1, 2, -2, 2}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -836,6 +856,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(0), Length: proto.Uint32(5)}, }, NegativeDelta: []int64{1, 2, -1, -2, 1}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -854,6 +875,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(1), Length: proto.Uint32(7)}, }, NegativeDelta: []int64{1, 1, -2, 2, -2, 0, 1}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -872,6 +894,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(2), Length: proto.Uint32(7)}, }, NegativeDelta: []int64{2, -2, 2, -2, 0, 1, 0}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -891,6 +914,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(7), Length: proto.Uint32(2)}, }, NegativeDelta: []int64{1, 0}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -909,6 +933,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(7), Length: proto.Uint32(2)}, }, PositiveDelta: []int64{1, 0}, + CreatedTimestamp: timestamppb.New(now()), }, }, { @@ -928,6 +953,7 @@ func TestNativeHistogram(t *testing.T) { {Offset: proto.Int32(7), Length: proto.Uint32(2)}, }, PositiveDelta: []int64{1, 0}, + CreatedTimestamp: timestamppb.New(now()), }, }, } @@ -942,6 +968,7 @@ func TestNativeHistogram(t *testing.T) { NativeHistogramMaxBucketNumber: s.maxBuckets, NativeHistogramMinResetDuration: s.minResetDuration, NativeHistogramMaxZeroThreshold: s.maxZeroThreshold, + now: now, }) ts := time.Now().Add(30 * time.Second) now := func() time.Time { diff --git a/prometheus/wrap_test.go b/prometheus/wrap_test.go index 7e5bba15a..911e85d6b 100644 --- a/prometheus/wrap_test.go +++ b/prometheus/wrap_test.go @@ -17,9 +17,11 @@ import ( "fmt" "strings" "testing" + "time" dto "github.com/prometheus/client_model/go" "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/types/known/timestamppb" ) // uncheckedCollector wraps a Collector but its Describe method yields no Desc. @@ -43,9 +45,11 @@ func toMetricFamilies(cs ...Collector) []*dto.MetricFamily { } func TestWrap(t *testing.T) { + now := func() time.Time { ct := timestamppb.Timestamp{Seconds: 1, Nanos: 1}; return ct.AsTime() } simpleCnt := NewCounter(CounterOpts{ Name: "simpleCnt", Help: "helpSimpleCnt", + now: now, }) simpleCnt.Inc() @@ -58,6 +62,7 @@ func TestWrap(t *testing.T) { preCnt := NewCounter(CounterOpts{ Name: "pre_simpleCnt", Help: "helpSimpleCnt", + now: now, }) preCnt.Inc() @@ -65,6 +70,7 @@ func TestWrap(t *testing.T) { Name: "simpleCnt", Help: "helpSimpleCnt", ConstLabels: Labels{"foo": "bar"}, + now: now, }) barLabeledCnt.Inc() @@ -72,6 +78,7 @@ func TestWrap(t *testing.T) { Name: "simpleCnt", Help: "helpSimpleCnt", ConstLabels: Labels{"foo": "baz"}, + now: now, }) bazLabeledCnt.Inc() @@ -79,6 +86,7 @@ func TestWrap(t *testing.T) { Name: "pre_simpleCnt", Help: "helpSimpleCnt", ConstLabels: Labels{"foo": "bar"}, + now: now, }) labeledPreCnt.Inc() @@ -86,6 +94,7 @@ func TestWrap(t *testing.T) { Name: "pre_simpleCnt", Help: "helpSimpleCnt", ConstLabels: Labels{"foo": "bar", "dings": "bums"}, + now: now, }) twiceLabeledPreCnt.Inc()