2020-03-13 20:42:20 +02:00
|
|
|
// Copyright 2020, 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.
|
|
|
|
|
|
|
|
// Package transform provides translations for opentelemetry-go concepts and
|
|
|
|
// structures to otlp structures.
|
|
|
|
package transform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
commonpb "github.com/open-telemetry/opentelemetry-proto/gen/go/common/v1"
|
|
|
|
metricpb "github.com/open-telemetry/opentelemetry-proto/gen/go/metrics/v1"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/api/core"
|
2020-03-19 21:02:46 +02:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
2020-03-13 20:42:20 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/export/metric/aggregator"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ErrUnimplementedAgg is returned when a transformation of an unimplemented
|
|
|
|
// aggregator is attempted.
|
|
|
|
var ErrUnimplementedAgg = errors.New("unimplemented aggregator")
|
|
|
|
|
|
|
|
// Record transforms a Record into an OTLP Metric. An ErrUnimplementedAgg
|
|
|
|
// error is returned if the Record Aggregator is not supported.
|
2020-03-19 21:02:46 +02:00
|
|
|
func Record(r export.Record) (*metricpb.Metric, error) {
|
2020-03-13 20:42:20 +02:00
|
|
|
d := r.Descriptor()
|
|
|
|
l := r.Labels()
|
|
|
|
switch a := r.Aggregator().(type) {
|
|
|
|
case aggregator.MinMaxSumCount:
|
|
|
|
return minMaxSumCount(d, l, a)
|
|
|
|
case aggregator.Sum:
|
|
|
|
return sum(d, l, a)
|
|
|
|
}
|
|
|
|
return nil, ErrUnimplementedAgg
|
|
|
|
}
|
|
|
|
|
|
|
|
// sum transforms a Sum Aggregator into an OTLP Metric.
|
2020-03-19 21:02:46 +02:00
|
|
|
func sum(desc *metric.Descriptor, labels export.Labels, a aggregator.Sum) (*metricpb.Metric, error) {
|
2020-03-13 20:42:20 +02:00
|
|
|
sum, err := a.Sum()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &metricpb.Metric{
|
|
|
|
MetricDescriptor: &metricpb.MetricDescriptor{
|
|
|
|
Name: desc.Name(),
|
|
|
|
Description: desc.Description(),
|
|
|
|
Unit: string(desc.Unit()),
|
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-20 00:01:34 +02:00
|
|
|
Labels: stringKeyValues(labels.Iter()),
|
2020-03-13 20:42:20 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
switch n := desc.NumberKind(); n {
|
|
|
|
case core.Int64NumberKind, core.Uint64NumberKind:
|
|
|
|
m.MetricDescriptor.Type = metricpb.MetricDescriptor_COUNTER_INT64
|
2020-03-17 01:43:54 +02:00
|
|
|
m.Int64DataPoints = []*metricpb.Int64DataPoint{
|
2020-03-13 20:42:20 +02:00
|
|
|
{Value: sum.CoerceToInt64(n)},
|
|
|
|
}
|
|
|
|
case core.Float64NumberKind:
|
|
|
|
m.MetricDescriptor.Type = metricpb.MetricDescriptor_COUNTER_DOUBLE
|
2020-03-17 01:43:54 +02:00
|
|
|
m.DoubleDataPoints = []*metricpb.DoubleDataPoint{
|
2020-03-13 20:42:20 +02:00
|
|
|
{Value: sum.CoerceToFloat64(n)},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// minMaxSumCountValue returns the values of the MinMaxSumCount Aggregator
|
|
|
|
// as discret values.
|
|
|
|
func minMaxSumCountValues(a aggregator.MinMaxSumCount) (min, max, sum core.Number, count int64, err error) {
|
|
|
|
if min, err = a.Min(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if max, err = a.Max(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if sum, err = a.Sum(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if count, err = a.Count(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// minMaxSumCount transforms a MinMaxSumCount Aggregator into an OTLP Metric.
|
2020-03-19 21:02:46 +02:00
|
|
|
func minMaxSumCount(desc *metric.Descriptor, labels export.Labels, a aggregator.MinMaxSumCount) (*metricpb.Metric, error) {
|
2020-03-13 20:42:20 +02:00
|
|
|
min, max, sum, count, err := minMaxSumCountValues(a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
numKind := desc.NumberKind()
|
|
|
|
return &metricpb.Metric{
|
|
|
|
MetricDescriptor: &metricpb.MetricDescriptor{
|
|
|
|
Name: desc.Name(),
|
|
|
|
Description: desc.Description(),
|
|
|
|
Unit: string(desc.Unit()),
|
|
|
|
Type: metricpb.MetricDescriptor_SUMMARY,
|
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-20 00:01:34 +02:00
|
|
|
Labels: stringKeyValues(labels.Iter()),
|
2020-03-13 20:42:20 +02:00
|
|
|
},
|
2020-03-17 01:43:54 +02:00
|
|
|
SummaryDataPoints: []*metricpb.SummaryDataPoint{
|
2020-03-13 20:42:20 +02:00
|
|
|
{
|
|
|
|
Count: uint64(count),
|
|
|
|
Sum: sum.CoerceToFloat64(numKind),
|
|
|
|
PercentileValues: []*metricpb.SummaryDataPoint_ValueAtPercentile{
|
|
|
|
{
|
|
|
|
Percentile: 0.0,
|
|
|
|
Value: min.CoerceToFloat64(numKind),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Percentile: 100.0,
|
|
|
|
Value: max.CoerceToFloat64(numKind),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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-20 00:01:34 +02:00
|
|
|
// stringKeyValues transforms a label iterator into an OTLP StringKeyValues.
|
|
|
|
func stringKeyValues(iter export.LabelIterator) []*commonpb.StringKeyValue {
|
|
|
|
l := iter.Len()
|
|
|
|
if l == 0 {
|
|
|
|
// TODO: That looks like a pointless allocation in case of
|
|
|
|
// no labels, but returning nil from this function makes
|
|
|
|
// the test fail.
|
|
|
|
return []*commonpb.StringKeyValue{}
|
|
|
|
}
|
|
|
|
result := make([]*commonpb.StringKeyValue, 0, l)
|
|
|
|
for iter.Next() {
|
|
|
|
kv := iter.Label()
|
2020-03-13 20:42:20 +02:00
|
|
|
result = append(result, &commonpb.StringKeyValue{
|
|
|
|
Key: string(kv.Key),
|
|
|
|
Value: kv.Value.Emit(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|