2020-03-23 22:41:10 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-11-15 13:01:20 -08: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-11 16:02:28 -07:00
|
|
|
"go.opentelemetry.io/otel/metric/sdkapi"
|
2019-11-15 13:01:20 -08:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
2020-04-01 23:36:37 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
|
2020-09-24 15:30:55 -07:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/lastvalue"
|
2019-11-26 14:07:58 -08:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/minmaxsumcount"
|
2020-03-11 20:21:34 -07:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/sum"
|
2019-11-15 13:01:20 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
selectorInexpensive struct{}
|
2021-01-12 10:19:13 -08:00
|
|
|
selectorHistogram struct {
|
2021-01-15 15:29:02 -08:00
|
|
|
options []histogram.Option
|
2020-04-01 23:36:37 +02:00
|
|
|
}
|
2019-11-15 13:01:20 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-06-23 10:51:15 -07:00
|
|
|
_ export.AggregatorSelector = selectorInexpensive{}
|
|
|
|
_ export.AggregatorSelector = selectorHistogram{}
|
2019-11-15 13:01:20 -08:00
|
|
|
)
|
|
|
|
|
2021-01-12 10:19:13 -08:00
|
|
|
// NewWithInexpensiveDistribution returns a simple aggregator selector
|
2021-09-01 13:38:37 -07:00
|
|
|
// that uses minmaxsumcount aggregators for `Histogram`
|
2021-01-12 10:19:13 -08: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 10:51:15 -07:00
|
|
|
func NewWithInexpensiveDistribution() export.AggregatorSelector {
|
2019-11-15 13:01:20 -08:00
|
|
|
return selectorInexpensive{}
|
|
|
|
}
|
|
|
|
|
2021-01-12 10:19:13 -08:00
|
|
|
// NewWithHistogramDistribution returns a simple aggregator selector
|
2021-09-01 13:38:37 -07:00
|
|
|
// that uses histogram aggregators for `Histogram` instruments.
|
2021-01-12 10:19:13 -08:00
|
|
|
// This selector is a good default choice for most metric exporters.
|
2021-01-15 15:29:02 -08:00
|
|
|
func NewWithHistogramDistribution(options ...histogram.Option) export.AggregatorSelector {
|
|
|
|
return selectorHistogram{options: options}
|
2020-04-01 23:36:37 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 00:55:01 -07:00
|
|
|
func sumAggs(aggPtrs []*export.Aggregator) {
|
|
|
|
aggs := sum.New(len(aggPtrs))
|
|
|
|
for i := range aggPtrs {
|
|
|
|
*aggPtrs[i] = &aggs[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 15:30:55 -07:00
|
|
|
func lastValueAggs(aggPtrs []*export.Aggregator) {
|
|
|
|
aggs := lastvalue.New(len(aggPtrs))
|
|
|
|
for i := range aggPtrs {
|
|
|
|
*aggPtrs[i] = &aggs[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-14 09:06:22 -07:00
|
|
|
func (selectorInexpensive) AggregatorFor(descriptor *sdkapi.Descriptor, aggPtrs ...*export.Aggregator) {
|
2020-10-13 07:55:31 -07:00
|
|
|
switch descriptor.InstrumentKind() {
|
2021-09-01 13:38:37 -07:00
|
|
|
case sdkapi.GaugeObserverInstrumentKind:
|
2020-09-24 15:30:55 -07:00
|
|
|
lastValueAggs(aggPtrs)
|
2021-09-01 13:38:37 -07:00
|
|
|
case sdkapi.HistogramInstrumentKind:
|
2020-06-13 00:55:01 -07:00
|
|
|
aggs := minmaxsumcount.New(len(aggPtrs), descriptor)
|
|
|
|
for i := range aggPtrs {
|
|
|
|
*aggPtrs[i] = &aggs[i]
|
|
|
|
}
|
2019-11-15 13:01:20 -08:00
|
|
|
default:
|
2020-06-13 00:55:01 -07:00
|
|
|
sumAggs(aggPtrs)
|
2019-11-15 13:01:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-14 09:06:22 -07:00
|
|
|
func (s selectorHistogram) AggregatorFor(descriptor *sdkapi.Descriptor, aggPtrs ...*export.Aggregator) {
|
2020-10-13 07:55:31 -07:00
|
|
|
switch descriptor.InstrumentKind() {
|
2021-09-01 13:38:37 -07:00
|
|
|
case sdkapi.GaugeObserverInstrumentKind:
|
2020-09-24 15:30:55 -07:00
|
|
|
lastValueAggs(aggPtrs)
|
2021-09-01 13:38:37 -07:00
|
|
|
case sdkapi.HistogramInstrumentKind:
|
2021-01-15 15:29:02 -08:00
|
|
|
aggs := histogram.New(len(aggPtrs), descriptor, s.options...)
|
2020-06-13 00:55:01 -07:00
|
|
|
for i := range aggPtrs {
|
|
|
|
*aggPtrs[i] = &aggs[i]
|
|
|
|
}
|
2020-04-01 23:36:37 +02:00
|
|
|
default:
|
2020-06-13 00:55:01 -07:00
|
|
|
sumAggs(aggPtrs)
|
2020-04-01 23:36:37 +02:00
|
|
|
}
|
|
|
|
}
|