|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package opencensus |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "fmt" |
| 20 | + "time" |
| 21 | + |
| 22 | + "go.opencensus.io/metric/metricdata" |
| 23 | + |
| 24 | + "go.opentelemetry.io/otel/metric/number" |
| 25 | + "go.opentelemetry.io/otel/sdk/export/metric/aggregation" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + errIncompatibleType = errors.New("incompatible type for aggregation") |
| 30 | + errEmpty = errors.New("points may not be empty") |
| 31 | + errBadPoint = errors.New("point cannot be converted") |
| 32 | +) |
| 33 | + |
| 34 | +// aggregationWithEndTime is an aggregation that can also provide the timestamp |
| 35 | +// of the last recorded point. |
| 36 | +type aggregationWithEndTime interface { |
| 37 | + aggregation.Aggregation |
| 38 | + end() time.Time |
| 39 | +} |
| 40 | + |
| 41 | +// newAggregationFromPoints creates an OpenTelemetry aggregation from |
| 42 | +// OpenCensus points. Points may not be empty and must be either |
| 43 | +// all (int|float)64 or all *metricdata.Distribution. |
| 44 | +func newAggregationFromPoints(points []metricdata.Point) (aggregationWithEndTime, error) { |
| 45 | + if len(points) == 0 { |
| 46 | + return nil, errEmpty |
| 47 | + } |
| 48 | + switch t := points[0].Value.(type) { |
| 49 | + case int64: |
| 50 | + return newExactAggregator(points) |
| 51 | + case float64: |
| 52 | + return newExactAggregator(points) |
| 53 | + case *metricdata.Distribution: |
| 54 | + return newDistributionAggregator(points) |
| 55 | + default: |
| 56 | + // TODO add *metricdata.Summary support |
| 57 | + return nil, fmt.Errorf("%w: %v", errIncompatibleType, t) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +var _ aggregation.Aggregation = &ocExactAggregator{} |
| 62 | +var _ aggregation.LastValue = &ocExactAggregator{} |
| 63 | +var _ aggregation.Points = &ocExactAggregator{} |
| 64 | + |
| 65 | +// newExactAggregator creates an OpenTelemetry aggreation from OpenCensus points. |
| 66 | +// Points may not be empty, and must only contain integers or floats. |
| 67 | +func newExactAggregator(pts []metricdata.Point) (aggregationWithEndTime, error) { |
| 68 | + points := make([]aggregation.Point, len(pts)) |
| 69 | + for i, pt := range pts { |
| 70 | + switch t := pt.Value.(type) { |
| 71 | + case int64: |
| 72 | + points[i] = aggregation.Point{ |
| 73 | + Number: number.NewInt64Number(pt.Value.(int64)), |
| 74 | + Time: pt.Time, |
| 75 | + } |
| 76 | + case float64: |
| 77 | + points[i] = aggregation.Point{ |
| 78 | + Number: number.NewFloat64Number(pt.Value.(float64)), |
| 79 | + Time: pt.Time, |
| 80 | + } |
| 81 | + default: |
| 82 | + return nil, fmt.Errorf("%w: %v", errIncompatibleType, t) |
| 83 | + } |
| 84 | + } |
| 85 | + return &ocExactAggregator{ |
| 86 | + points: points, |
| 87 | + }, nil |
| 88 | +} |
| 89 | + |
| 90 | +type ocExactAggregator struct { |
| 91 | + points []aggregation.Point |
| 92 | +} |
| 93 | + |
| 94 | +// Kind returns the kind of aggregation this is. |
| 95 | +func (o *ocExactAggregator) Kind() aggregation.Kind { |
| 96 | + return aggregation.ExactKind |
| 97 | +} |
| 98 | + |
| 99 | +// Points returns access to the raw data set. |
| 100 | +func (o *ocExactAggregator) Points() ([]aggregation.Point, error) { |
| 101 | + return o.points, nil |
| 102 | +} |
| 103 | + |
| 104 | +// LastValue returns the last point. |
| 105 | +func (o *ocExactAggregator) LastValue() (number.Number, time.Time, error) { |
| 106 | + last := o.points[len(o.points)-1] |
| 107 | + return last.Number, last.Time, nil |
| 108 | +} |
| 109 | + |
| 110 | +// end returns the timestamp of the last point |
| 111 | +func (o *ocExactAggregator) end() time.Time { |
| 112 | + _, t, _ := o.LastValue() |
| 113 | + return t |
| 114 | +} |
| 115 | + |
| 116 | +var _ aggregation.Aggregation = &ocDistAggregator{} |
| 117 | +var _ aggregation.Histogram = &ocDistAggregator{} |
| 118 | + |
| 119 | +// newDistributionAggregator creates an OpenTelemetry aggreation from |
| 120 | +// OpenCensus points. Points may not be empty, and must only contain |
| 121 | +// Distributions. The most recent disribution will be used in the aggregation. |
| 122 | +func newDistributionAggregator(pts []metricdata.Point) (aggregationWithEndTime, error) { |
| 123 | + // only use the most recent datapoint for now. |
| 124 | + pt := pts[len(pts)-1] |
| 125 | + val, ok := pt.Value.(*metricdata.Distribution) |
| 126 | + if !ok { |
| 127 | + return nil, fmt.Errorf("%w: %v", errBadPoint, pt.Value) |
| 128 | + } |
| 129 | + bucketCounts := make([]uint64, len(val.Buckets)) |
| 130 | + for i, bucket := range val.Buckets { |
| 131 | + if bucket.Count < 0 { |
| 132 | + return nil, fmt.Errorf("%w: bucket count may not be negative", errBadPoint) |
| 133 | + } |
| 134 | + bucketCounts[i] = uint64(bucket.Count) |
| 135 | + } |
| 136 | + if val.Count < 0 { |
| 137 | + return nil, fmt.Errorf("%w: count may not be negative", errBadPoint) |
| 138 | + } |
| 139 | + return &ocDistAggregator{ |
| 140 | + sum: number.NewFloat64Number(val.Sum), |
| 141 | + count: uint64(val.Count), |
| 142 | + buckets: aggregation.Buckets{ |
| 143 | + Boundaries: val.BucketOptions.Bounds, |
| 144 | + Counts: bucketCounts, |
| 145 | + }, |
| 146 | + endTime: pts[len(pts)-1].Time, |
| 147 | + }, nil |
| 148 | +} |
| 149 | + |
| 150 | +type ocDistAggregator struct { |
| 151 | + sum number.Number |
| 152 | + count uint64 |
| 153 | + buckets aggregation.Buckets |
| 154 | + endTime time.Time |
| 155 | +} |
| 156 | + |
| 157 | +// Kind returns the kind of aggregation this is. |
| 158 | +func (o *ocDistAggregator) Kind() aggregation.Kind { |
| 159 | + return aggregation.HistogramKind |
| 160 | +} |
| 161 | + |
| 162 | +// Sum returns the sum of values. |
| 163 | +func (o *ocDistAggregator) Sum() (number.Number, error) { |
| 164 | + return o.sum, nil |
| 165 | +} |
| 166 | + |
| 167 | +// Count returns the number of values. |
| 168 | +func (o *ocDistAggregator) Count() (uint64, error) { |
| 169 | + return o.count, nil |
| 170 | +} |
| 171 | + |
| 172 | +// Histogram returns the count of events in pre-determined buckets. |
| 173 | +func (o *ocDistAggregator) Histogram() (aggregation.Buckets, error) { |
| 174 | + return o.buckets, nil |
| 175 | +} |
| 176 | + |
| 177 | +// end returns the time the histogram was measured. |
| 178 | +func (o *ocDistAggregator) end() time.Time { |
| 179 | + return o.endTime |
| 180 | +} |
0 commit comments