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 (
|
2020-03-19 21:02:46 +02:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
2019-11-15 23:01:20 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/array"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/ddsketch"
|
2020-04-01 23:36:37 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
|
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{}
|
|
|
|
selectorSketch struct {
|
|
|
|
config *ddsketch.Config
|
|
|
|
}
|
2020-04-01 23:36:37 +02:00
|
|
|
selectorHistogram struct {
|
2020-05-21 19:29:03 +02:00
|
|
|
boundaries []float64
|
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 = selectorSketch{}
|
|
|
|
_ export.AggregatorSelector = selectorExact{}
|
|
|
|
_ export.AggregatorSelector = selectorHistogram{}
|
2019-11-15 23:01:20 +02:00
|
|
|
)
|
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
// NewWithInexpensiveDistribution returns a simple aggregation selector
|
2020-03-11 01:00:37 +02:00
|
|
|
// that uses counter, minmaxsumcount and minmaxsumcount aggregators
|
|
|
|
// for the three kinds of metric. This selector is faster and uses
|
|
|
|
// less memory than the others because minmaxsumcount does not
|
|
|
|
// aggregate quantile information.
|
2020-06-23 19:51:15 +02:00
|
|
|
func NewWithInexpensiveDistribution() export.AggregatorSelector {
|
2019-11-15 23:01:20 +02:00
|
|
|
return selectorInexpensive{}
|
|
|
|
}
|
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
// NewWithSketchDistribution returns a simple aggregation selector that
|
2020-03-11 01:00:37 +02:00
|
|
|
// uses counter, ddsketch, and ddsketch aggregators for the three
|
|
|
|
// kinds of metric. This selector uses more cpu and memory than the
|
2020-05-16 07:11:12 +02:00
|
|
|
// NewWithInexpensiveDistribution because it uses one DDSketch per distinct
|
|
|
|
// instrument and label set.
|
2020-06-23 19:51:15 +02:00
|
|
|
func NewWithSketchDistribution(config *ddsketch.Config) export.AggregatorSelector {
|
2019-11-15 23:01:20 +02:00
|
|
|
return selectorSketch{
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
// NewWithExactDistribution returns a simple aggregation selector that uses
|
2020-03-11 01:00:37 +02:00
|
|
|
// counter, array, and array aggregators for the three kinds of metric.
|
2020-05-16 07:11:12 +02:00
|
|
|
// This selector uses more memory than the NewWithSketchDistribution
|
2019-11-15 23:01:20 +02:00
|
|
|
// because it aggregates an array of all values, therefore is able to
|
|
|
|
// compute exact quantiles.
|
2020-06-23 19:51:15 +02:00
|
|
|
func NewWithExactDistribution() export.AggregatorSelector {
|
2019-11-15 23:01:20 +02:00
|
|
|
return selectorExact{}
|
|
|
|
}
|
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
// NewWithHistogramDistribution returns a simple aggregation selector that uses counter,
|
2020-04-01 23:36:37 +02:00
|
|
|
// histogram, and histogram aggregators for the three kinds of metric. This
|
2020-05-16 07:11:12 +02:00
|
|
|
// selector uses more memory than the NewWithInexpensiveDistribution because it
|
2020-04-01 23:36:37 +02:00
|
|
|
// uses a counter per bucket.
|
2020-06-23 19:51:15 +02:00
|
|
|
func NewWithHistogramDistribution(boundaries []float64) export.AggregatorSelector {
|
2020-04-01 23:36:37 +02:00
|
|
|
return selectorHistogram{boundaries: boundaries}
|
|
|
|
}
|
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (selectorInexpensive) AggregatorFor(descriptor *metric.Descriptor, aggPtrs ...*export.Aggregator) {
|
2019-11-15 23:01:20 +02:00
|
|
|
switch descriptor.MetricKind() {
|
2020-05-20 19:27:26 +02:00
|
|
|
case metric.ValueObserverKind, metric.ValueRecorderKind:
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
func (s selectorSketch) AggregatorFor(descriptor *metric.Descriptor, aggPtrs ...*export.Aggregator) {
|
2019-11-15 23:01:20 +02:00
|
|
|
switch descriptor.MetricKind() {
|
2020-05-20 19:27:26 +02:00
|
|
|
case metric.ValueObserverKind, metric.ValueRecorderKind:
|
2020-06-13 09:55:01 +02:00
|
|
|
aggs := ddsketch.New(len(aggPtrs), descriptor, s.config)
|
|
|
|
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-06-13 09:55:01 +02:00
|
|
|
func (selectorExact) AggregatorFor(descriptor *metric.Descriptor, aggPtrs ...*export.Aggregator) {
|
2019-11-15 23:01:20 +02:00
|
|
|
switch descriptor.MetricKind() {
|
2020-05-20 19:27:26 +02:00
|
|
|
case metric.ValueObserverKind, metric.ValueRecorderKind:
|
2020-06-13 09:55:01 +02:00
|
|
|
aggs := array.New(len(aggPtrs))
|
|
|
|
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
|
|
|
|
2020-06-13 09:55:01 +02:00
|
|
|
func (s selectorHistogram) AggregatorFor(descriptor *metric.Descriptor, aggPtrs ...*export.Aggregator) {
|
2020-04-01 23:36:37 +02:00
|
|
|
switch descriptor.MetricKind() {
|
2020-05-20 19:27:26 +02:00
|
|
|
case metric.ValueObserverKind, metric.ValueRecorderKind:
|
2020-06-13 09:55:01 +02:00
|
|
|
aggs := histogram.New(len(aggPtrs), descriptor, s.boundaries)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|