/
oc_to_metrics.go
348 lines (304 loc) · 12.1 KB
/
oc_to_metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package opencensus // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/opencensus"
import (
occommon "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
ocmetrics "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
ocresource "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
)
// OCToMetrics converts OC data format to pmetric.Metrics,
// may be used only by OpenCensus receiver and exporter implementations.
func OCToMetrics(node *occommon.Node, resource *ocresource.Resource, metrics []*ocmetrics.Metric) pmetric.Metrics {
dest := pmetric.NewMetrics()
if node == nil && resource == nil && len(metrics) == 0 {
return dest
}
rms := dest.ResourceMetrics()
initialRmsLen := rms.Len()
if len(metrics) == 0 {
// At least one of the md.Node or md.Resource is not nil. Set the resource and return.
ocNodeResourceToInternal(node, resource, rms.AppendEmpty().Resource())
return dest
}
// We may need to split OC metrics into several ResourceMetrics. OC metrics can have a
// Resource field inside them set to nil which indicates they use the Resource
// specified in "md.Resource", or they can have the Resource field inside them set
// to non-nil which indicates they have overridden Resource field and "md.Resource"
// does not apply to those metrics.
//
// Each OC metric that has its own Resource field set to non-nil must be placed in a
// separate ResourceMetrics instance, containing only that metric. All other OC Metrics
// that have nil Resource field must be placed in one other ResourceMetrics instance,
// which will gets its Resource field from "md.Resource".
//
// We will end up with with one or more ResourceMetrics like this:
//
// ResourceMetrics ResourceMetrics ResourceMetrics
// +-------+-------+---+-------+ +--------------+ +--------------+
// |Metric1|Metric2|...|MetricM| |Metric | |Metric | ...
// +-------+-------+---+-------+ +--------------+ +--------------+
// Count the number of metrics that have nil Resource and need to be combined
// in one slice.
combinedMetricCount := 0
distinctResourceCount := 0
for _, ocMetric := range metrics {
if ocMetric == nil {
// Skip nil metrics.
continue
}
if ocMetric.Resource == nil {
combinedMetricCount++
} else {
distinctResourceCount++
}
}
// Total number of resources is equal to:
// initial + numMetricsWithResource + (optional) 1
resourceCount := initialRmsLen + distinctResourceCount
if combinedMetricCount > 0 {
// +1 for all metrics with nil resource
resourceCount++
}
rms.EnsureCapacity(resourceCount)
// Translate "combinedMetrics" first
if combinedMetricCount > 0 {
rm0 := rms.AppendEmpty()
ocNodeResourceToInternal(node, resource, rm0.Resource())
// Allocate a slice for metrics that need to be combined into first ResourceMetrics.
ilms := rm0.ScopeMetrics()
combinedMetrics := ilms.AppendEmpty().Metrics()
combinedMetrics.EnsureCapacity(combinedMetricCount)
for _, ocMetric := range metrics {
if ocMetric == nil {
// Skip nil metrics.
continue
}
if ocMetric.Resource != nil {
continue // Those are processed separately below.
}
// Add the metric to the "combinedMetrics". combinedMetrics length is equal
// to combinedMetricCount. The loop above that calculates combinedMetricCount
// has exact same conditions as we have here in this loop.
ocMetricToMetrics(ocMetric, combinedMetrics.AppendEmpty())
}
}
// Translate distinct metrics
for _, ocMetric := range metrics {
if ocMetric == nil {
// Skip nil metrics.
continue
}
if ocMetric.Resource == nil {
continue // Already processed above.
}
// This metric has a different Resource and must be placed in a different
// ResourceMetrics instance. Create a separate ResourceMetrics item just for this metric
// and store at resourceMetricIdx.
ocMetricToResourceMetrics(ocMetric, node, rms.AppendEmpty())
}
return dest
}
func ocMetricToResourceMetrics(ocMetric *ocmetrics.Metric, node *occommon.Node, out pmetric.ResourceMetrics) {
ocNodeResourceToInternal(node, ocMetric.Resource, out.Resource())
ilms := out.ScopeMetrics()
ocMetricToMetrics(ocMetric, ilms.AppendEmpty().Metrics().AppendEmpty())
}
func ocMetricToMetrics(ocMetric *ocmetrics.Metric, metric pmetric.Metric) {
ocDescriptor := ocMetric.GetMetricDescriptor()
if ocDescriptor == nil {
pmetric.NewMetric().CopyTo(metric)
return
}
dataType, valType := descriptorTypeToMetrics(ocDescriptor.Type, metric)
if dataType == pmetric.MetricTypeEmpty {
pmetric.NewMetric().CopyTo(metric)
return
}
metric.SetDescription(ocDescriptor.GetDescription())
metric.SetName(ocDescriptor.GetName())
metric.SetUnit(ocDescriptor.GetUnit())
setDataPoints(ocMetric, metric, valType)
}
func descriptorTypeToMetrics(t ocmetrics.MetricDescriptor_Type, metric pmetric.Metric) (pmetric.MetricType, pmetric.NumberDataPointValueType) {
switch t {
case ocmetrics.MetricDescriptor_GAUGE_INT64:
metric.SetEmptyGauge()
return pmetric.MetricTypeGauge, pmetric.NumberDataPointValueTypeInt
case ocmetrics.MetricDescriptor_GAUGE_DOUBLE:
metric.SetEmptyGauge()
return pmetric.MetricTypeGauge, pmetric.NumberDataPointValueTypeDouble
case ocmetrics.MetricDescriptor_CUMULATIVE_INT64:
sum := metric.SetEmptySum()
sum.SetIsMonotonic(true)
sum.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
return pmetric.MetricTypeSum, pmetric.NumberDataPointValueTypeInt
case ocmetrics.MetricDescriptor_CUMULATIVE_DOUBLE:
sum := metric.SetEmptySum()
sum.SetIsMonotonic(true)
sum.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
return pmetric.MetricTypeSum, pmetric.NumberDataPointValueTypeDouble
case ocmetrics.MetricDescriptor_CUMULATIVE_DISTRIBUTION:
histo := metric.SetEmptyHistogram()
histo.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
return pmetric.MetricTypeHistogram, pmetric.NumberDataPointValueTypeEmpty
case ocmetrics.MetricDescriptor_SUMMARY:
metric.SetEmptySummary()
// no temporality specified for summary metric
return pmetric.MetricTypeSummary, pmetric.NumberDataPointValueTypeEmpty
}
return pmetric.MetricTypeEmpty, pmetric.NumberDataPointValueTypeEmpty
}
// setDataPoints converts OC timeseries to internal datapoints based on metric type
func setDataPoints(ocMetric *ocmetrics.Metric, metric pmetric.Metric, valType pmetric.NumberDataPointValueType) {
switch metric.Type() {
case pmetric.MetricTypeGauge:
fillNumberDataPoint(ocMetric, metric.Gauge().DataPoints(), valType)
case pmetric.MetricTypeSum:
fillNumberDataPoint(ocMetric, metric.Sum().DataPoints(), valType)
case pmetric.MetricTypeHistogram:
fillDoubleHistogramDataPoint(ocMetric, metric.Histogram().DataPoints())
case pmetric.MetricTypeSummary:
fillDoubleSummaryDataPoint(ocMetric, metric.Summary().DataPoints())
}
}
func fillAttributesMap(ocLabelsKeys []*ocmetrics.LabelKey, ocLabelValues []*ocmetrics.LabelValue, attributesMap pcommon.Map) {
if len(ocLabelsKeys) == 0 || len(ocLabelValues) == 0 {
return
}
lablesCount := len(ocLabelsKeys)
// Handle invalid length of OC label values list
if len(ocLabelValues) < lablesCount {
lablesCount = len(ocLabelValues)
}
attributesMap.EnsureCapacity(lablesCount)
for i := 0; i < lablesCount; i++ {
if !ocLabelValues[i].GetHasValue() {
continue
}
attributesMap.PutStr(ocLabelsKeys[i].Key, ocLabelValues[i].Value)
}
}
func fillNumberDataPoint(ocMetric *ocmetrics.Metric, dps pmetric.NumberDataPointSlice, valType pmetric.NumberDataPointValueType) {
ocPointsCount := getPointsCount(ocMetric)
dps.EnsureCapacity(ocPointsCount)
ocLabelsKeys := ocMetric.GetMetricDescriptor().GetLabelKeys()
for _, timeseries := range ocMetric.GetTimeseries() {
if timeseries == nil {
continue
}
startTimestamp := pcommon.NewTimestampFromTime(timeseries.GetStartTimestamp().AsTime())
for _, point := range timeseries.GetPoints() {
if point == nil {
continue
}
dp := dps.AppendEmpty()
dp.SetStartTimestamp(startTimestamp)
dp.SetTimestamp(pcommon.NewTimestampFromTime(point.GetTimestamp().AsTime()))
fillAttributesMap(ocLabelsKeys, timeseries.LabelValues, dp.Attributes())
switch valType {
case pmetric.NumberDataPointValueTypeInt:
dp.SetIntValue(point.GetInt64Value())
case pmetric.NumberDataPointValueTypeDouble:
dp.SetDoubleValue(point.GetDoubleValue())
}
}
}
}
func fillDoubleHistogramDataPoint(ocMetric *ocmetrics.Metric, dps pmetric.HistogramDataPointSlice) {
ocPointsCount := getPointsCount(ocMetric)
dps.EnsureCapacity(ocPointsCount)
ocLabelsKeys := ocMetric.GetMetricDescriptor().GetLabelKeys()
for _, timeseries := range ocMetric.GetTimeseries() {
if timeseries == nil {
continue
}
startTimestamp := pcommon.NewTimestampFromTime(timeseries.GetStartTimestamp().AsTime())
for _, point := range timeseries.GetPoints() {
if point == nil {
continue
}
dp := dps.AppendEmpty()
dp.SetStartTimestamp(startTimestamp)
dp.SetTimestamp(pcommon.NewTimestampFromTime(point.GetTimestamp().AsTime()))
fillAttributesMap(ocLabelsKeys, timeseries.LabelValues, dp.Attributes())
distributionValue := point.GetDistributionValue()
dp.SetSum(distributionValue.GetSum())
dp.SetCount(uint64(distributionValue.GetCount()))
ocHistogramBucketsToMetrics(distributionValue.GetBuckets(), dp)
dp.ExplicitBounds().FromRaw(distributionValue.GetBucketOptions().GetExplicit().GetBounds())
}
}
}
func fillDoubleSummaryDataPoint(ocMetric *ocmetrics.Metric, dps pmetric.SummaryDataPointSlice) {
ocPointsCount := getPointsCount(ocMetric)
dps.EnsureCapacity(ocPointsCount)
ocLabelsKeys := ocMetric.GetMetricDescriptor().GetLabelKeys()
for _, timeseries := range ocMetric.GetTimeseries() {
if timeseries == nil {
continue
}
startTimestamp := pcommon.NewTimestampFromTime(timeseries.GetStartTimestamp().AsTime())
for _, point := range timeseries.GetPoints() {
if point == nil {
continue
}
dp := dps.AppendEmpty()
dp.SetStartTimestamp(startTimestamp)
dp.SetTimestamp(pcommon.NewTimestampFromTime(point.GetTimestamp().AsTime()))
fillAttributesMap(ocLabelsKeys, timeseries.LabelValues, dp.Attributes())
summaryValue := point.GetSummaryValue()
dp.SetSum(summaryValue.GetSum().GetValue())
dp.SetCount(uint64(summaryValue.GetCount().GetValue()))
ocSummaryPercentilesToMetrics(summaryValue.GetSnapshot().GetPercentileValues(), dp)
}
}
}
func ocHistogramBucketsToMetrics(ocBuckets []*ocmetrics.DistributionValue_Bucket, dp pmetric.HistogramDataPoint) {
if len(ocBuckets) == 0 {
return
}
buckets := make([]uint64, len(ocBuckets))
for i := range buckets {
buckets[i] = uint64(ocBuckets[i].GetCount())
if ocBuckets[i].GetExemplar() != nil {
exemplar := dp.Exemplars().AppendEmpty()
exemplarToMetrics(ocBuckets[i].GetExemplar(), exemplar)
}
}
dp.BucketCounts().FromRaw(buckets)
}
func ocSummaryPercentilesToMetrics(ocPercentiles []*ocmetrics.SummaryValue_Snapshot_ValueAtPercentile, dp pmetric.SummaryDataPoint) {
if len(ocPercentiles) == 0 {
return
}
quantiles := pmetric.NewSummaryDataPointValueAtQuantileSlice()
quantiles.EnsureCapacity(len(ocPercentiles))
for _, percentile := range ocPercentiles {
quantile := quantiles.AppendEmpty()
quantile.SetQuantile(percentile.GetPercentile() / 100)
quantile.SetValue(percentile.GetValue())
}
quantiles.CopyTo(dp.QuantileValues())
}
func exemplarToMetrics(ocExemplar *ocmetrics.DistributionValue_Exemplar, exemplar pmetric.Exemplar) {
if ocExemplar.GetTimestamp() != nil {
exemplar.SetTimestamp(pcommon.NewTimestampFromTime(ocExemplar.GetTimestamp().AsTime()))
}
ocAttachments := ocExemplar.GetAttachments()
exemplar.SetDoubleValue(ocExemplar.GetValue())
filteredAttributes := exemplar.FilteredAttributes()
filteredAttributes.EnsureCapacity(len(ocAttachments))
for k, v := range ocAttachments {
filteredAttributes.PutStr(k, v)
}
}
func getPointsCount(ocMetric *ocmetrics.Metric) int {
timeseriesSlice := ocMetric.GetTimeseries()
var count int
for _, timeseries := range timeseriesSlice {
count += len(timeseries.GetPoints())
}
return count
}