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) {
now := time.Now()
nowFn := func() time.Time { return now }
counter := NewCounter(CounterOpts{
Name: "test",
Help: "test help",
ConstLabels: Labels{"a": "1", "b": "2"},
now: nowFn,
}).(*counter)
counter.Inc()
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("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) {
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) {
now := time.Now()
nowFn := func() time.Time { return now }
counter := NewCounter(CounterOpts{
Name: "test",
Help: "test help",
now: nowFn,
}).(*counter)
counter.Inc()
@ -173,7 +182,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(nowFn()),
},
}
@ -183,9 +193,12 @@ func TestCounterAddInf(t *testing.T) {
}
func TestCounterAddLarge(t *testing.T) {
now := time.Now()
nowFn := func() time.Time { return now }
counter := NewCounter(CounterOpts{
Name: "test",
Help: "test help",
now: nowFn,
}).(*counter)
// large overflows the underlying type and should therefore be stored in valBits.
@ -203,7 +216,8 @@ func TestCounterAddLarge(t *testing.T) {
expected := &dto.Metric{
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) {
now := time.Now()
nowFn := func() time.Time { return now }
counter := NewCounter(CounterOpts{
Name: "test",
Help: "test help",
now: nowFn,
}).(*counter)
small := 0.000000000001
counter.Add(small)
@ -231,7 +248,8 @@ func TestCounterAddSmall(t *testing.T) {
expected := &dto.Metric{
Counter: &dto.Counter{
Value: proto.Float64(small),
Value: proto.Float64(small),
CreatedTimestamp: timestamppb.New(nowFn()),
},
}

View File

@ -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:

View File

@ -469,6 +469,8 @@ func TestHistogramExemplar(t *testing.T) {
}
func TestNativeHistogram(t *testing.T) {
now := time.Now()
nowFn := func() time.Time { return now }
scenarios := []struct {
name string
observations []float64 // With simulated interval of 1m.
@ -499,17 +501,19 @@ func TestNativeHistogram(t *testing.T) {
{CumulativeCount: proto.Uint64(3), UpperBound: proto.Float64(5)},
{CumulativeCount: proto.Uint64(3), UpperBound: proto.Float64(10)},
},
CreatedTimestamp: timestamppb.New(nowFn()),
},
},
{
name: "no observations",
factor: 1.1,
want: &dto.Histogram{
SampleCount: proto.Uint64(0),
SampleSum: proto.Float64(0),
Schema: proto.Int32(3),
ZeroThreshold: proto.Float64(2.938735877055719e-39),
ZeroCount: proto.Uint64(0),
SampleCount: proto.Uint64(0),
SampleSum: proto.Float64(0),
Schema: proto.Int32(3),
ZeroThreshold: proto.Float64(2.938735877055719e-39),
ZeroCount: proto.Uint64(0),
CreatedTimestamp: timestamppb.New(nowFn()),
},
},
{
@ -525,6 +529,7 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{
{Offset: proto.Int32(0), Length: proto.Uint32(0)},
},
CreatedTimestamp: timestamppb.New(nowFn()),
},
},
{
@ -542,7 +547,8 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(7), 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()),
},
},
{
@ -558,7 +564,8 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{
{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()),
},
},
{
@ -581,7 +588,8 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{
{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()),
},
},
{
@ -602,7 +610,8 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{
{Offset: proto.Int32(-1), Length: proto.Uint32(4)},
},
PositiveDelta: []int64{2, 2, 3, -6},
PositiveDelta: []int64{2, 2, 3, -6},
CreatedTimestamp: timestamppb.New(nowFn()),
},
},
{
@ -618,7 +627,8 @@ func TestNativeHistogram(t *testing.T) {
NegativeSpan: []*dto.BucketSpan{
{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()),
},
},
{
@ -638,7 +648,8 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{
{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()),
},
},
{
@ -659,7 +670,8 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{
{Offset: proto.Int32(4), Length: proto.Uint32(1)},
},
PositiveDelta: []int64{2},
PositiveDelta: []int64{2},
CreatedTimestamp: timestamppb.New(nowFn()),
},
},
{
@ -675,7 +687,8 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{
{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()),
},
},
{
@ -692,7 +705,8 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(0), Length: proto.Uint32(5)},
{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()),
},
},
{
@ -712,7 +726,8 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{
{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()),
},
},
{
@ -729,7 +744,8 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{
{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()),
},
},
{
@ -746,7 +762,8 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{
{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()),
},
},
{
@ -764,7 +781,8 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{
{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()),
},
},
{
@ -782,7 +800,8 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{
{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()),
},
},
{
@ -801,7 +820,8 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{
{Offset: proto.Int32(7), Length: proto.Uint32(2)},
},
PositiveDelta: []int64{1, 0},
PositiveDelta: []int64{1, 0},
CreatedTimestamp: timestamppb.New(nowFn()),
},
},
{
@ -818,7 +838,8 @@ func TestNativeHistogram(t *testing.T) {
NegativeSpan: []*dto.BucketSpan{
{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()),
},
},
{
@ -835,7 +856,8 @@ func TestNativeHistogram(t *testing.T) {
NegativeSpan: []*dto.BucketSpan{
{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()),
},
},
{
@ -853,7 +875,8 @@ func TestNativeHistogram(t *testing.T) {
NegativeSpan: []*dto.BucketSpan{
{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()),
},
},
{
@ -871,7 +894,8 @@ func TestNativeHistogram(t *testing.T) {
NegativeSpan: []*dto.BucketSpan{
{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()),
},
},
{
@ -890,7 +914,8 @@ func TestNativeHistogram(t *testing.T) {
NegativeSpan: []*dto.BucketSpan{
{Offset: proto.Int32(7), Length: proto.Uint32(2)},
},
NegativeDelta: []int64{1, 0},
NegativeDelta: []int64{1, 0},
CreatedTimestamp: timestamppb.New(nowFn()),
},
},
{
@ -908,7 +933,8 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{
{Offset: proto.Int32(7), Length: proto.Uint32(2)},
},
PositiveDelta: []int64{1, 0},
PositiveDelta: []int64{1, 0},
CreatedTimestamp: timestamppb.New(nowFn()),
},
},
{
@ -927,7 +953,8 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{
{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,
NativeHistogramMinResetDuration: s.minResetDuration,
NativeHistogramMaxZeroThreshold: s.maxZeroThreshold,
now: nowFn,
})
ts := time.Now().Add(30 * time.Second)
now := func() time.Time {

View File

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

View File

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