Backport created timestamp to existing tests

Signed-off-by: Arthur Silva Sens <arthur.sens@coralogix.com>
This commit is contained in:
Arthur Silva Sens 2023-08-11 18:43:28 -03:00
parent 3c7e78cf3e
commit 226eb8dcd4
No known key found for this signature in database
GPG Key ID: 7B844F1CE139BA7E
5 changed files with 101 additions and 33 deletions

View File

@ -26,10 +26,13 @@ import (
) )
func TestCounterAdd(t *testing.T) { func TestCounterAdd(t *testing.T) {
now := time.Now()
nowFn := func() time.Time { return now }
counter := NewCounter(CounterOpts{ counter := NewCounter(CounterOpts{
Name: "test", Name: "test",
Help: "test help", Help: "test help",
ConstLabels: Labels{"a": "1", "b": "2"}, ConstLabels: Labels{"a": "1", "b": "2"},
now: nowFn,
}).(*counter) }).(*counter)
counter.Inc() counter.Inc()
if expected, got := 0.0, math.Float64frombits(counter.valBits); expected != got { if expected, got := 0.0, math.Float64frombits(counter.valBits); expected != got {
@ -66,7 +69,10 @@ func TestCounterAdd(t *testing.T) {
{Name: proto.String("a"), Value: proto.String("1")}, {Name: proto.String("a"), Value: proto.String("1")},
{Name: proto.String("b"), Value: proto.String("2")}, {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(nowFn()),
},
} }
if !proto.Equal(expected, m) { if !proto.Equal(expected, m) {
t.Errorf("expected %q, got %q", expected, m) t.Errorf("expected %q, got %q", expected, m)
@ -139,9 +145,12 @@ func expectPanic(t *testing.T, op func(), errorMsg string) {
} }
func TestCounterAddInf(t *testing.T) { func TestCounterAddInf(t *testing.T) {
now := time.Now()
nowFn := func() time.Time { return now }
counter := NewCounter(CounterOpts{ counter := NewCounter(CounterOpts{
Name: "test", Name: "test",
Help: "test help", Help: "test help",
now: nowFn,
}).(*counter) }).(*counter)
counter.Inc() counter.Inc()
@ -174,6 +183,7 @@ func TestCounterAddInf(t *testing.T) {
expected := &dto.Metric{ expected := &dto.Metric{
Counter: &dto.Counter{ Counter: &dto.Counter{
Value: proto.Float64(math.Inf(1)), Value: proto.Float64(math.Inf(1)),
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
} }
@ -183,9 +193,12 @@ func TestCounterAddInf(t *testing.T) {
} }
func TestCounterAddLarge(t *testing.T) { func TestCounterAddLarge(t *testing.T) {
now := time.Now()
nowFn := func() time.Time { return now }
counter := NewCounter(CounterOpts{ counter := NewCounter(CounterOpts{
Name: "test", Name: "test",
Help: "test help", Help: "test help",
now: nowFn,
}).(*counter) }).(*counter)
// large overflows the underlying type and should therefore be stored in valBits. // large overflows the underlying type and should therefore be stored in valBits.
@ -204,6 +217,7 @@ func TestCounterAddLarge(t *testing.T) {
expected := &dto.Metric{ expected := &dto.Metric{
Counter: &dto.Counter{ Counter: &dto.Counter{
Value: proto.Float64(large), Value: proto.Float64(large),
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
} }
@ -213,9 +227,12 @@ func TestCounterAddLarge(t *testing.T) {
} }
func TestCounterAddSmall(t *testing.T) { func TestCounterAddSmall(t *testing.T) {
now := time.Now()
nowFn := func() time.Time { return now }
counter := NewCounter(CounterOpts{ counter := NewCounter(CounterOpts{
Name: "test", Name: "test",
Help: "test help", Help: "test help",
now: nowFn,
}).(*counter) }).(*counter)
small := 0.000000000001 small := 0.000000000001
counter.Add(small) counter.Add(small)
@ -232,6 +249,7 @@ func TestCounterAddSmall(t *testing.T) {
expected := &dto.Metric{ expected := &dto.Metric{
Counter: &dto.Counter{ Counter: &dto.Counter{
Value: proto.Float64(small), Value: proto.Float64(small),
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
} }

View File

@ -319,6 +319,8 @@ func ExampleSummary() {
// internally). // internally).
metric := &dto.Metric{} metric := &dto.Metric{}
temps.Write(metric) temps.Write(metric)
// We remove CreatedTimestamp just to make sure the assert below works.
metric.Summary.CreatedTimestamp = nil
printlnNormalized(metric) printlnNormalized(metric)
@ -355,6 +357,11 @@ func ExampleSummaryVec() {
if err != nil || len(metricFamilies) != 1 { if err != nil || len(metricFamilies) != 1 {
panic("unexpected behavior of custom test registry") 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]) printlnNormalized(metricFamilies[0])
// Output: // Output:
@ -405,6 +412,9 @@ func ExampleHistogram() {
// internally). // internally).
metric := &dto.Metric{} metric := &dto.Metric{}
temps.Write(metric) temps.Write(metric)
// We remove CreatedTimestamp just to make sure the assert below works.
metric.Histogram.CreatedTimestamp = nil
printlnNormalized(metric) printlnNormalized(metric)
// Output: // Output:

View File

@ -469,6 +469,8 @@ func TestHistogramExemplar(t *testing.T) {
} }
func TestNativeHistogram(t *testing.T) { func TestNativeHistogram(t *testing.T) {
now := time.Now()
nowFn := func() time.Time { return now }
scenarios := []struct { scenarios := []struct {
name string name string
observations []float64 // With simulated interval of 1m. observations []float64 // With simulated interval of 1m.
@ -499,6 +501,7 @@ func TestNativeHistogram(t *testing.T) {
{CumulativeCount: proto.Uint64(3), UpperBound: proto.Float64(5)}, {CumulativeCount: proto.Uint64(3), UpperBound: proto.Float64(5)},
{CumulativeCount: proto.Uint64(3), UpperBound: proto.Float64(10)}, {CumulativeCount: proto.Uint64(3), UpperBound: proto.Float64(10)},
}, },
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -510,6 +513,7 @@ func TestNativeHistogram(t *testing.T) {
Schema: proto.Int32(3), Schema: proto.Int32(3),
ZeroThreshold: proto.Float64(2.938735877055719e-39), ZeroThreshold: proto.Float64(2.938735877055719e-39),
ZeroCount: proto.Uint64(0), ZeroCount: proto.Uint64(0),
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -525,6 +529,7 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{ PositiveSpan: []*dto.BucketSpan{
{Offset: proto.Int32(0), Length: proto.Uint32(0)}, {Offset: proto.Int32(0), Length: proto.Uint32(0)},
}, },
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -543,6 +548,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(4), Length: proto.Uint32(1)}, {Offset: proto.Int32(4), Length: proto.Uint32(1)},
}, },
PositiveDelta: []int64{1, 0, 0}, PositiveDelta: []int64{1, 0, 0},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -559,6 +565,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(0), Length: proto.Uint32(5)}, {Offset: proto.Int32(0), Length: proto.Uint32(5)},
}, },
PositiveDelta: []int64{1, -1, 2, -2, 2}, PositiveDelta: []int64{1, -1, 2, -2, 2},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -582,6 +589,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(-2), Length: proto.Uint32(6)}, {Offset: proto.Int32(-2), Length: proto.Uint32(6)},
}, },
PositiveDelta: []int64{2, 0, 0, 2, -1, -2}, PositiveDelta: []int64{2, 0, 0, 2, -1, -2},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -603,6 +611,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(-1), Length: proto.Uint32(4)}, {Offset: proto.Int32(-1), Length: proto.Uint32(4)},
}, },
PositiveDelta: []int64{2, 2, 3, -6}, PositiveDelta: []int64{2, 2, 3, -6},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -619,6 +628,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(0), Length: proto.Uint32(5)}, {Offset: proto.Int32(0), Length: proto.Uint32(5)},
}, },
NegativeDelta: []int64{1, -1, 2, -2, 2}, NegativeDelta: []int64{1, -1, 2, -2, 2},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -639,6 +649,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(0), Length: proto.Uint32(5)}, {Offset: proto.Int32(0), Length: proto.Uint32(5)},
}, },
PositiveDelta: []int64{1, -1, 2, -2, 2}, PositiveDelta: []int64{1, -1, 2, -2, 2},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -660,6 +671,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(4), Length: proto.Uint32(1)}, {Offset: proto.Int32(4), Length: proto.Uint32(1)},
}, },
PositiveDelta: []int64{2}, PositiveDelta: []int64{2},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -676,6 +688,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(0), Length: proto.Uint32(5)}, {Offset: proto.Int32(0), Length: proto.Uint32(5)},
}, },
PositiveDelta: []int64{1, -1, 2, -2, 2}, PositiveDelta: []int64{1, -1, 2, -2, 2},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -693,6 +706,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(4092), Length: proto.Uint32(1)}, {Offset: proto.Int32(4092), Length: proto.Uint32(1)},
}, },
PositiveDelta: []int64{1, -1, 2, -2, 2, -1}, PositiveDelta: []int64{1, -1, 2, -2, 2, -1},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -713,6 +727,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(0), Length: proto.Uint32(5)}, {Offset: proto.Int32(0), Length: proto.Uint32(5)},
}, },
PositiveDelta: []int64{1, -1, 2, -2, 2}, PositiveDelta: []int64{1, -1, 2, -2, 2},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -730,6 +745,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(0), Length: proto.Uint32(5)}, {Offset: proto.Int32(0), Length: proto.Uint32(5)},
}, },
PositiveDelta: []int64{1, -1, 2, -2, 2}, PositiveDelta: []int64{1, -1, 2, -2, 2},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -747,6 +763,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(0), Length: proto.Uint32(5)}, {Offset: proto.Int32(0), Length: proto.Uint32(5)},
}, },
PositiveDelta: []int64{1, 2, -1, -2, 1}, PositiveDelta: []int64{1, 2, -1, -2, 1},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -765,6 +782,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(1), Length: proto.Uint32(7)}, {Offset: proto.Int32(1), Length: proto.Uint32(7)},
}, },
PositiveDelta: []int64{1, 1, -2, 2, -2, 0, 1}, PositiveDelta: []int64{1, 1, -2, 2, -2, 0, 1},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -783,6 +801,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(2), Length: proto.Uint32(7)}, {Offset: proto.Int32(2), Length: proto.Uint32(7)},
}, },
PositiveDelta: []int64{2, -2, 2, -2, 0, 1, 0}, PositiveDelta: []int64{2, -2, 2, -2, 0, 1, 0},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -802,6 +821,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(7), Length: proto.Uint32(2)}, {Offset: proto.Int32(7), Length: proto.Uint32(2)},
}, },
PositiveDelta: []int64{1, 0}, PositiveDelta: []int64{1, 0},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -819,6 +839,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(0), Length: proto.Uint32(5)}, {Offset: proto.Int32(0), Length: proto.Uint32(5)},
}, },
NegativeDelta: []int64{1, -1, 2, -2, 2}, NegativeDelta: []int64{1, -1, 2, -2, 2},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -836,6 +857,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(0), Length: proto.Uint32(5)}, {Offset: proto.Int32(0), Length: proto.Uint32(5)},
}, },
NegativeDelta: []int64{1, 2, -1, -2, 1}, NegativeDelta: []int64{1, 2, -1, -2, 1},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -854,6 +876,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(1), Length: proto.Uint32(7)}, {Offset: proto.Int32(1), Length: proto.Uint32(7)},
}, },
NegativeDelta: []int64{1, 1, -2, 2, -2, 0, 1}, NegativeDelta: []int64{1, 1, -2, 2, -2, 0, 1},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -872,6 +895,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(2), Length: proto.Uint32(7)}, {Offset: proto.Int32(2), Length: proto.Uint32(7)},
}, },
NegativeDelta: []int64{2, -2, 2, -2, 0, 1, 0}, NegativeDelta: []int64{2, -2, 2, -2, 0, 1, 0},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -891,6 +915,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(7), Length: proto.Uint32(2)}, {Offset: proto.Int32(7), Length: proto.Uint32(2)},
}, },
NegativeDelta: []int64{1, 0}, NegativeDelta: []int64{1, 0},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -909,6 +934,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(7), Length: proto.Uint32(2)}, {Offset: proto.Int32(7), Length: proto.Uint32(2)},
}, },
PositiveDelta: []int64{1, 0}, PositiveDelta: []int64{1, 0},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
{ {
@ -928,6 +954,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(7), Length: proto.Uint32(2)}, {Offset: proto.Int32(7), Length: proto.Uint32(2)},
}, },
PositiveDelta: []int64{1, 0}, PositiveDelta: []int64{1, 0},
CreatedTimestamp: timestamppb.New(nowFn()),
}, },
}, },
} }
@ -942,6 +969,7 @@ func TestNativeHistogram(t *testing.T) {
NativeHistogramMaxBucketNumber: s.maxBuckets, NativeHistogramMaxBucketNumber: s.maxBuckets,
NativeHistogramMinResetDuration: s.minResetDuration, NativeHistogramMinResetDuration: s.minResetDuration,
NativeHistogramMaxZeroThreshold: s.maxZeroThreshold, NativeHistogramMaxZeroThreshold: s.maxZeroThreshold,
now: nowFn,
}) })
ts := time.Now().Add(30 * time.Second) ts := time.Now().Add(30 * time.Second)
now := func() time.Time { now := func() time.Time {

View File

@ -37,6 +37,7 @@ import (
dto "github.com/prometheus/client_model/go" dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt" "github.com/prometheus/common/expfmt"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
) )
// uncheckedCollector wraps a Collector but its Describe method yields no Desc. // uncheckedCollector wraps a Collector but its Describe method yields no Desc.
@ -139,6 +140,7 @@ metric: <
}, },
Counter: &dto.Counter{ Counter: &dto.Counter{
Value: proto.Float64(1), Value: proto.Float64(1),
CreatedTimestamp: timestamppb.New(time.Now()),
}, },
}, },
{ {
@ -154,6 +156,7 @@ metric: <
}, },
Counter: &dto.Counter{ Counter: &dto.Counter{
Value: proto.Float64(1), Value: proto.Float64(1),
CreatedTimestamp: timestamppb.New(time.Now()),
}, },
}, },
}, },

View File

@ -17,6 +17,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
"time"
dto "github.com/prometheus/client_model/go" dto "github.com/prometheus/client_model/go"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
@ -43,9 +44,12 @@ func toMetricFamilies(cs ...Collector) []*dto.MetricFamily {
} }
func TestWrap(t *testing.T) { func TestWrap(t *testing.T) {
now := time.Now()
nowFn := func() time.Time { return now }
simpleCnt := NewCounter(CounterOpts{ simpleCnt := NewCounter(CounterOpts{
Name: "simpleCnt", Name: "simpleCnt",
Help: "helpSimpleCnt", Help: "helpSimpleCnt",
now: nowFn,
}) })
simpleCnt.Inc() simpleCnt.Inc()
@ -58,6 +62,7 @@ func TestWrap(t *testing.T) {
preCnt := NewCounter(CounterOpts{ preCnt := NewCounter(CounterOpts{
Name: "pre_simpleCnt", Name: "pre_simpleCnt",
Help: "helpSimpleCnt", Help: "helpSimpleCnt",
now: nowFn,
}) })
preCnt.Inc() preCnt.Inc()
@ -65,6 +70,7 @@ func TestWrap(t *testing.T) {
Name: "simpleCnt", Name: "simpleCnt",
Help: "helpSimpleCnt", Help: "helpSimpleCnt",
ConstLabels: Labels{"foo": "bar"}, ConstLabels: Labels{"foo": "bar"},
now: nowFn,
}) })
barLabeledCnt.Inc() barLabeledCnt.Inc()
@ -72,6 +78,7 @@ func TestWrap(t *testing.T) {
Name: "simpleCnt", Name: "simpleCnt",
Help: "helpSimpleCnt", Help: "helpSimpleCnt",
ConstLabels: Labels{"foo": "baz"}, ConstLabels: Labels{"foo": "baz"},
now: nowFn,
}) })
bazLabeledCnt.Inc() bazLabeledCnt.Inc()
@ -79,6 +86,7 @@ func TestWrap(t *testing.T) {
Name: "pre_simpleCnt", Name: "pre_simpleCnt",
Help: "helpSimpleCnt", Help: "helpSimpleCnt",
ConstLabels: Labels{"foo": "bar"}, ConstLabels: Labels{"foo": "bar"},
now: nowFn,
}) })
labeledPreCnt.Inc() labeledPreCnt.Inc()
@ -86,6 +94,7 @@ func TestWrap(t *testing.T) {
Name: "pre_simpleCnt", Name: "pre_simpleCnt",
Help: "helpSimpleCnt", Help: "helpSimpleCnt",
ConstLabels: Labels{"foo": "bar", "dings": "bums"}, ConstLabels: Labels{"foo": "bar", "dings": "bums"},
now: nowFn,
}) })
twiceLabeledPreCnt.Inc() twiceLabeledPreCnt.Inc()