2021-03-11 19:49:20 +02: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.
|
|
|
|
|
2021-09-28 04:37:26 +02:00
|
|
|
package opencensus // import "go.opentelemetry.io/otel/bridge/opencensus"
|
2021-03-11 19:49:20 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.opencensus.io/metric/metricdata"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/metric/number"
|
|
|
|
"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errIncompatibleType = errors.New("incompatible type for aggregation")
|
|
|
|
errEmpty = errors.New("points may not be empty")
|
|
|
|
errBadPoint = errors.New("point cannot be converted")
|
|
|
|
)
|
|
|
|
|
2021-11-15 20:05:14 +02:00
|
|
|
type recordFunc func(agg aggregation.Aggregation, end time.Time) error
|
2021-03-11 19:49:20 +02:00
|
|
|
|
2021-11-15 20:05:14 +02:00
|
|
|
// recordAggregationsFromPoints records one OpenTelemetry aggregation for
|
|
|
|
// each OpenCensus point. Points may not be empty and must be either
|
2021-03-11 19:49:20 +02:00
|
|
|
// all (int|float)64 or all *metricdata.Distribution.
|
2021-11-15 20:05:14 +02:00
|
|
|
func recordAggregationsFromPoints(points []metricdata.Point, recorder recordFunc) error {
|
2021-03-11 19:49:20 +02:00
|
|
|
if len(points) == 0 {
|
2021-11-15 20:05:14 +02:00
|
|
|
return errEmpty
|
2021-03-11 19:49:20 +02:00
|
|
|
}
|
|
|
|
switch t := points[0].Value.(type) {
|
|
|
|
case int64:
|
2021-11-15 20:05:14 +02:00
|
|
|
return recordGaugePoints(points, recorder)
|
2021-03-11 19:49:20 +02:00
|
|
|
case float64:
|
2021-11-15 20:05:14 +02:00
|
|
|
return recordGaugePoints(points, recorder)
|
2021-03-11 19:49:20 +02:00
|
|
|
case *metricdata.Distribution:
|
2021-11-15 20:05:14 +02:00
|
|
|
return recordDistributionPoint(points, recorder)
|
2021-03-11 19:49:20 +02:00
|
|
|
default:
|
|
|
|
// TODO add *metricdata.Summary support
|
2021-11-15 20:05:14 +02:00
|
|
|
return fmt.Errorf("%w: %v", errIncompatibleType, t)
|
2021-03-11 19:49:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 20:05:14 +02:00
|
|
|
var _ aggregation.Aggregation = &ocRawAggregator{}
|
|
|
|
var _ aggregation.LastValue = &ocRawAggregator{}
|
2021-03-11 19:49:20 +02:00
|
|
|
|
2021-11-15 20:05:14 +02:00
|
|
|
// recordGaugePoints creates an OpenTelemetry aggregation from OpenCensus points.
|
2021-03-11 19:49:20 +02:00
|
|
|
// Points may not be empty, and must only contain integers or floats.
|
2021-11-15 20:05:14 +02:00
|
|
|
func recordGaugePoints(pts []metricdata.Point, recorder recordFunc) error {
|
|
|
|
for _, pt := range pts {
|
2021-03-11 19:49:20 +02:00
|
|
|
switch t := pt.Value.(type) {
|
|
|
|
case int64:
|
2021-11-15 20:05:14 +02:00
|
|
|
if err := recorder(&ocRawAggregator{
|
|
|
|
value: number.NewInt64Number(pt.Value.(int64)),
|
|
|
|
time: pt.Time,
|
|
|
|
}, pt.Time); err != nil {
|
|
|
|
return err
|
2021-03-11 19:49:20 +02:00
|
|
|
}
|
|
|
|
case float64:
|
2021-11-15 20:05:14 +02:00
|
|
|
if err := recorder(&ocRawAggregator{
|
|
|
|
value: number.NewFloat64Number(pt.Value.(float64)),
|
|
|
|
time: pt.Time,
|
|
|
|
}, pt.Time); err != nil {
|
|
|
|
return err
|
2021-03-11 19:49:20 +02:00
|
|
|
}
|
|
|
|
default:
|
2021-11-15 20:05:14 +02:00
|
|
|
return fmt.Errorf("%w: %v", errIncompatibleType, t)
|
2021-03-11 19:49:20 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-15 20:05:14 +02:00
|
|
|
return nil
|
2021-03-11 19:49:20 +02:00
|
|
|
}
|
|
|
|
|
2021-11-15 20:05:14 +02:00
|
|
|
type ocRawAggregator struct {
|
|
|
|
value number.Number
|
|
|
|
time time.Time
|
2021-03-11 19:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Kind returns the kind of aggregation this is.
|
2021-11-15 20:05:14 +02:00
|
|
|
func (o *ocRawAggregator) Kind() aggregation.Kind {
|
|
|
|
return aggregation.LastValueKind
|
2021-03-11 19:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LastValue returns the last point.
|
2021-11-15 20:05:14 +02:00
|
|
|
func (o *ocRawAggregator) LastValue() (number.Number, time.Time, error) {
|
|
|
|
return o.value, o.time, nil
|
2021-03-11 19:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ aggregation.Aggregation = &ocDistAggregator{}
|
|
|
|
var _ aggregation.Histogram = &ocDistAggregator{}
|
|
|
|
|
2021-11-15 20:05:14 +02:00
|
|
|
// recordDistributionPoint creates an OpenTelemetry aggregation from
|
2021-03-11 19:49:20 +02:00
|
|
|
// OpenCensus points. Points may not be empty, and must only contain
|
|
|
|
// Distributions. The most recent disribution will be used in the aggregation.
|
2021-11-15 20:05:14 +02:00
|
|
|
func recordDistributionPoint(pts []metricdata.Point, recorder recordFunc) error {
|
2021-03-11 19:49:20 +02:00
|
|
|
// only use the most recent datapoint for now.
|
|
|
|
pt := pts[len(pts)-1]
|
|
|
|
val, ok := pt.Value.(*metricdata.Distribution)
|
|
|
|
if !ok {
|
2021-11-15 20:05:14 +02:00
|
|
|
return fmt.Errorf("%w: %v", errBadPoint, pt.Value)
|
2021-03-11 19:49:20 +02:00
|
|
|
}
|
|
|
|
bucketCounts := make([]uint64, len(val.Buckets))
|
|
|
|
for i, bucket := range val.Buckets {
|
|
|
|
if bucket.Count < 0 {
|
2021-11-15 20:05:14 +02:00
|
|
|
return fmt.Errorf("%w: bucket count may not be negative", errBadPoint)
|
2021-03-11 19:49:20 +02:00
|
|
|
}
|
|
|
|
bucketCounts[i] = uint64(bucket.Count)
|
|
|
|
}
|
|
|
|
if val.Count < 0 {
|
2021-11-15 20:05:14 +02:00
|
|
|
return fmt.Errorf("%w: count may not be negative", errBadPoint)
|
2021-03-11 19:49:20 +02:00
|
|
|
}
|
2021-11-15 20:05:14 +02:00
|
|
|
return recorder(&ocDistAggregator{
|
2021-03-11 19:49:20 +02:00
|
|
|
sum: number.NewFloat64Number(val.Sum),
|
|
|
|
count: uint64(val.Count),
|
|
|
|
buckets: aggregation.Buckets{
|
|
|
|
Boundaries: val.BucketOptions.Bounds,
|
|
|
|
Counts: bucketCounts,
|
|
|
|
},
|
2021-11-15 20:05:14 +02:00
|
|
|
}, pts[len(pts)-1].Time)
|
2021-03-11 19:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ocDistAggregator struct {
|
|
|
|
sum number.Number
|
|
|
|
count uint64
|
|
|
|
buckets aggregation.Buckets
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kind returns the kind of aggregation this is.
|
|
|
|
func (o *ocDistAggregator) Kind() aggregation.Kind {
|
|
|
|
return aggregation.HistogramKind
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sum returns the sum of values.
|
|
|
|
func (o *ocDistAggregator) Sum() (number.Number, error) {
|
|
|
|
return o.sum, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count returns the number of values.
|
|
|
|
func (o *ocDistAggregator) Count() (uint64, error) {
|
|
|
|
return o.count, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Histogram returns the count of events in pre-determined buckets.
|
|
|
|
func (o *ocDistAggregator) Histogram() (aggregation.Buckets, error) {
|
|
|
|
return o.buckets, nil
|
|
|
|
}
|