|
| 1 | +// Copyright 2021, OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package telegrafreceiver |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/influxdata/telegraf" |
| 21 | + "go.opentelemetry.io/collector/consumer/pdata" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + fieldLabel = "field" |
| 26 | +) |
| 27 | + |
| 28 | +type MetricConverter interface { |
| 29 | + Convert(telegraf.Metric) (pdata.Metrics, error) |
| 30 | +} |
| 31 | + |
| 32 | +type metricConverter struct { |
| 33 | + separateField bool |
| 34 | +} |
| 35 | + |
| 36 | +func newConverter(separateField bool) MetricConverter { |
| 37 | + return metricConverter{ |
| 38 | + separateField: separateField, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// Convert converts telegraf.Metric to pdata.Metrics. |
| 43 | +func (mc metricConverter) Convert(m telegraf.Metric) (pdata.Metrics, error) { |
| 44 | + ms := pdata.NewMetrics() |
| 45 | + rms := ms.ResourceMetrics() |
| 46 | + rms.Resize(1) |
| 47 | + rm := rms.At(0) |
| 48 | + |
| 49 | + // Attach tags as attributes - pipe through the metadata |
| 50 | + for _, t := range m.TagList() { |
| 51 | + rm.Resource().Attributes().InsertString(t.Key, t.Value) |
| 52 | + } |
| 53 | + |
| 54 | + rm.InstrumentationLibraryMetrics().Resize(1) |
| 55 | + ilm := rm.InstrumentationLibraryMetrics().At(0) |
| 56 | + |
| 57 | + il := ilm.InstrumentationLibrary() |
| 58 | + il.SetName(typeStr) |
| 59 | + il.SetVersion(versionStr) |
| 60 | + |
| 61 | + tim := m.Time().UnixNano() |
| 62 | + |
| 63 | + metrics := ilm.Metrics() |
| 64 | + |
| 65 | + switch t := m.Type(); t { |
| 66 | + case telegraf.Gauge: |
| 67 | + for _, f := range m.FieldList() { |
| 68 | + pm := pdata.NewMetric() |
| 69 | + |
| 70 | + if mc.separateField { |
| 71 | + pm.SetName(m.Name()) |
| 72 | + } else { |
| 73 | + pm.SetName(m.Name() + "_" + f.Key) |
| 74 | + } |
| 75 | + |
| 76 | + switch v := f.Value.(type) { |
| 77 | + case float64: |
| 78 | + pm.SetDataType(pdata.MetricDataTypeDoubleGauge) |
| 79 | + dps := pm.DoubleGauge().DataPoints() |
| 80 | + dps.Resize(1) |
| 81 | + dp := dps.At(0) |
| 82 | + dp.SetValue(v) |
| 83 | + dp.SetTimestamp(pdata.TimestampUnixNano(tim)) |
| 84 | + if mc.separateField { |
| 85 | + dp.LabelsMap().Insert(fieldLabel, f.Key) |
| 86 | + } |
| 87 | + |
| 88 | + case int64, uint64: |
| 89 | + pm.SetDataType(pdata.MetricDataTypeIntGauge) |
| 90 | + dps := pm.IntGauge().DataPoints() |
| 91 | + dps.Resize(1) |
| 92 | + dp := dps.At(0) |
| 93 | + switch vv := v.(type) { |
| 94 | + case int64: |
| 95 | + dp.SetValue(vv) |
| 96 | + case uint64: |
| 97 | + dp.SetValue(int64(vv)) |
| 98 | + } |
| 99 | + |
| 100 | + dp.SetTimestamp(pdata.TimestampUnixNano(tim)) |
| 101 | + if mc.separateField { |
| 102 | + dp.LabelsMap().Insert(fieldLabel, f.Key) |
| 103 | + } |
| 104 | + |
| 105 | + default: |
| 106 | + return pdata.Metrics{}, |
| 107 | + fmt.Errorf("unknown data type in telegraf.Gauge metric: %T", v) |
| 108 | + } |
| 109 | + metrics.Append(pm) |
| 110 | + } |
| 111 | + |
| 112 | + case telegraf.Counter: |
| 113 | + return pdata.Metrics{}, fmt.Errorf("unsupported metric type: telegraf.Counter") |
| 114 | + case telegraf.Untyped: |
| 115 | + return pdata.Metrics{}, fmt.Errorf("unsupported metric type: telegraf.Untyped") |
| 116 | + case telegraf.Summary: |
| 117 | + return pdata.Metrics{}, fmt.Errorf("unsupported metric type: telegraf.Summary") |
| 118 | + case telegraf.Histogram: |
| 119 | + return pdata.Metrics{}, fmt.Errorf("unsupported metric type: telegraf.Histogram") |
| 120 | + |
| 121 | + default: |
| 122 | + return pdata.Metrics{}, fmt.Errorf("unknown metric type: %T", t) |
| 123 | + } |
| 124 | + |
| 125 | + return ms, nil |
| 126 | +} |
0 commit comments