forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.go
294 lines (258 loc) · 9.4 KB
/
converter.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package translation // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/translation"
import (
"fmt"
"strings"
"unicode"
sfxpb "github.com/signalfx/com_signalfx_metrics_protobuf/model"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/translation/dpfilters"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/signalfx"
)
// Some fields on SignalFx protobuf are pointers, in order to reduce
// allocations create the most used ones.
var (
// SignalFx metric types used in the conversions.
sfxMetricTypeGauge = sfxpb.MetricType_GAUGE
sfxMetricTypeCumulativeCounter = sfxpb.MetricType_CUMULATIVE_COUNTER
sfxMetricTypeCounter = sfxpb.MetricType_COUNTER
)
// MetricsConverter converts MetricsData to sfxpb DataPoints. It holds an optional
// MetricTranslator to translate SFx metrics using translation rules.
type MetricsConverter struct {
logger *zap.Logger
metricTranslator *MetricTranslator
filterSet *dpfilters.FilterSet
datapointValidator *datapointValidator
translator *signalfx.FromTranslator
dropHistogramBuckets bool
processHistograms bool
}
// NewMetricsConverter creates a MetricsConverter from the passed in logger and
// MetricTranslator. Pass in a nil MetricTranslator to not use translation
// rules.
func NewMetricsConverter(
logger *zap.Logger,
t *MetricTranslator,
excludes []dpfilters.MetricFilter,
includes []dpfilters.MetricFilter,
nonAlphanumericDimChars string,
dropHistogramBuckets bool,
processHistograms bool,
) (*MetricsConverter, error) {
fs, err := dpfilters.NewFilterSet(excludes, includes)
if err != nil {
return nil, err
}
return &MetricsConverter{
logger: logger,
metricTranslator: t,
filterSet: fs,
datapointValidator: newDatapointValidator(logger, nonAlphanumericDimChars),
translator: &signalfx.FromTranslator{},
dropHistogramBuckets: dropHistogramBuckets,
processHistograms: processHistograms,
}, nil
}
func (c *MetricsConverter) Start() {
if c.metricTranslator != nil {
c.metricTranslator.Start()
}
}
// MetricsToSignalFxV2 converts the passed in MetricsData to SFx datapoints
// and if processHistograms is set, histogram metrics are not converted to SFx format.
// It returns those datapoints and the number of time series that had to be
// dropped because of errors or warnings.
func (c *MetricsConverter) MetricsToSignalFxV2(md pmetric.Metrics) []*sfxpb.DataPoint {
var sfxDataPoints []*sfxpb.DataPoint
rms := md.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
rm := rms.At(i)
extraDimensions := resourceToDimensions(rm.Resource())
for j := 0; j < rm.ScopeMetrics().Len(); j++ {
ilm := rm.ScopeMetrics().At(j)
var initialDps []*sfxpb.DataPoint
for k := 0; k < ilm.Metrics().Len(); k++ {
currentMetric := ilm.Metrics().At(k)
dps := c.translator.FromMetric(currentMetric, extraDimensions, c.dropHistogramBuckets, c.processHistograms)
initialDps = append(initialDps, dps...)
}
// Translate and filter all metrics within the current ScopeMetric
sfxDataPoints = append(sfxDataPoints, c.translateAndFilter(initialDps)...)
}
}
return c.datapointValidator.sanitizeDataPoints(sfxDataPoints)
}
func (c *MetricsConverter) translateAndFilter(dps []*sfxpb.DataPoint) []*sfxpb.DataPoint {
if c.metricTranslator != nil {
dps = c.metricTranslator.TranslateDataPoints(c.logger, dps)
}
resultSliceLen := 0
for i, dp := range dps {
if !c.filterSet.Matches(dp) {
if resultSliceLen < i {
dps[resultSliceLen] = dp
}
resultSliceLen++
} else {
c.logger.Debug("Datapoint does not match filter, skipping", zap.Stringer("dp", dp))
}
}
dps = dps[:resultSliceLen]
return dps
}
func filterKeyChars(str string, nonAlphanumericDimChars string) string {
filterMap := func(r rune) rune {
if unicode.IsLetter(r) || unicode.IsDigit(r) || strings.ContainsRune(nonAlphanumericDimChars, r) {
return r
}
return '_'
}
return strings.Map(filterMap, str)
}
// resourceToDimensions will return a set of dimension from the
// resource attributes, including a cloud host id (AWSUniqueId, gcp_id, etc.)
// if it can be constructed from the provided metadata.
func resourceToDimensions(res pcommon.Resource) []*sfxpb.Dimension {
var dims []*sfxpb.Dimension
if hostID, ok := splunk.ResourceToHostID(res); ok && hostID.Key != splunk.HostIDKeyHost {
dims = append(dims, &sfxpb.Dimension{
Key: string(hostID.Key),
Value: hostID.ID,
})
}
res.Attributes().Range(func(k string, val pcommon.Value) bool {
// Never send the SignalFX token
if k == splunk.SFxAccessTokenLabel {
return true
}
dims = append(dims, &sfxpb.Dimension{
Key: k,
Value: val.AsString(),
})
return true
})
return dims
}
func (c *MetricsConverter) ConvertDimension(dim string) string {
res := dim
if c.metricTranslator != nil {
res = c.metricTranslator.translateDimension(dim)
}
return filterKeyChars(res, c.datapointValidator.nonAlphanumericDimChars)
}
func (c *MetricsConverter) Shutdown() {
if c.metricTranslator != nil {
c.metricTranslator.Shutdown()
}
}
// Values obtained from https://dev.splunk.com/observability/docs/datamodel/ingest#Criteria-for-metric-and-dimension-names-and-values
const (
maxMetricNameLength = 256
maxDimensionNameLength = 128
maxDimensionValueLength = 256
maxNumberOfDimensions = 36
)
var (
invalidMetricNameReason = fmt.Sprintf(
"metric name longer than %d characters", maxMetricNameLength)
invalidDimensionNameReason = fmt.Sprintf(
"dimension name longer than %d characters", maxDimensionNameLength)
invalidDimensionValueReason = fmt.Sprintf(
"dimension value longer than %d characters", maxDimensionValueLength)
invalidNumberOfDimensions = fmt.Sprintf(
"number of dimensions is larger than %d", maxNumberOfDimensions)
)
type datapointValidator struct {
logger *zap.Logger
nonAlphanumericDimChars string
}
func newDatapointValidator(logger *zap.Logger, nonAlphanumericDimChars string) *datapointValidator {
return &datapointValidator{logger: logger, nonAlphanumericDimChars: nonAlphanumericDimChars}
}
// sanitizeDataPoints sanitizes datapoints prior to dispatching them to the backend.
// Datapoints that do not conform to the requirements are removed. This method drops
// datapoints with metric name greater than 256 characters and number of dimensions greater than 36.
func (dpv *datapointValidator) sanitizeDataPoints(dps []*sfxpb.DataPoint) []*sfxpb.DataPoint {
resultDatapointsLen := 0
for dpIndex, dp := range dps {
if dpv.isValidMetricName(dp.Metric) && dpv.isValidNumberOfDimension(dp) {
dp.Dimensions = dpv.sanitizeDimensions(dp.Dimensions)
if resultDatapointsLen < dpIndex {
dps[resultDatapointsLen] = dp
}
resultDatapointsLen++
}
}
// Trim datapoints slice to account for any removed datapoints.
return dps[:resultDatapointsLen]
}
// sanitizeDimensions replaces all characters unsupported by SignalFx backend
// in metric label keys and with "_" and drops dimensions when the key is greater
// than 128 characters or when value is greater than 256 characters in length.
func (dpv *datapointValidator) sanitizeDimensions(dimensions []*sfxpb.Dimension) []*sfxpb.Dimension {
resultDimensionsLen := 0
for dimensionIndex, d := range dimensions {
if dpv.isValidDimension(d) {
d.Key = filterKeyChars(d.Key, dpv.nonAlphanumericDimChars)
if resultDimensionsLen < dimensionIndex {
dimensions[resultDimensionsLen] = d
}
resultDimensionsLen++
}
}
// Trim dimensions slice to account for any removed dimensions.
return dimensions[:resultDimensionsLen]
}
func (dpv *datapointValidator) isValidMetricName(name string) bool {
if len(name) > maxMetricNameLength {
dpv.logger.Debug("dropping datapoint",
zap.String("reason", invalidMetricNameReason),
zap.String("metric_name", name),
zap.Int("metric_name_length", len(name)),
)
return false
}
return true
}
func (dpv *datapointValidator) isValidNumberOfDimension(dp *sfxpb.DataPoint) bool {
if len(dp.Dimensions) > maxNumberOfDimensions {
dpv.logger.Debug("dropping datapoint",
zap.String("reason", invalidNumberOfDimensions),
zap.Stringer("datapoint", dp),
zap.Int("number_of_dimensions", len(dp.Dimensions)),
)
return false
}
return true
}
func (dpv *datapointValidator) isValidDimension(dimension *sfxpb.Dimension) bool {
return dpv.isValidDimensionName(dimension.Key) && dpv.isValidDimensionValue(dimension.Value, dimension.Key)
}
func (dpv *datapointValidator) isValidDimensionName(name string) bool {
if len(name) > maxDimensionNameLength {
dpv.logger.Debug("dropping dimension",
zap.String("reason", invalidDimensionNameReason),
zap.String("dimension_name", name),
zap.Int("dimension_name_length", len(name)),
)
return false
}
return true
}
func (dpv *datapointValidator) isValidDimensionValue(value, name string) bool {
if len(value) > maxDimensionValueLength {
dpv.logger.Debug("dropping dimension",
zap.String("dimension_name", name),
zap.String("reason", invalidDimensionValueReason),
zap.String("dimension_value", value),
zap.Int("dimension_value_length", len(value)),
)
return false
}
return true
}