1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-06 09:09:44 +02:00

Test metric aggregate times (#5323)

This commit is contained in:
Tyler Yahn
2024-05-09 11:11:32 -07:00
committed by GitHub
parent d0068d09e7
commit 9e7d7446c3
5 changed files with 174 additions and 117 deletions

View File

@@ -6,6 +6,7 @@ package aggregate // import "go.opentelemetry.io/otel/sdk/metric/internal/aggreg
import (
"context"
"strconv"
"sync/atomic"
"testing"
"time"
@@ -39,16 +40,39 @@ var (
fltrBob = attribute.NewSet(userBob)
// Sat Jan 01 2000 00:00:00 GMT+0000.
staticTime = time.Unix(946684800, 0)
staticNowFunc = func() time.Time { return staticTime }
// Pass to t.Cleanup to override the now function with staticNowFunc and
// revert once the test completes. E.g. t.Cleanup(mockTime(now)).
mockTime = func(orig func() time.Time) (cleanup func()) {
now = staticNowFunc
return func() { now = orig }
}
y2k = time.Unix(946684800, 0)
)
// y2kPlus returns the timestamp at n seconds past Sat Jan 01 2000 00:00:00 GMT+0000.
func y2kPlus(n int64) time.Time {
d := time.Duration(n) * time.Second
return y2k.Add(d)
}
// clock is a test clock. It provides a predictable value for now() that can be
// reset.
type clock struct {
ticks atomic.Int64
}
// Now returns the mocked time starting at y2kPlus(0). Each call to Now will
// increment the returned value by one second.
func (c *clock) Now() time.Time {
old := c.ticks.Add(1) - 1
return y2kPlus(old)
}
// Reset resets the clock c to tick from y2kPlus(0).
func (c *clock) Reset() { c.ticks.Store(0) }
// Register registers clock c's Now method as the now var. It returns an
// unregister func that should be called to restore the original now value.
func (c *clock) Register() (unregister func()) {
orig := now
now = c.Now
return func() { now = orig }
}
func dropExemplars[N int64 | float64]() exemplar.Reservoir {
return exemplar.Drop()
}