2020-03-23 22:41:10 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-07-24 20:32:52 -07:00
|
|
|
package metrictest // import "go.opentelemetry.io/otel/sdk/export/metric/metrictest"
|
2019-11-15 13:01:20 -08:00
|
|
|
|
|
|
|
import (
|
2019-11-26 15:47:15 -04:00
|
|
|
"context"
|
2020-03-16 16:28:33 -07:00
|
|
|
"errors"
|
2020-06-13 00:55:01 -07:00
|
|
|
"reflect"
|
2020-05-18 18:37:41 -07:00
|
|
|
"sync"
|
2020-06-18 10:16:33 -07:00
|
|
|
"time"
|
2019-11-26 15:47:15 -04:00
|
|
|
|
2020-05-14 07:06:03 +08:00
|
|
|
"go.opentelemetry.io/otel/api/kv"
|
2020-04-23 12:10:58 -07:00
|
|
|
"go.opentelemetry.io/otel/api/label"
|
2020-03-19 12:02:46 -07:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
2019-11-15 13:01:20 -08:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
2020-06-09 22:53:30 -07:00
|
|
|
"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
|
2020-05-18 17:44:28 -07:00
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
2019-11-15 13:01:20 -08:00
|
|
|
)
|
|
|
|
|
2020-04-24 09:44:21 -07:00
|
|
|
type mapkey struct {
|
|
|
|
desc *metric.Descriptor
|
|
|
|
distinct label.Distinct
|
|
|
|
}
|
|
|
|
|
2020-06-13 00:55:01 -07:00
|
|
|
// CheckpointSet is useful for testing Exporters.
|
2020-08-13 13:12:32 -07:00
|
|
|
// TODO(#872): Uses of this can be replaced by processortest.Output.
|
2019-11-15 13:01:20 -08:00
|
|
|
type CheckpointSet struct {
|
2020-05-18 18:37:41 -07:00
|
|
|
sync.RWMutex
|
2020-05-18 17:44:28 -07:00
|
|
|
records map[mapkey]export.Record
|
|
|
|
updates []export.Record
|
2020-05-18 18:37:41 -07:00
|
|
|
resource *resource.Resource
|
2019-11-15 13:01:20 -08:00
|
|
|
}
|
|
|
|
|
2020-06-13 00:55:01 -07:00
|
|
|
// NoopAggregator is useful for testing Exporters.
|
|
|
|
type NoopAggregator struct{}
|
|
|
|
|
|
|
|
var _ export.Aggregator = (*NoopAggregator)(nil)
|
|
|
|
|
|
|
|
// Update implements export.Aggregator.
|
2020-06-18 10:16:33 -07:00
|
|
|
func (NoopAggregator) Update(context.Context, metric.Number, *metric.Descriptor) error {
|
2020-06-13 00:55:01 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-23 10:41:11 -07:00
|
|
|
// SynchronizedMove implements export.Aggregator.
|
|
|
|
func (NoopAggregator) SynchronizedMove(export.Aggregator, *metric.Descriptor) error {
|
2020-06-13 00:55:01 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge implements export.Aggregator.
|
2020-06-18 10:16:33 -07:00
|
|
|
func (NoopAggregator) Merge(export.Aggregator, *metric.Descriptor) error {
|
2020-06-13 00:55:01 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-18 10:16:33 -07:00
|
|
|
// Aggregation returns an interface for reading the state of this aggregator.
|
|
|
|
func (NoopAggregator) Aggregation() aggregation.Aggregation {
|
|
|
|
return NoopAggregator{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kind implements aggregation.Aggregation.
|
|
|
|
func (NoopAggregator) Kind() aggregation.Kind {
|
|
|
|
return aggregation.Kind("Noop")
|
|
|
|
}
|
|
|
|
|
2019-12-23 14:47:51 -03:00
|
|
|
// NewCheckpointSet returns a test CheckpointSet that new records could be added.
|
2020-03-27 14:06:48 -07:00
|
|
|
// Records are grouped by their encoded labels.
|
2020-05-18 17:44:28 -07:00
|
|
|
func NewCheckpointSet(resource *resource.Resource) *CheckpointSet {
|
2019-11-15 13:01:20 -08:00
|
|
|
return &CheckpointSet{
|
2020-05-18 17:44:28 -07:00
|
|
|
records: make(map[mapkey]export.Record),
|
|
|
|
resource: resource,
|
2019-11-15 13:01:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 00:55:01 -07:00
|
|
|
// Reset clears the Aggregator state.
|
2019-11-15 13:01:20 -08:00
|
|
|
func (p *CheckpointSet) Reset() {
|
2020-04-24 09:44:21 -07:00
|
|
|
p.records = make(map[mapkey]export.Record)
|
2019-11-15 13:01:20 -08:00
|
|
|
p.updates = nil
|
|
|
|
}
|
|
|
|
|
2020-06-13 00:55:01 -07:00
|
|
|
// Add a new record to a CheckpointSet.
|
2019-12-23 14:47:51 -03:00
|
|
|
//
|
2020-03-27 14:06:48 -07:00
|
|
|
// If there is an existing record with the same descriptor and labels,
|
2019-12-23 14:47:51 -03:00
|
|
|
// the stored aggregator will be returned and should be merged.
|
2020-05-14 07:06:03 +08:00
|
|
|
func (p *CheckpointSet) Add(desc *metric.Descriptor, newAgg export.Aggregator, labels ...kv.KeyValue) (agg export.Aggregator, added bool) {
|
2020-04-23 12:10:58 -07:00
|
|
|
elabels := label.NewSet(labels...)
|
2019-11-15 13:01:20 -08:00
|
|
|
|
2020-04-24 09:44:21 -07:00
|
|
|
key := mapkey{
|
|
|
|
desc: desc,
|
|
|
|
distinct: elabels.Equivalent(),
|
|
|
|
}
|
2019-12-23 14:47:51 -03:00
|
|
|
if record, ok := p.records[key]; ok {
|
2020-06-18 10:16:33 -07:00
|
|
|
return record.Aggregation().(export.Aggregator), false
|
2019-12-23 14:47:51 -03:00
|
|
|
}
|
|
|
|
|
2020-06-18 10:16:33 -07:00
|
|
|
rec := export.NewRecord(desc, &elabels, p.resource, newAgg.Aggregation(), time.Time{}, time.Time{})
|
2019-12-23 14:47:51 -03:00
|
|
|
p.updates = append(p.updates, rec)
|
|
|
|
p.records[key] = rec
|
|
|
|
return newAgg, true
|
2019-11-15 13:01:20 -08:00
|
|
|
}
|
|
|
|
|
2020-06-23 12:00:15 -07:00
|
|
|
// ForEach does not use ExportKindSelected: use a real Processor to
|
2020-06-22 22:59:51 -07:00
|
|
|
// test ExportKind functionality.
|
|
|
|
func (p *CheckpointSet) ForEach(_ export.ExportKindSelector, f func(export.Record) error) error {
|
2019-11-15 13:01:20 -08:00
|
|
|
for _, r := range p.updates {
|
2020-06-09 22:53:30 -07:00
|
|
|
if err := f(r); err != nil && !errors.Is(err, aggregation.ErrNoData) {
|
2020-03-16 16:28:33 -07:00
|
|
|
return err
|
|
|
|
}
|
2019-11-15 13:01:20 -08:00
|
|
|
}
|
2020-03-16 16:28:33 -07:00
|
|
|
return nil
|
2019-11-15 13:01:20 -08:00
|
|
|
}
|
2020-06-13 00:55:01 -07:00
|
|
|
|
|
|
|
// Takes a slice of []some.Aggregator and returns a slice of []export.Aggregator
|
|
|
|
func Unslice2(sl interface{}) (one, two export.Aggregator) {
|
|
|
|
slv := reflect.ValueOf(sl)
|
|
|
|
if slv.Type().Kind() != reflect.Slice {
|
|
|
|
panic("Invalid Unslice2")
|
|
|
|
}
|
|
|
|
if slv.Len() != 2 {
|
|
|
|
panic("Invalid Unslice2: length > 2")
|
|
|
|
}
|
|
|
|
one = slv.Index(0).Addr().Interface().(export.Aggregator)
|
|
|
|
two = slv.Index(1).Addr().Interface().(export.Aggregator)
|
|
|
|
return
|
|
|
|
}
|