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 <[email protected]>
  • Loading branch information
Arthur Silva Sens committed Aug 11, 2023
1 parent 5ce7416 commit 3f535a6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
18 changes: 16 additions & 2 deletions prometheus/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()),
},
}

Expand All @@ -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.
Expand All @@ -204,6 +214,7 @@ func TestCounterAddLarge(t *testing.T) {
expected := &dto.Metric{
Counter: &dto.Counter{
Value: proto.Float64(large),
CreatedTimestamp: timestamppb.New(now()),
},
}

Expand All @@ -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)
Expand All @@ -232,6 +245,7 @@ func TestCounterAddSmall(t *testing.T) {
expected := &dto.Metric{
Counter: &dto.Counter{
Value: proto.Float64(small),
CreatedTimestamp: timestamppb.New(now()),
},
}

Expand Down
27 changes: 27 additions & 0 deletions prometheus/histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -525,6 +528,7 @@ func TestNativeHistogram(t *testing.T) {
PositiveSpan: []*dto.BucketSpan{
{Offset: proto.Int32(0), Length: proto.Uint32(0)},
},
CreatedTimestamp: timestamppb.New(now()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -660,6 +670,7 @@ func TestNativeHistogram(t *testing.T) {
{Offset: proto.Int32(4), Length: proto.Uint32(1)},
},
PositiveDelta: []int64{2},
CreatedTimestamp: timestamppb.New(now()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
{
Expand All @@ -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()),
},
},
}
Expand All @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions prometheus/wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()

Expand All @@ -58,34 +62,39 @@ func TestWrap(t *testing.T) {
preCnt := NewCounter(CounterOpts{
Name: "pre_simpleCnt",
Help: "helpSimpleCnt",
now: now,
})
preCnt.Inc()

barLabeledCnt := NewCounter(CounterOpts{
Name: "simpleCnt",
Help: "helpSimpleCnt",
ConstLabels: Labels{"foo": "bar"},
now: now,
})
barLabeledCnt.Inc()

bazLabeledCnt := NewCounter(CounterOpts{
Name: "simpleCnt",
Help: "helpSimpleCnt",
ConstLabels: Labels{"foo": "baz"},
now: now,
})
bazLabeledCnt.Inc()

labeledPreCnt := NewCounter(CounterOpts{
Name: "pre_simpleCnt",
Help: "helpSimpleCnt",
ConstLabels: Labels{"foo": "bar"},
now: now,
})
labeledPreCnt.Inc()

twiceLabeledPreCnt := NewCounter(CounterOpts{
Name: "pre_simpleCnt",
Help: "helpSimpleCnt",
ConstLabels: Labels{"foo": "bar", "dings": "bums"},
now: now,
})
twiceLabeledPreCnt.Inc()

Expand Down

0 comments on commit 3f535a6

Please sign in to comment.