Skip to content

Commit

Permalink
Split NewPrecomputedSum into delta/cumulative vers
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Oct 19, 2022
1 parent 9dd140e commit 144a05b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
32 changes: 19 additions & 13 deletions sdk/metric/internal/sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,26 +166,32 @@ func (s *cumulativeSum[N]) Aggregation() metricdata.Aggregation {
return out
}

// NewPrecomputedSum returns an Aggregator that summarizes a set of
// NewPrecomputedDeltaSum returns an Aggregator that summarizes a set of
// measurements as their pre-computed arithmetic sum. Each sum is scoped by
// attributes and the aggregation cycle the measurements were made in.
//
// The monotonic value is used to communicate the produced Aggregation is
// monotonic or not. The returned Aggregator does not make any guarantees this
// value is accurate. It is up to the caller to ensure it.
//
// The temporality value is used to communicate the produced Aggregation
// temporality. The returned Aggregator does not make any guarantees this value
// is accurate. It is up to the caller to ensure it.
func NewPrecomputedSum[N int64 | float64](monotonic bool, temporality metricdata.Temporality) Aggregator[N] {
var s settableSum[N]
switch temporality {
case metricdata.DeltaTemporality:
s = newDeltaSum[N](monotonic)
default:
s = newCumulativeSum[N](monotonic)
}
return &precomputedSum[N]{settableSum: s}
// The output Aggregation will report recorded values as delta temporality. It
// is up to the caller to ensure this is accurate.
func NewPrecomputedDeltaSum[N int64 | float64](monotonic bool) Aggregator[N] {
return &precomputedSum[N]{settableSum: newDeltaSum[N](monotonic)}
}

// NewPrecomputedCumulativeSum returns an Aggregator that summarizes a set of
// measurements as their pre-computed arithmetic sum. Each sum is scoped by
// attributes and the aggregation cycle the measurements were made in.
//
// The monotonic value is used to communicate the produced Aggregation is
// monotonic or not. The returned Aggregator does not make any guarantees this
// value is accurate. It is up to the caller to ensure it.
//
// The output Aggregation will report recorded values as cumulative
// temporality. It is up to the caller to ensure this is accurate.
func NewPrecomputedCumulativeSum[N int64 | float64](monotonic bool) Aggregator[N] {
return &precomputedSum[N]{settableSum: newCumulativeSum[N](monotonic)}
}

type settableSum[N int64 | float64] interface {
Expand Down
20 changes: 11 additions & 9 deletions sdk/metric/internal/sum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,24 @@ func testSum[N int64 | float64](t *testing.T) {
t.Run("NonMonotonic", tester.Run(NewCumulativeSum[N](mono), incr, eFunc))
})

t.Run("PreComputed", func(t *testing.T) {
t.Run("PreComputedDelta", func(t *testing.T) {
incr, mono, temp := monoIncr, true, metricdata.DeltaTemporality
eFunc := preExpecter[N](incr, mono, temp)
t.Run("Monotonic/Delta", tester.Run(NewPrecomputedSum[N](mono, temp), incr, eFunc))
t.Run("Monotonic", tester.Run(NewPrecomputedDeltaSum[N](mono), incr, eFunc))

temp = metricdata.CumulativeTemporality
incr, mono = nonMonoIncr, false
eFunc = preExpecter[N](incr, mono, temp)
t.Run("Monotonic/Cumulative", tester.Run(NewPrecomputedSum[N](mono, temp), incr, eFunc))
t.Run("NonMonotonic", tester.Run(NewPrecomputedDeltaSum[N](mono), incr, eFunc))
})

incr, mono, temp = nonMonoIncr, false, metricdata.DeltaTemporality
eFunc = preExpecter[N](incr, mono, temp)
t.Run("NonMonotonic/Delta", tester.Run(NewPrecomputedSum[N](mono, temp), incr, eFunc))
t.Run("PreComputedCumulative", func(t *testing.T) {
incr, mono, temp := monoIncr, true, metricdata.CumulativeTemporality
eFunc := preExpecter[N](incr, mono, temp)
t.Run("Monotonic", tester.Run(NewPrecomputedCumulativeSum[N](mono), incr, eFunc))

temp = metricdata.CumulativeTemporality
incr, mono = nonMonoIncr, false
eFunc = preExpecter[N](incr, mono, temp)
t.Run("NonMonotonic/Cumulative", tester.Run(NewPrecomputedSum[N](mono, temp), incr, eFunc))
t.Run("NonMonotonic", tester.Run(NewPrecomputedCumulativeSum[N](mono), incr, eFunc))
})
}

Expand Down
9 changes: 8 additions & 1 deletion sdk/metric/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,14 @@ func (i *inserter[N]) aggregator(agg aggregation.Aggregation, kind view.Instrume
// Asynchronous counters and up-down-counters are defined to record
// the absolute value of the count:
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#asynchronous-counter-creation
return internal.NewPrecomputedSum[N](monotonic, temporality), nil
switch temporality {
case metricdata.CumulativeTemporality:
return internal.NewPrecomputedCumulativeSum[N](monotonic), nil
case metricdata.DeltaTemporality:
return internal.NewPrecomputedDeltaSum[N](monotonic), nil
default:
return nil, fmt.Errorf("%w: %s(%d)", errUnknownTemporality, temporality.String(), temporality)
}
}

switch temporality {
Expand Down
9 changes: 4 additions & 5 deletions sdk/metric/pipeline_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"go.opentelemetry.io/otel/metric/unit"
"go.opentelemetry.io/otel/sdk/metric/aggregation"
"go.opentelemetry.io/otel/sdk/metric/internal"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/metric/view"
"go.opentelemetry.io/otel/sdk/resource"
)
Expand Down Expand Up @@ -108,15 +107,15 @@ func testCreateAggregators[N int64 | float64](t *testing.T) {
reader: NewManualReader(WithTemporalitySelector(deltaTemporalitySelector)),
views: []view.View{defaultAggView},
inst: instruments[view.AsyncCounter],
wantKind: internal.NewPrecomputedSum[N](true, metricdata.DeltaTemporality),
wantKind: internal.NewPrecomputedDeltaSum[N](true),
wantLen: 1,
},
{
name: "default agg should use reader",
reader: NewManualReader(WithTemporalitySelector(deltaTemporalitySelector)),
views: []view.View{defaultAggView},
inst: instruments[view.AsyncUpDownCounter],
wantKind: internal.NewPrecomputedSum[N](false, metricdata.DeltaTemporality),
wantKind: internal.NewPrecomputedDeltaSum[N](false),
wantLen: 1,
},
{
Expand Down Expand Up @@ -156,15 +155,15 @@ func testCreateAggregators[N int64 | float64](t *testing.T) {
reader: NewManualReader(),
views: []view.View{{}},
inst: instruments[view.AsyncCounter],
wantKind: internal.NewPrecomputedSum[N](true, metricdata.CumulativeTemporality),
wantKind: internal.NewPrecomputedCumulativeSum[N](true),
wantLen: 1,
},
{
name: "reader should set default agg",
reader: NewManualReader(),
views: []view.View{{}},
inst: instruments[view.AsyncUpDownCounter],
wantKind: internal.NewPrecomputedSum[N](false, metricdata.CumulativeTemporality),
wantKind: internal.NewPrecomputedCumulativeSum[N](false),
wantLen: 1,
},
{
Expand Down

0 comments on commit 144a05b

Please sign in to comment.