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 test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
|
|
"go.opentelemetry.io/otel/api/key"
|
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-03-19 12:02:46 -07:00
|
|
|
"go.opentelemetry.io/otel/sdk/export/metric/aggregator"
|
2019-11-15 13:01:20 -08:00
|
|
|
sdk "go.opentelemetry.io/otel/sdk/metric"
|
2020-03-10 16:00:37 -07:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/lastvalue"
|
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 (
|
|
|
|
// Encoder is an alternate label encoder to validate grouping logic.
|
|
|
|
Encoder struct{}
|
|
|
|
|
|
|
|
// Output collects distinct metric/label set outputs.
|
2020-03-19 12:02:46 -07:00
|
|
|
Output map[string]float64
|
2019-11-15 13:01:20 -08:00
|
|
|
|
|
|
|
// testAggregationSelector returns aggregators consistent with
|
|
|
|
// the test variables below, needed for testing stateful
|
|
|
|
// batchers, which clone Aggregators using AggregatorFor(desc).
|
|
|
|
testAggregationSelector struct{}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-03-10 16:00:37 -07:00
|
|
|
// LastValueADesc and LastValueBDesc group by "G"
|
2020-03-19 12:02:46 -07:00
|
|
|
LastValueADesc = metric.NewDescriptor(
|
|
|
|
"lastvalue.a", metric.ObserverKind, core.Int64NumberKind, metric.WithKeys(key.New("G")))
|
|
|
|
LastValueBDesc = metric.NewDescriptor(
|
|
|
|
"lastvalue.b", metric.ObserverKind, core.Int64NumberKind, metric.WithKeys(key.New("G")))
|
2019-11-19 13:25:24 -08:00
|
|
|
// CounterADesc and CounterBDesc group by "C"
|
2020-03-19 12:02:46 -07:00
|
|
|
CounterADesc = metric.NewDescriptor(
|
|
|
|
"sum.a", metric.CounterKind, core.Int64NumberKind, metric.WithKeys(key.New("C")))
|
|
|
|
CounterBDesc = metric.NewDescriptor(
|
|
|
|
"sum.b", metric.CounterKind, core.Int64NumberKind, metric.WithKeys(key.New("C")))
|
2019-11-15 13:01:20 -08:00
|
|
|
|
|
|
|
// SdkEncoder uses a non-standard encoder like K1~V1&K2~V2
|
|
|
|
SdkEncoder = &Encoder{}
|
|
|
|
// GroupEncoder uses the SDK default encoder
|
2019-11-21 20:46:05 -08:00
|
|
|
GroupEncoder = sdk.NewDefaultLabelEncoder()
|
2019-11-15 13:01:20 -08:00
|
|
|
|
2020-03-10 16:00:37 -07:00
|
|
|
// LastValue groups are (labels1), (labels2+labels3)
|
2019-11-15 13:01:20 -08:00
|
|
|
// Counter groups are (labels1+labels2), (labels3)
|
|
|
|
|
|
|
|
// Labels1 has G=H and C=D
|
|
|
|
Labels1 = makeLabels(SdkEncoder, key.String("G", "H"), key.String("C", "D"))
|
|
|
|
// Labels2 has C=D and E=F
|
|
|
|
Labels2 = makeLabels(SdkEncoder, key.String("C", "D"), key.String("E", "F"))
|
|
|
|
// Labels3 is the empty set
|
|
|
|
Labels3 = makeLabels(SdkEncoder)
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewAggregationSelector returns a policy that is consistent with the
|
2020-03-11 20:21:34 -07:00
|
|
|
// test descriptors above. I.e., it returns sum.New() for counter
|
2020-03-10 16:00:37 -07:00
|
|
|
// instruments and lastvalue.New for lastValue instruments.
|
2019-11-15 13:01:20 -08:00
|
|
|
func NewAggregationSelector() export.AggregationSelector {
|
|
|
|
return &testAggregationSelector{}
|
|
|
|
}
|
|
|
|
|
2020-03-19 12:02:46 -07:00
|
|
|
func (*testAggregationSelector) AggregatorFor(desc *metric.Descriptor) export.Aggregator {
|
2019-11-15 13:01:20 -08:00
|
|
|
switch desc.MetricKind() {
|
2020-03-19 12:02:46 -07:00
|
|
|
case metric.CounterKind:
|
2020-03-11 20:21:34 -07:00
|
|
|
return sum.New()
|
2020-03-19 12:02:46 -07:00
|
|
|
case metric.ObserverKind:
|
2020-03-10 16:00:37 -07:00
|
|
|
return lastvalue.New()
|
2019-11-15 13:01:20 -08:00
|
|
|
default:
|
|
|
|
panic("Invalid descriptor MetricKind for this test")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeLabels(encoder export.LabelEncoder, labels ...core.KeyValue) export.Labels {
|
Replace `Ordered` with an iterator in `export.Labels`. (#567)
* Do not expose a slice of labels in export.Record
This is really an inconvenient implementation detail leak - we may
want to store labels in a different way. Replace it with an iterator -
it does not force us to use slice of key values as a storage in the
long run.
* Add Len to LabelIterator
It may come in handy in several situations, where we don't have access
to export.Labels object, but only to the label iterator.
* Use reflect value label iterator for the fixed labels
* add reset operation to iterator
Makes my life easier when writing a benchmark. Might also be an
alternative to cloning the iterator.
* Add benchmarks for iterators
* Add import comment
* Add clone operation to label iterator
* Move iterator tests to a separate package
* Add tests for cloning iterators
* Pass label iterator to export labels
* Use non-addressable array reflect values
By not using the value created by `reflect.New()`, but rather by
`reflect.ValueOf()`, we get a non-addressable array in the value,
which does not infer an allocation cost when getting an element from
the array.
* Drop zero iterator
This can be substituted by a reflect value iterator that goes over a
value with a zero-sized array.
* Add a simple iterator that implements label iterator
In the long run this will completely replace the LabelIterator
interface.
* Replace reflect value iterator with simple iterator
* Pass label storage to new export labels, not label iterator
* Drop label iterator interface, rename storage iterator to label iterator
* Drop clone operation from iterator
It's a leftover from interface times and now it's pointless - the
iterator is a simple struct, so cloning it is a simple copy.
* Drop Reset from label iterator
The sole existence of Reset was actually for benchmarking convenience.
Now we can just copy the iterator cheaply, so a need for Reset is no
more.
* Drop noop iterator tests
* Move back iterator tests to export package
* Eagerly get the reflect value of ordered labels
So we won't get into problems when several goroutines want to iterate
the same labels at the same time. Not sure if this would be a big
deal, since every goroutine would compute the same reflect.Value, but
concurrent write to the same memory is bad anyway. And it doesn't cost
us any extra allocations anyway.
* Replace NewSliceLabelIterator() with a method of LabelSlice
* Add some documentation
* Documentation fixes
2020-03-19 23:01:34 +01:00
|
|
|
ls := export.LabelSlice(labels)
|
|
|
|
return export.NewLabels(ls, encoder.Encode(ls.Iter()), encoder)
|
2019-11-15 13:01:20 -08:00
|
|
|
}
|
|
|
|
|
Replace `Ordered` with an iterator in `export.Labels`. (#567)
* Do not expose a slice of labels in export.Record
This is really an inconvenient implementation detail leak - we may
want to store labels in a different way. Replace it with an iterator -
it does not force us to use slice of key values as a storage in the
long run.
* Add Len to LabelIterator
It may come in handy in several situations, where we don't have access
to export.Labels object, but only to the label iterator.
* Use reflect value label iterator for the fixed labels
* add reset operation to iterator
Makes my life easier when writing a benchmark. Might also be an
alternative to cloning the iterator.
* Add benchmarks for iterators
* Add import comment
* Add clone operation to label iterator
* Move iterator tests to a separate package
* Add tests for cloning iterators
* Pass label iterator to export labels
* Use non-addressable array reflect values
By not using the value created by `reflect.New()`, but rather by
`reflect.ValueOf()`, we get a non-addressable array in the value,
which does not infer an allocation cost when getting an element from
the array.
* Drop zero iterator
This can be substituted by a reflect value iterator that goes over a
value with a zero-sized array.
* Add a simple iterator that implements label iterator
In the long run this will completely replace the LabelIterator
interface.
* Replace reflect value iterator with simple iterator
* Pass label storage to new export labels, not label iterator
* Drop label iterator interface, rename storage iterator to label iterator
* Drop clone operation from iterator
It's a leftover from interface times and now it's pointless - the
iterator is a simple struct, so cloning it is a simple copy.
* Drop Reset from label iterator
The sole existence of Reset was actually for benchmarking convenience.
Now we can just copy the iterator cheaply, so a need for Reset is no
more.
* Drop noop iterator tests
* Move back iterator tests to export package
* Eagerly get the reflect value of ordered labels
So we won't get into problems when several goroutines want to iterate
the same labels at the same time. Not sure if this would be a big
deal, since every goroutine would compute the same reflect.Value, but
concurrent write to the same memory is bad anyway. And it doesn't cost
us any extra allocations anyway.
* Replace NewSliceLabelIterator() with a method of LabelSlice
* Add some documentation
* Documentation fixes
2020-03-19 23:01:34 +01:00
|
|
|
func (Encoder) Encode(iter export.LabelIterator) string {
|
2019-11-15 13:01:20 -08:00
|
|
|
var sb strings.Builder
|
Replace `Ordered` with an iterator in `export.Labels`. (#567)
* Do not expose a slice of labels in export.Record
This is really an inconvenient implementation detail leak - we may
want to store labels in a different way. Replace it with an iterator -
it does not force us to use slice of key values as a storage in the
long run.
* Add Len to LabelIterator
It may come in handy in several situations, where we don't have access
to export.Labels object, but only to the label iterator.
* Use reflect value label iterator for the fixed labels
* add reset operation to iterator
Makes my life easier when writing a benchmark. Might also be an
alternative to cloning the iterator.
* Add benchmarks for iterators
* Add import comment
* Add clone operation to label iterator
* Move iterator tests to a separate package
* Add tests for cloning iterators
* Pass label iterator to export labels
* Use non-addressable array reflect values
By not using the value created by `reflect.New()`, but rather by
`reflect.ValueOf()`, we get a non-addressable array in the value,
which does not infer an allocation cost when getting an element from
the array.
* Drop zero iterator
This can be substituted by a reflect value iterator that goes over a
value with a zero-sized array.
* Add a simple iterator that implements label iterator
In the long run this will completely replace the LabelIterator
interface.
* Replace reflect value iterator with simple iterator
* Pass label storage to new export labels, not label iterator
* Drop label iterator interface, rename storage iterator to label iterator
* Drop clone operation from iterator
It's a leftover from interface times and now it's pointless - the
iterator is a simple struct, so cloning it is a simple copy.
* Drop Reset from label iterator
The sole existence of Reset was actually for benchmarking convenience.
Now we can just copy the iterator cheaply, so a need for Reset is no
more.
* Drop noop iterator tests
* Move back iterator tests to export package
* Eagerly get the reflect value of ordered labels
So we won't get into problems when several goroutines want to iterate
the same labels at the same time. Not sure if this would be a big
deal, since every goroutine would compute the same reflect.Value, but
concurrent write to the same memory is bad anyway. And it doesn't cost
us any extra allocations anyway.
* Replace NewSliceLabelIterator() with a method of LabelSlice
* Add some documentation
* Documentation fixes
2020-03-19 23:01:34 +01:00
|
|
|
for iter.Next() {
|
|
|
|
i, l := iter.IndexedLabel()
|
2019-11-15 13:01:20 -08:00
|
|
|
if i > 0 {
|
|
|
|
sb.WriteString("&")
|
|
|
|
}
|
|
|
|
sb.WriteString(string(l.Key))
|
|
|
|
sb.WriteString("~")
|
|
|
|
sb.WriteString(l.Value.Emit())
|
|
|
|
}
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
2020-03-10 16:00:37 -07:00
|
|
|
// LastValueAgg returns a checkpointed lastValue aggregator w/ the specified descriptor and value.
|
2020-03-19 12:02:46 -07:00
|
|
|
func LastValueAgg(desc *metric.Descriptor, v int64) export.Aggregator {
|
2019-11-15 13:01:20 -08:00
|
|
|
ctx := context.Background()
|
2020-03-10 16:00:37 -07:00
|
|
|
gagg := lastvalue.New()
|
2019-11-19 13:25:24 -08:00
|
|
|
_ = gagg.Update(ctx, core.NewInt64Number(v), desc)
|
|
|
|
gagg.Checkpoint(ctx, desc)
|
2019-11-15 13:01:20 -08:00
|
|
|
return gagg
|
|
|
|
}
|
|
|
|
|
2020-03-10 16:00:37 -07:00
|
|
|
// Convenience method for building a test exported lastValue record.
|
2020-03-19 12:02:46 -07:00
|
|
|
func NewLastValueRecord(desc *metric.Descriptor, labels export.Labels, value int64) export.Record {
|
2020-03-10 16:00:37 -07:00
|
|
|
return export.NewRecord(desc, labels, LastValueAgg(desc, value))
|
2019-11-19 13:25:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience method for building a test exported counter record.
|
2020-03-19 12:02:46 -07:00
|
|
|
func NewCounterRecord(desc *metric.Descriptor, labels export.Labels, value int64) export.Record {
|
2019-11-19 13:25:24 -08:00
|
|
|
return export.NewRecord(desc, labels, CounterAgg(desc, value))
|
|
|
|
}
|
|
|
|
|
|
|
|
// CounterAgg returns a checkpointed counter aggregator w/ the specified descriptor and value.
|
2020-03-19 12:02:46 -07:00
|
|
|
func CounterAgg(desc *metric.Descriptor, v int64) export.Aggregator {
|
2019-11-15 13:01:20 -08:00
|
|
|
ctx := context.Background()
|
2020-03-11 20:21:34 -07:00
|
|
|
cagg := sum.New()
|
2019-11-19 13:25:24 -08:00
|
|
|
_ = cagg.Update(ctx, core.NewInt64Number(v), desc)
|
|
|
|
cagg.Checkpoint(ctx, desc)
|
2019-11-15 13:01:20 -08:00
|
|
|
return cagg
|
|
|
|
}
|
|
|
|
|
2020-03-10 16:00:37 -07:00
|
|
|
// AddTo adds a name/label-encoding entry with the lastValue or counter
|
2019-11-15 13:01:20 -08:00
|
|
|
// value to the output map.
|
2020-03-16 16:28:33 -07:00
|
|
|
func (o Output) AddTo(rec export.Record) error {
|
2019-11-15 13:01:20 -08:00
|
|
|
labels := rec.Labels()
|
|
|
|
key := fmt.Sprint(rec.Descriptor().Name(), "/", labels.Encoded())
|
2020-03-19 12:02:46 -07:00
|
|
|
var value float64
|
|
|
|
|
|
|
|
if s, ok := rec.Aggregator().(aggregator.Sum); ok {
|
|
|
|
sum, _ := s.Sum()
|
|
|
|
value = sum.CoerceToFloat64(rec.Descriptor().NumberKind())
|
|
|
|
} else if l, ok := rec.Aggregator().(aggregator.LastValue); ok {
|
|
|
|
last, _, _ := l.LastValue()
|
|
|
|
value = last.CoerceToFloat64(rec.Descriptor().NumberKind())
|
|
|
|
} else {
|
|
|
|
panic(fmt.Sprintf("Unhandled aggregator type: %T", rec.Aggregator()))
|
2019-11-15 13:01:20 -08:00
|
|
|
}
|
|
|
|
o[key] = value
|
2020-03-16 16:28:33 -07:00
|
|
|
return nil
|
2019-11-15 13:01:20 -08:00
|
|
|
}
|