@@ -25,11 +25,8 @@ import (
25
25
)
26
26
27
27
var (
28
- errConversion = errors .New ("converting from OpenCensus to OpenTelemetry" )
29
28
errAggregationType = errors .New ("unsupported OpenCensus aggregation type" )
30
29
errMismatchedValueTypes = errors .New ("wrong value type for data point" )
31
- errNumberDataPoint = errors .New ("converting a number data point" )
32
- errHistogramDataPoint = errors .New ("converting a histogram data point" )
33
30
errNegativeDistributionCount = errors .New ("distribution count is negative" )
34
31
errNegativeBucketCount = errors .New ("distribution bucket count is negative" )
35
32
errMismatchedAttributeKeyValues = errors .New ("mismatched number of attribute keys and values" )
@@ -38,14 +35,14 @@ var (
38
35
// ConvertMetrics converts metric data from OpenCensus to OpenTelemetry.
39
36
func ConvertMetrics (ocmetrics []* ocmetricdata.Metric ) ([]metricdata.Metrics , error ) {
40
37
otelMetrics := make ([]metricdata.Metrics , 0 , len (ocmetrics ))
41
- var errInfo [] string
38
+ var err error
42
39
for _ , ocm := range ocmetrics {
43
40
if ocm == nil {
44
41
continue
45
42
}
46
- agg , err := convertAggregation (ocm )
47
- if err != nil {
48
- errInfo = append ( errInfo , err . Error ( ))
43
+ agg , aggregationErr := convertAggregation (ocm )
44
+ if aggregationErr != nil {
45
+ err = errors . Join ( err , fmt . Errorf ( "error converting metric %v: %w" , ocm . Descriptor . Name , aggregationErr ))
49
46
continue
50
47
}
51
48
otelMetrics = append (otelMetrics , metricdata.Metrics {
@@ -55,11 +52,10 @@ func ConvertMetrics(ocmetrics []*ocmetricdata.Metric) ([]metricdata.Metrics, err
55
52
Data : agg ,
56
53
})
57
54
}
58
- var aggregatedError error
59
- if len (errInfo ) > 0 {
60
- aggregatedError = fmt .Errorf ("%w: %q" , errConversion , errInfo )
55
+ if err != nil {
56
+ return otelMetrics , fmt .Errorf ("error converting from OpenCensus to OpenTelemetry: %w" , err )
61
57
}
62
- return otelMetrics , aggregatedError
58
+ return otelMetrics , nil
63
59
}
64
60
65
61
// convertAggregation produces an aggregation based on the OpenCensus Metric.
@@ -97,17 +93,17 @@ func convertSum[N int64 | float64](labelKeys []ocmetricdata.LabelKey, ts []*ocme
97
93
// convertNumberDataPoints converts OpenCensus TimeSeries to OpenTelemetry DataPoints.
98
94
func convertNumberDataPoints [N int64 | float64 ](labelKeys []ocmetricdata.LabelKey , ts []* ocmetricdata.TimeSeries ) ([]metricdata.DataPoint [N ], error ) {
99
95
var points []metricdata.DataPoint [N ]
100
- var errInfo [] string
96
+ var err error
101
97
for _ , t := range ts {
102
- attrs , err := convertAttrs (labelKeys , t .LabelValues )
103
- if err != nil {
104
- errInfo = append ( errInfo , err . Error () )
98
+ attrs , attrsErr := convertAttrs (labelKeys , t .LabelValues )
99
+ if attrsErr != nil {
100
+ err = errors . Join ( err , attrsErr )
105
101
continue
106
102
}
107
103
for _ , p := range t .Points {
108
104
v , ok := p .Value .(N )
109
105
if ! ok {
110
- errInfo = append ( errInfo , fmt .Sprintf ("%v : %q" , errMismatchedValueTypes , p .Value ))
106
+ err = errors . Join ( err , fmt .Errorf ("%w : %q" , errMismatchedValueTypes , p .Value ))
111
107
continue
112
108
}
113
109
points = append (points , metricdata.DataPoint [N ]{
@@ -118,37 +114,33 @@ func convertNumberDataPoints[N int64 | float64](labelKeys []ocmetricdata.LabelKe
118
114
})
119
115
}
120
116
}
121
- var aggregatedError error
122
- if len (errInfo ) > 0 {
123
- aggregatedError = fmt .Errorf ("%w: %v" , errNumberDataPoint , errInfo )
124
- }
125
- return points , aggregatedError
117
+ return points , err
126
118
}
127
119
128
120
// convertHistogram converts OpenCensus Distribution timeseries to an
129
121
// OpenTelemetry Histogram aggregation.
130
122
func convertHistogram (labelKeys []ocmetricdata.LabelKey , ts []* ocmetricdata.TimeSeries ) (metricdata.Histogram [float64 ], error ) {
131
123
points := make ([]metricdata.HistogramDataPoint [float64 ], 0 , len (ts ))
132
- var errInfo [] string
124
+ var err error
133
125
for _ , t := range ts {
134
- attrs , err := convertAttrs (labelKeys , t .LabelValues )
135
- if err != nil {
136
- errInfo = append ( errInfo , err . Error () )
126
+ attrs , attrsErr := convertAttrs (labelKeys , t .LabelValues )
127
+ if attrsErr != nil {
128
+ err = errors . Join ( err , attrsErr )
137
129
continue
138
130
}
139
131
for _ , p := range t .Points {
140
132
dist , ok := p .Value .(* ocmetricdata.Distribution )
141
133
if ! ok {
142
- errInfo = append ( errInfo , fmt .Sprintf ("%v : %d" , errMismatchedValueTypes , p .Value ))
134
+ err = errors . Join ( err , fmt .Errorf ("%w : %d" , errMismatchedValueTypes , p .Value ))
143
135
continue
144
136
}
145
- bucketCounts , err := convertBucketCounts (dist .Buckets )
146
- if err != nil {
147
- errInfo = append ( errInfo , err . Error () )
137
+ bucketCounts , bucketErr := convertBucketCounts (dist .Buckets )
138
+ if bucketErr != nil {
139
+ err = errors . Join ( err , bucketErr )
148
140
continue
149
141
}
150
142
if dist .Count < 0 {
151
- errInfo = append ( errInfo , fmt .Sprintf ("%v : %d" , errNegativeDistributionCount , dist .Count ))
143
+ err = errors . Join ( err , fmt .Errorf ("%w : %d" , errNegativeDistributionCount , dist .Count ))
152
144
continue
153
145
}
154
146
// TODO: handle exemplars
@@ -163,11 +155,7 @@ func convertHistogram(labelKeys []ocmetricdata.LabelKey, ts []*ocmetricdata.Time
163
155
})
164
156
}
165
157
}
166
- var aggregatedError error
167
- if len (errInfo ) > 0 {
168
- aggregatedError = fmt .Errorf ("%w: %v" , errHistogramDataPoint , errInfo )
169
- }
170
- return metricdata.Histogram [float64 ]{DataPoints : points , Temporality : metricdata .CumulativeTemporality }, aggregatedError
158
+ return metricdata.Histogram [float64 ]{DataPoints : points , Temporality : metricdata .CumulativeTemporality }, err
171
159
}
172
160
173
161
// convertBucketCounts converts from OpenCensus bucket counts to slice of uint64.
0 commit comments