1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-07-15 01:04:25 +02:00

Use uint64 Count consistently in metric aggregation (#1430)

* Use uint64 Count consistently

* Number
This commit is contained in:
Joshua MacDonald
2021-01-05 23:17:20 -08:00
committed by GitHub
parent 3a337d0b79
commit fe9d1f7ec5
16 changed files with 37 additions and 43 deletions

View File

@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- `NewExporter` from `exporters/otlp` now takes a `ProtocolDriver` as a parameter. (#1369) - `NewExporter` from `exporters/otlp` now takes a `ProtocolDriver` as a parameter. (#1369)
- Many OTLP Exporter options became gRPC ProtocolDriver options. (#1369) - Many OTLP Exporter options became gRPC ProtocolDriver options. (#1369)
- Unify endpoint API that related to OTel exporter. (#1401) - Unify endpoint API that related to OTel exporter. (#1401)
- Metric aggregator Count() and histogram Bucket.Counts are consistently `uint64`. (1430)
### Removed ### Removed

View File

@ -458,7 +458,7 @@ func sumPoint(record export.Record, num number.Number, start, end time.Time, ek
// minMaxSumCountValue returns the values of the MinMaxSumCount Aggregator // minMaxSumCountValue returns the values of the MinMaxSumCount Aggregator
// as discrete values. // as discrete values.
func minMaxSumCountValues(a aggregation.MinMaxSumCount) (min, max, sum number.Number, count int64, err error) { func minMaxSumCountValues(a aggregation.MinMaxSumCount) (min, max, sum number.Number, count uint64, err error) {
if min, err = a.Min(); err != nil { if min, err = a.Min(); err != nil {
return return
} }
@ -531,7 +531,7 @@ func minMaxSumCount(record export.Record, a aggregation.MinMaxSumCount) (*metric
return m, nil return m, nil
} }
func histogramValues(a aggregation.Histogram) (boundaries []float64, counts []float64, err error) { func histogramValues(a aggregation.Histogram) (boundaries []float64, counts []uint64, err error) {
var buckets aggregation.Buckets var buckets aggregation.Buckets
if buckets, err = a.Histogram(); err != nil { if buckets, err = a.Histogram(); err != nil {
return return
@ -563,10 +563,6 @@ func histogramPoint(record export.Record, ek export.ExportKind, a aggregation.Hi
return nil, err return nil, err
} }
buckets := make([]uint64, len(counts))
for i := 0; i < len(counts); i++ {
buckets[i] = uint64(counts[i])
}
m := &metricpb.Metric{ m := &metricpb.Metric{
Name: desc.Name(), Name: desc.Name(),
Description: desc.Description(), Description: desc.Description(),
@ -584,7 +580,7 @@ func histogramPoint(record export.Record, ek export.ExportKind, a aggregation.Hi
StartTimeUnixNano: toNanos(record.StartTime()), StartTimeUnixNano: toNanos(record.StartTime()),
TimeUnixNano: toNanos(record.EndTime()), TimeUnixNano: toNanos(record.EndTime()),
Count: uint64(count), Count: uint64(count),
BucketCounts: buckets, BucketCounts: counts,
ExplicitBounds: boundaries, ExplicitBounds: boundaries,
}, },
}, },
@ -601,7 +597,7 @@ func histogramPoint(record export.Record, ek export.ExportKind, a aggregation.Hi
StartTimeUnixNano: toNanos(record.StartTime()), StartTimeUnixNano: toNanos(record.StartTime()),
TimeUnixNano: toNanos(record.EndTime()), TimeUnixNano: toNanos(record.EndTime()),
Count: uint64(count), Count: uint64(count),
BucketCounts: buckets, BucketCounts: counts,
ExplicitBounds: boundaries, ExplicitBounds: boundaries,
}, },
}, },

View File

@ -117,7 +117,7 @@ func TestMinMaxSumCountValue(t *testing.T) {
assert.Equal(t, min, number.NewInt64Number(1)) assert.Equal(t, min, number.NewInt64Number(1))
assert.Equal(t, max, number.NewInt64Number(10)) assert.Equal(t, max, number.NewInt64Number(10))
assert.Equal(t, sum, number.NewInt64Number(11)) assert.Equal(t, sum, number.NewInt64Number(11))
assert.Equal(t, count, int64(2)) assert.Equal(t, count, uint64(2))
} }
} }
@ -369,7 +369,7 @@ func (te *testErrMinMaxSumCount) Max() (number.Number, error) {
return 0, te.err return 0, te.err
} }
func (te *testErrMinMaxSumCount) Count() (int64, error) { func (te *testErrMinMaxSumCount) Count() (uint64, error) {
return 0, te.err return 0, te.err
} }

View File

@ -43,7 +43,7 @@ type (
// Count returns the number of values that were aggregated. // Count returns the number of values that were aggregated.
Count interface { Count interface {
Aggregation Aggregation
Count() (int64, error) Count() (uint64, error)
} }
// Min returns the minimum value over the set of values that were aggregated. // Min returns the minimum value over the set of values that were aggregated.
@ -86,16 +86,14 @@ type (
// aggregating integers. // aggregating integers.
Boundaries []float64 Boundaries []float64
// Counts are floating point numbers to account for // Counts holds the count in each bucket.
// the possibility of sampling which allows for Counts []uint64
// non-integer count values.
Counts []float64
} }
// Histogram returns the count of events in pre-determined buckets. // Histogram returns the count of events in pre-determined buckets.
Histogram interface { Histogram interface {
Aggregation Aggregation
Count() (int64, error) Count() (uint64, error)
Sum() (number.Number, error) Sum() (number.Number, error)
Histogram() (Buckets, error) Histogram() (Buckets, error)
} }
@ -106,7 +104,7 @@ type (
Min() (number.Number, error) Min() (number.Number, error)
Max() (number.Number, error) Max() (number.Number, error)
Sum() (number.Number, error) Sum() (number.Number, error)
Count() (int64, error) Count() (uint64, error)
} }
// Distribution supports the Min, Max, Sum, Count, and Quantile // Distribution supports the Min, Max, Sum, Count, and Quantile
@ -116,7 +114,7 @@ type (
Min() (number.Number, error) Min() (number.Number, error)
Max() (number.Number, error) Max() (number.Number, error)
Sum() (number.Number, error) Sum() (number.Number, error)
Count() (int64, error) Count() (uint64, error)
Quantile(float64) (number.Number, error) Quantile(float64) (number.Number, error)
} }
) )

View File

@ -134,8 +134,8 @@ func (n *Numbers) Sum() number.Number {
return sum return sum
} }
func (n *Numbers) Count() int64 { func (n *Numbers) Count() uint64 {
return int64(len(n.numbers)) return uint64(len(n.numbers))
} }
func (n *Numbers) Min() number.Number { func (n *Numbers) Min() number.Number {
@ -223,7 +223,7 @@ func SynchronizedMoveResetTest(t *testing.T, mkind metric.InstrumentKind, nf fun
if count, ok := agg.(aggregation.Count); ok { if count, ok := agg.(aggregation.Count); ok {
c, err := count.Count() c, err := count.Count()
require.Equal(t, int64(0), c) require.Equal(t, uint64(0), c)
require.NoError(t, err) require.NoError(t, err)
} }
@ -270,7 +270,7 @@ func SynchronizedMoveResetTest(t *testing.T, mkind metric.InstrumentKind, nf fun
if count, ok := agg.(aggregation.Count); ok { if count, ok := agg.(aggregation.Count); ok {
c, err := count.Count() c, err := count.Count()
require.Equal(t, int64(1), c) require.Equal(t, uint64(1), c)
require.NoError(t, err) require.NoError(t, err)
} }

View File

@ -68,8 +68,8 @@ func (c *Aggregator) Sum() (number.Number, error) {
} }
// Count returns the number of values in the checkpoint. // Count returns the number of values in the checkpoint.
func (c *Aggregator) Count() (int64, error) { func (c *Aggregator) Count() (uint64, error) {
return int64(len(c.points)), nil return uint64(len(c.points)), nil
} }
// Max returns the maximum value in the checkpoint. // Max returns the maximum value in the checkpoint.

View File

@ -42,7 +42,7 @@ func checkZero(t *testing.T, agg *Aggregator, desc *metric.Descriptor) {
count, err := agg.Count() count, err := agg.Count()
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, int64(0), count) require.Equal(t, uint64(0), count)
max, err := agg.Max() max, err := agg.Max()
require.True(t, errors.Is(err, aggregation.ErrNoData)) require.True(t, errors.Is(err, aggregation.ErrNoData))
@ -237,7 +237,7 @@ func TestArrayErrors(t *testing.T) {
require.NoError(t, agg.SynchronizedMove(ckpt, descriptor)) require.NoError(t, agg.SynchronizedMove(ckpt, descriptor))
count, err := ckpt.Count() count, err := ckpt.Count()
require.Equal(t, int64(1), count, "NaN value was not counted") require.Equal(t, uint64(1), count, "NaN value was not counted")
require.Nil(t, err) require.Nil(t, err)
num, err := ckpt.Quantile(0) num, err := ckpt.Quantile(0)

View File

@ -79,8 +79,8 @@ func (c *Aggregator) Sum() (number.Number, error) {
} }
// Count returns the number of values in the checkpoint. // Count returns the number of values in the checkpoint.
func (c *Aggregator) Count() (int64, error) { func (c *Aggregator) Count() (uint64, error) {
return c.sketch.Count(), nil return uint64(c.sketch.Count()), nil
} }
// Max returns the maximum value in the checkpoint. // Max returns the maximum value in the checkpoint.

View File

@ -51,7 +51,7 @@ func checkZero(t *testing.T, agg *Aggregator, desc *metric.Descriptor) {
count, err := agg.Count() count, err := agg.Count()
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, int64(0), count) require.Equal(t, uint64(0), count)
max, err := agg.Max() max, err := agg.Max()
require.True(t, errors.Is(err, aggregation.ErrNoData)) require.True(t, errors.Is(err, aggregation.ErrNoData))

View File

@ -45,9 +45,9 @@ type (
// the sum and counts for all observed values and // the sum and counts for all observed values and
// the less than equal bucket count for the pre-determined boundaries. // the less than equal bucket count for the pre-determined boundaries.
state struct { state struct {
bucketCounts []float64 bucketCounts []uint64
sum number.Number sum number.Number
count int64 count uint64
} }
) )
@ -100,7 +100,7 @@ func (c *Aggregator) Sum() (number.Number, error) {
} }
// Count returns the number of values in the checkpoint. // Count returns the number of values in the checkpoint.
func (c *Aggregator) Count() (int64, error) { func (c *Aggregator) Count() (uint64, error) {
return c.state.count, nil return c.state.count, nil
} }
@ -135,7 +135,7 @@ func (c *Aggregator) SynchronizedMove(oa export.Aggregator, desc *metric.Descrip
func emptyState(boundaries []float64) state { func emptyState(boundaries []float64) state {
return state{ return state{
bucketCounts: make([]float64, len(boundaries)+1), bucketCounts: make([]uint64, len(boundaries)+1),
} }
} }

View File

@ -78,7 +78,7 @@ func checkZero(t *testing.T, agg *histogram.Aggregator, desc *metric.Descriptor)
require.NoError(t, err) require.NoError(t, err)
count, err := agg.Count() count, err := agg.Count()
require.Equal(t, int64(0), count, "Empty checkpoint count = 0") require.Equal(t, uint64(0), count, "Empty checkpoint count = 0")
require.NoError(t, err) require.NoError(t, err)
buckets, err := agg.Histogram() buckets, err := agg.Histogram()

View File

@ -38,7 +38,7 @@ type (
sum number.Number sum number.Number
min number.Number min number.Number
max number.Number max number.Number
count int64 count uint64
} }
) )
@ -78,7 +78,7 @@ func (c *Aggregator) Sum() (number.Number, error) {
} }
// Count returns the number of values in the checkpoint. // Count returns the number of values in the checkpoint.
func (c *Aggregator) Count() (int64, error) { func (c *Aggregator) Count() (uint64, error) {
return c.count, nil return c.count, nil
} }

View File

@ -97,7 +97,7 @@ func checkZero(t *testing.T, agg *Aggregator, desc *metric.Descriptor) {
count, err := agg.Count() count, err := agg.Count()
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, int64(0), count) require.Equal(t, uint64(0), count)
max, err := agg.Max() max, err := agg.Max()
require.True(t, errors.Is(err, aggregation.ErrNoData)) require.True(t, errors.Is(err, aggregation.ErrNoData))
@ -228,7 +228,7 @@ func TestMaxSumCountNotSet(t *testing.T) {
require.Nil(t, err) require.Nil(t, err)
count, err := ckpt.Count() count, err := ckpt.Count()
require.Equal(t, int64(0), count, "Empty checkpoint count = 0") require.Equal(t, uint64(0), count, "Empty checkpoint count = 0")
require.Nil(t, err) require.Nil(t, err)
max, err := ckpt.Max() max, err := ckpt.Max()

View File

@ -168,7 +168,7 @@ func TestInputRangeValueRecorder(t *testing.T) {
checkpointed = sdk.Collect(ctx) checkpointed = sdk.Collect(ctx)
count, err := processor.accumulations[0].Aggregator().(aggregation.Distribution).Count() count, err := processor.accumulations[0].Aggregator().(aggregation.Distribution).Count()
require.Equal(t, int64(2), count) require.Equal(t, uint64(2), count)
require.Equal(t, 1, checkpointed) require.Equal(t, 1, checkpointed)
require.Nil(t, testHandler.Flush()) require.Nil(t, testHandler.Flush())
require.Nil(t, err) require.Nil(t, err)

View File

@ -54,10 +54,9 @@ func TestStressInt64Histogram(t *testing.T) {
b, _ := ckpt.Histogram() b, _ := ckpt.Histogram()
c, _ := ckpt.Count() c, _ := ckpt.Count()
var realCount int64 var realCount uint64
for _, c := range b.Counts { for _, c := range b.Counts {
v := int64(c) realCount += c
realCount += v
} }
if realCount != c { if realCount != c {

View File

@ -63,7 +63,7 @@ func TestStressInt64MinMaxSumCount(t *testing.T) {
} }
lo, hi, sum := min.AsInt64(), max.AsInt64(), s.AsInt64() lo, hi, sum := min.AsInt64(), max.AsInt64(), s.AsInt64()
if hi-lo+1 != c { if uint64(hi-lo)+1 != c {
t.Fail() t.Fail()
} }
if c == 1 { if c == 1 {