-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathconverter.go
296 lines (251 loc) · 7.43 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
295
296
// Copyright 2021, 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 telegrafreceiver
import (
"fmt"
"github.com/influxdata/telegraf"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
)
const (
fieldLabel = "field"
)
type MetricConverter interface {
Convert(telegraf.Metric) (pmetric.Metrics, error)
}
type metricConverter struct {
separateField bool
logger *zap.Logger
}
func newConverter(separateField bool, logger *zap.Logger) MetricConverter {
return metricConverter{
separateField: separateField,
logger: logger,
}
}
// Convert converts telegraf.Metric to pmetric.Metrics.
func (mc metricConverter) Convert(m telegraf.Metric) (pmetric.Metrics, error) {
ms := pmetric.NewMetrics()
rms := ms.ResourceMetrics()
rm := rms.AppendEmpty()
// Attach tags as resource attributes.
rAttributes := rm.Resource().Attributes()
for _, t := range m.TagList() {
rAttributes.PutStr(t.Key, t.Value)
}
sm := rm.ScopeMetrics().AppendEmpty()
scope := sm.Scope()
scope.SetName(typeStr)
scope.SetVersion(versionStr)
tim := m.Time()
metrics := sm.Metrics()
opts := []MetricOpt{
// Note: don't copy telegraf tags to record level attributes.
//
// This way we cannot use e.g. metricstransformprocessor. because
// as of now it only allows to manipulate record level attributes
// but we won't break existing workflows like k8sprocessor
// relying on resource level attributes.
//
// WithTags(m.TagList()),
WithTime(tim),
}
switch t := m.Type(); t {
case telegraf.Gauge:
metrics.EnsureCapacity(len(m.FieldList()))
for _, f := range m.FieldList() {
pm, err := mc.convertToGauge(m.Name(), f, opts...)
if err != nil {
mc.logger.Debug(
"unsupported data type when handling telegraf.Gauge",
zap.String("key", f.Key),
zap.Any("value", f.Value),
zap.Error(err),
)
continue
}
pm.CopyTo(metrics.AppendEmpty())
}
case telegraf.Untyped:
metrics.EnsureCapacity(len(m.FieldList()))
for _, f := range m.FieldList() {
pm, err := mc.convertToGauge(m.Name(), f, opts...)
if err != nil {
mc.logger.Debug(
"unsupported data type when handling telegraf.Untyped",
zap.String("key", f.Key),
zap.Any("value", f.Value),
zap.Error(err),
)
continue
}
pm.CopyTo(metrics.AppendEmpty())
}
case telegraf.Counter:
metrics.EnsureCapacity(len(m.FieldList()))
for _, f := range m.FieldList() {
pm, err := mc.convertToSum(m.Name(), f, opts...)
if err != nil {
mc.logger.Debug(
"unsupported data type when handling telegraf.Gauge",
zap.String("key", f.Key),
zap.Any("value", f.Value),
zap.Error(err),
)
continue
}
pm.CopyTo(metrics.AppendEmpty())
}
case telegraf.Summary:
return pmetric.Metrics{}, fmt.Errorf("unsupported metric type: telegraf.Summary")
case telegraf.Histogram:
return pmetric.Metrics{}, fmt.Errorf("unsupported metric type: telegraf.Histogram")
default:
return pmetric.Metrics{}, fmt.Errorf("unknown metric type: %T", t)
}
return ms, nil
}
// convertToGauge returns a pmetric.Metric gauge converted from telegraf metric,
// based on provided metric name, field and metric options which are passed
// to metric constructors to manipulate the created metric in a functional manner.
func (mc metricConverter) convertToGauge(name string, f *telegraf.Field, opts ...MetricOpt) (pmetric.Metric, error) {
if mc.separateField {
opts = append(opts, WithField(f.Key))
}
opts = append(opts, WithName(mc.createMetricName(name, f.Key)))
var pm pmetric.Metric
switch v := f.Value.(type) {
case float64:
pm = newDoubleGauge(v, opts...)
case int64:
pm = newIntGauge(v, opts...)
case uint64:
pm = newIntGauge(int64(v), opts...)
case bool:
var vv int64 = 0
if v {
vv = 1
}
pm = newIntGauge(vv, opts...)
default:
return pm, fmt.Errorf("unsupported underlying type: %T", v)
}
return pm, nil
}
// convertToGauge returns a pmetric.Metric sum converted from telegraf metric,
// based on provided metric name, field and metric options which are passed
// to metric constructors to manipulate the created metric in a functional manner.
func (mc metricConverter) convertToSum(name string, f *telegraf.Field, opts ...MetricOpt) (pmetric.Metric, error) {
if mc.separateField {
opts = append(opts, WithField(f.Key))
}
opts = append(opts, WithName(mc.createMetricName(name, f.Key)))
var pm pmetric.Metric
switch v := f.Value.(type) {
case float64:
pm = newDoubleSum(v, opts...)
case int64:
pm = newIntSum(v, opts...)
case uint64:
pm = newIntSum(int64(v), opts...)
case bool:
var vv int64 = 0
if v {
vv = 1
}
pm = newIntSum(vv, opts...)
default:
return pm, fmt.Errorf("unsupported underlying type: %T", v)
}
return pm, nil
}
// createMetricName returns a metric name using provided metric name and key/field.
// If metric converter was configured to create metrics with separate fields then
// don't use the provided field and just use the metric name. Field name will be
// added as data point label, with "field" key name.
func (mc metricConverter) createMetricName(name string, field string) string {
if mc.separateField {
return name
} else {
return name + "_" + field
}
}
func newDoubleSum(
value float64,
opts ...MetricOpt,
) pmetric.Metric {
pm := pmetric.NewMetric()
pm.SetEmptySum()
// "[...] OTLP Sum is either translated into a Timeseries Counter, when
// the sum is monotonic, or a Gauge when the sum is not monotonic."
// https://github.com/open-telemetry/opentelemetry-specification/blob/7fc28733/specification/metrics/datamodel.md#opentelemetry-protocol-data-model
ds := pm.Sum()
ds.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
ds.SetIsMonotonic(true)
dps := ds.DataPoints()
dp := dps.AppendEmpty()
dp.SetDoubleValue(value)
for _, opt := range opts {
opt(pm)
}
return pm
}
func newIntSum(
value int64,
opts ...MetricOpt,
) pmetric.Metric {
pm := pmetric.NewMetric()
pm.SetEmptySum()
// "[...] OTLP Sum is either translated into a Timeseries Counter, when
// the sum is monotonic, or a Gauge when the sum is not monotonic."
// https://github.com/open-telemetry/opentelemetry-specification/blob/7fc28733/specification/metrics/datamodel.md#opentelemetry-protocol-data-model
ds := pm.Sum()
ds.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
ds.SetIsMonotonic(true)
dps := ds.DataPoints()
dp := dps.AppendEmpty()
dp.SetIntValue(value)
for _, opt := range opts {
opt(pm)
}
return pm
}
func newDoubleGauge(
value float64,
opts ...MetricOpt,
) pmetric.Metric {
pm := pmetric.NewMetric()
pm.SetEmptyGauge()
dps := pm.Gauge().DataPoints()
dp := dps.AppendEmpty()
dp.SetDoubleValue(value)
for _, opt := range opts {
opt(pm)
}
return pm
}
func newIntGauge(
value int64,
opts ...MetricOpt,
) pmetric.Metric {
pm := pmetric.NewMetric()
pm.SetEmptyGauge()
dps := pm.Gauge().DataPoints()
dp := dps.AppendEmpty()
dp.SetIntValue(value)
for _, opt := range opts {
opt(pm)
}
return pm
}