2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-11-15 23:01:20 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package simple // import "go.opentelemetry.io/otel/sdk/metric/selector/simple"
|
|
|
|
|
|
|
|
import (
|
2021-08-12 01:02:28 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/sdkapi"
|
2019-11-15 23:01:20 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
2021-01-12 20:19:13 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/exact"
|
2020-04-01 23:36:37 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
|
2020-09-25 00:30:55 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/lastvalue"
|
2019-11-27 00:07:58 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/minmaxsumcount"
|
2020-03-12 05:21:34 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/sum"
|
2019-11-15 23:01:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
selectorInexpensive struct{}
|
|
|
|
selectorExact struct{}
|
2021-01-12 20:19:13 +02:00
|
|
|
selectorHistogram struct {
|
2021-01-16 01:29:02 +02:00
|
|
|
options []histogram.Option
|
2020-04-01 23:36:37 +02:00
|
|
|
}
|
2019-11-15 23:01:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-06-23 19:51:15 +02:00
|
|
|
_ export.AggregatorSelector = selectorInexpensive{}
|
|
|
|
_ export.AggregatorSelector = selectorExact{}
|
|
|
|
_ export.AggregatorSelector = selectorHistogram{}
|
2019-11-15 23:01:20 +02:00
|
|
|
)
|
|
|
|
|
2021-01-12 20:19:13 +02:00
|
|
|
// NewWithInexpensiveDistribution returns a simple aggregator selector
|
2021-09-01 22:38:37 +02:00
|
|
|
// that uses minmaxsumcount aggregators for `Histogram`
|
2021-01-12 20:19:13 +02:00
|
|
|
// instruments. This selector is faster and uses less memory than the
|
|
|
|
// others in this package because minmaxsumcount aggregators maintain
|
|
|
|
// the least information about the distribution among these choices.
|
2020-06-23 19:51:15 +02:00
|
|
|
func NewWithInexpensiveDistribution() export.AggregatorSelector {
|
2019-11-15 23:01:20 +02:00
|
|
|
return selectorInexpensive{}
|
|
|
|
}
|
|
|
|
|
2021-01-12 20:19:13 +02:00
|
|
|
// NewWithExactDistribution returns a simple aggregator selector that
|
2021-09-01 22:38:37 +02:00
|
|
|
// uses exact aggregators for `Histogram` instruments. This
|
2021-01-12 20:19:13 +02:00
|
|
|
// selector uses more memory than the others in this package because
|
|
|
|
// exact aggregators maintain the most information about the
|
|
|
|
// distribution among these choices.
|
2020-06-23 19:51:15 +02:00
|
|
|
func NewWithExactDistribution() export.AggregatorSelector {
|
2019-11-15 23:01:20 +02:00
|
|
|
return selectorExact{}
|
|
|
|
}
|
|
|
|
|
2021-01-12 20:19:13 +02:00
|
|
|
// NewWithHistogramDistribution returns a simple aggregator selector
|
2021-09-01 22:38:37 +02:00
|
|
|
// that uses histogram aggregators for `Histogram` instruments.
|
2021-01-12 20:19:13 +02:00
|
|
|
// This selector is a good default choice for most metric exporters.
|
2021-01-16 01:29:02 +02:00
|
|
|
func NewWithHistogramDistribution(options ...histogram.Option) export.AggregatorSelector {
|
|
|
|
return selectorHistogram{options: options}
|
2020-04-01 23:36:37 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
func sumAggs(aggPtrs []*export.Aggregator) {
|
|
|
|
aggs := sum.New(len(aggPtrs))
|
|
|
|
for i := range aggPtrs {
|
|
|
|
*aggPtrs[i] = &aggs[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 00:30:55 +02:00
|
|
|
func lastValueAggs(aggPtrs []*export.Aggregator) {
|
|
|
|
aggs := lastvalue.New(len(aggPtrs))
|
|
|
|
for i := range aggPtrs {
|
|
|
|
*aggPtrs[i] = &aggs[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
func (selectorInexpensive) AggregatorFor(descriptor *sdkapi.Descriptor, aggPtrs ...*export.Aggregator) {
|
2020-10-13 16:55:31 +02:00
|
|
|
switch descriptor.InstrumentKind() {
|
2021-09-01 22:38:37 +02:00
|
|
|
case sdkapi.GaugeObserverInstrumentKind:
|
2020-09-25 00:30:55 +02:00
|
|
|
lastValueAggs(aggPtrs)
|
2021-09-01 22:38:37 +02:00
|
|
|
case sdkapi.HistogramInstrumentKind:
|
2020-06-13 09:55:01 +02:00
|
|
|
aggs := minmaxsumcount.New(len(aggPtrs), descriptor)
|
|
|
|
for i := range aggPtrs {
|
|
|
|
*aggPtrs[i] = &aggs[i]
|
|
|
|
}
|
2019-11-15 23:01:20 +02:00
|
|
|
default:
|
2020-06-13 09:55:01 +02:00
|
|
|
sumAggs(aggPtrs)
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
func (selectorExact) AggregatorFor(descriptor *sdkapi.Descriptor, aggPtrs ...*export.Aggregator) {
|
2020-10-13 16:55:31 +02:00
|
|
|
switch descriptor.InstrumentKind() {
|
2021-09-01 22:38:37 +02:00
|
|
|
case sdkapi.GaugeObserverInstrumentKind:
|
2020-09-25 00:30:55 +02:00
|
|
|
lastValueAggs(aggPtrs)
|
2021-09-01 22:38:37 +02:00
|
|
|
case sdkapi.HistogramInstrumentKind:
|
2021-01-12 20:19:13 +02:00
|
|
|
aggs := exact.New(len(aggPtrs))
|
2020-06-13 09:55:01 +02:00
|
|
|
for i := range aggPtrs {
|
|
|
|
*aggPtrs[i] = &aggs[i]
|
|
|
|
}
|
2019-11-15 23:01:20 +02:00
|
|
|
default:
|
2020-06-13 09:55:01 +02:00
|
|
|
sumAggs(aggPtrs)
|
2019-11-15 23:01:20 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-01 23:36:37 +02:00
|
|
|
|
2021-10-14 18:06:22 +02:00
|
|
|
func (s selectorHistogram) AggregatorFor(descriptor *sdkapi.Descriptor, aggPtrs ...*export.Aggregator) {
|
2020-10-13 16:55:31 +02:00
|
|
|
switch descriptor.InstrumentKind() {
|
2021-09-01 22:38:37 +02:00
|
|
|
case sdkapi.GaugeObserverInstrumentKind:
|
2020-09-25 00:30:55 +02:00
|
|
|
lastValueAggs(aggPtrs)
|
2021-09-01 22:38:37 +02:00
|
|
|
case sdkapi.HistogramInstrumentKind:
|
2021-01-16 01:29:02 +02:00
|
|
|
aggs := histogram.New(len(aggPtrs), descriptor, s.options...)
|
2020-06-13 09:55:01 +02:00
|
|
|
for i := range aggPtrs {
|
|
|
|
*aggPtrs[i] = &aggs[i]
|
|
|
|
}
|
2020-04-01 23:36:37 +02:00
|
|
|
default:
|
2020-06-13 09:55:01 +02:00
|
|
|
sumAggs(aggPtrs)
|
2020-04-01 23:36:37 +02:00
|
|
|
}
|
|
|
|
}
|