Skip to content

Commit

Permalink
Backport created timestamp to existing tests
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Silva Sens <arthur.sens@coralogix.com>
  • Loading branch information
ArthurSens committed Aug 24, 2023
1 parent 3c7e78c commit 226eb8d
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 33 deletions.
26 changes: 22 additions & 4 deletions prometheus/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()),
},
}

Expand All @@ -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.
Expand All @@ -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()),
},
}

Expand All @@ -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)
Expand All @@ -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()),
},
}

Expand Down
10 changes: 10 additions & 0 deletions prometheus/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
82 changes: 55 additions & 27 deletions prometheus/histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()),
},
},
{
Expand All @@ -525,6 +529,7 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{
{Offset: proto.Int32(0), Length: proto.Uint32(0)},
},
CreatedTimestamp: timestamppb.New(nowFn()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
}
Expand All @@ -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 {
Expand Down

0 comments on commit 226eb8d

Please sign in to comment.