-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathresource_to_telemetry.go
106 lines (92 loc) · 3.52 KB
/
resource_to_telemetry.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
// 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.
package resourcetotelemetry
import (
"context"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/model/pdata"
)
// Settings defines configuration for converting resource attributes to telemetry attributes.
// When used, it must be embedded in the exporter configuration:
// type Config struct {
// // ...
// resourcetotelemetry.Settings `mapstructure:"resource_to_telemetry_conversion"`
// }
type Settings struct {
// Enabled indicates whether to convert resource attributes to telemetry attributes. Default is `false`.
Enabled bool `mapstructure:"enabled"`
}
type wrapperMetricsExporter struct {
component.MetricsExporter
}
func (wme *wrapperMetricsExporter) ConsumeMetrics(ctx context.Context, md pdata.Metrics) error {
return wme.MetricsExporter.ConsumeMetrics(ctx, convertToMetricsAttributes(md))
}
func (wme *wrapperMetricsExporter) Capabilities() consumer.Capabilities {
// Always return false since this wrapper clones the data.
return consumer.Capabilities{MutatesData: false}
}
// WrapMetricsExporter wraps a given component.MetricsExporter and based on the given settings
// converts incoming resource attributes to metrics attributes.
func WrapMetricsExporter(set Settings, exporter component.MetricsExporter) component.MetricsExporter {
if !set.Enabled {
return exporter
}
return &wrapperMetricsExporter{MetricsExporter: exporter}
}
func convertToMetricsAttributes(md pdata.Metrics) pdata.Metrics {
cloneMd := md.Clone()
rms := cloneMd.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
resource := rms.At(i).Resource()
ilms := rms.At(i).InstrumentationLibraryMetrics()
for j := 0; j < ilms.Len(); j++ {
ilm := ilms.At(j)
metricSlice := ilm.Metrics()
for k := 0; k < metricSlice.Len(); k++ {
metric := metricSlice.At(k)
addAttributesToMetric(&metric, resource.Attributes())
}
}
}
return cloneMd
}
// addAttributesToMetric adds additional labels to the given metric
func addAttributesToMetric(metric *pdata.Metric, labelMap pdata.AttributeMap) {
switch metric.DataType() {
case pdata.MetricDataTypeGauge:
addAttributesToNumberDataPoints(metric.Gauge().DataPoints(), labelMap)
case pdata.MetricDataTypeSum:
addAttributesToNumberDataPoints(metric.Sum().DataPoints(), labelMap)
case pdata.MetricDataTypeHistogram:
addAttributesToHistogramDataPoints(metric.Histogram().DataPoints(), labelMap)
}
}
func addAttributesToNumberDataPoints(ps pdata.NumberDataPointSlice, newAttributeMap pdata.AttributeMap) {
for i := 0; i < ps.Len(); i++ {
joinAttributeMaps(newAttributeMap, ps.At(i).Attributes())
}
}
func addAttributesToHistogramDataPoints(ps pdata.HistogramDataPointSlice, newAttributeMap pdata.AttributeMap) {
for i := 0; i < ps.Len(); i++ {
joinAttributeMaps(newAttributeMap, ps.At(i).Attributes())
}
}
func joinAttributeMaps(from, to pdata.AttributeMap) {
from.Range(func(k string, v pdata.AttributeValue) bool {
to.Upsert(k, v)
return true
})
}