/
metrics_otel_v1.go
187 lines (162 loc) · 6.29 KB
/
metrics_otel_v1.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
// 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 otel2influx // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/influx/otel2influx"
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/multierr"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/influx/common"
)
var _ metricWriter = (*metricWriterOtelV1)(nil)
type metricWriterOtelV1 struct {
logger common.Logger
}
func (m *metricWriterOtelV1) writeMetric(ctx context.Context, resource pcommon.Resource, is pcommon.InstrumentationScope, pm pmetric.Metric, batch InfluxWriterBatch) (err error) {
defer func() {
if r := recover(); r != nil {
var rerr error
switch v := r.(type) {
case error:
rerr = v
case string:
rerr = errors.New(v)
default:
rerr = fmt.Errorf("%+v", r)
}
err = multierr.Combine(err, rerr)
}
if err != nil && !consumererror.IsPermanent(err) {
m.logger.Debug(err.Error())
err = nil
}
}()
// TODO metric description
measurementName := fmt.Sprintf("%s_%s_%s", pm.Name(), pm.Unit(), strings.ToLower(pm.Type().String()))
resourceTags := convertResourceTags(resource)
scopeFields := convertScopeFields(is)
switch pm.Type() {
case pmetric.MetricTypeSum:
m.writeSum(ctx, measurementName, resourceTags, scopeFields, pm, batch)
case pmetric.MetricTypeHistogram:
m.writeHistogram(ctx, measurementName, resourceTags, scopeFields, pm, batch)
default:
err = fmt.Errorf("unrecognized metric type %q", pm.Type())
}
return
}
// formatFieldKeyMetricSumOtelV1 composes a value field key from (sum temporality, sum monotonicity, and datapoint value type)
func formatFieldKeyMetricSumOtelV1(temporality string, monotonic bool, dataPointValueType string) string {
var monotonicity string
if monotonic {
monotonicity = "monotonic"
} else {
monotonicity = "nonmonotonic"
}
return fmt.Sprintf("value_%s_%s_%s", strings.ToLower(temporality), monotonicity, strings.ToLower(dataPointValueType))
}
func (m *metricWriterOtelV1) writeSum(ctx context.Context, measurementName string, resourceTags map[string]string, scopeFields map[string]interface{}, pm pmetric.Metric, batch InfluxWriterBatch) {
temporality := pm.Sum().AggregationTemporality().String()
monotonic := pm.Sum().IsMonotonic()
buildValue := func(dataPoint pmetric.NumberDataPoint) (string, interface{}) {
fieldKey := formatFieldKeyMetricSumOtelV1(temporality, monotonic, dataPoint.ValueType().String())
switch dataPoint.ValueType() {
case pmetric.NumberDataPointValueTypeInt:
return fieldKey, dataPoint.IntValue()
case pmetric.NumberDataPointValueTypeDouble:
return fieldKey, dataPoint.DoubleValue()
default:
panic(fmt.Sprintf("unsupported data point value type '%s'", dataPoint.ValueType().String()))
}
}
for i := 0; i < pm.Sum().DataPoints().Len(); i++ {
// TODO datapoint exemplars
// TODO datapoint flags
dataPoint := pm.Sum().DataPoints().At(i)
fields := make(map[string]interface{}, len(scopeFields)+3)
if dataPoint.StartTimestamp() != 0 {
fields[common.AttributeStartTimeUnixNano] = int64(dataPoint.StartTimestamp())
}
for k, v := range scopeFields {
fields[k] = v
}
valueFieldKey, value := buildValue(dataPoint)
fields[valueFieldKey] = value
tags := make(map[string]string, dataPoint.Attributes().Len()+len(resourceTags))
for k, v := range resourceTags {
tags[k] = v
}
dataPoint.Attributes().Range(func(k string, v pcommon.Value) bool {
tags[k] = v.AsString()
return true
})
err := batch.WritePoint(ctx, measurementName, tags, fields, dataPoint.Timestamp().AsTime(), common.InfluxMetricValueTypeUntyped)
if err != nil {
panic(err)
}
}
}
func (m *metricWriterOtelV1) writeHistogram(ctx context.Context, measurementName string, resourceTags map[string]string, scopeFields map[string]interface{}, pm pmetric.Metric, batch InfluxWriterBatch) {
temporality := strings.ToLower(pm.Histogram().AggregationTemporality().String())
for i := 0; i < pm.Histogram().DataPoints().Len(); i++ {
// TODO datapoint exemplars
// TODO datapoint flags
dataPoint := pm.Histogram().DataPoints().At(i)
bucketCounts, explicitBounds := dataPoint.BucketCounts(), dataPoint.ExplicitBounds()
if bucketCounts.Len() > 0 &&
bucketCounts.Len() != explicitBounds.Len() &&
bucketCounts.Len() != explicitBounds.Len()+1 {
// The infinity bucket is not used in this schema,
// so accept input if that particular bucket is missing.
panic(fmt.Sprintf("invalid metric histogram bucket counts qty %d vs explicit bounds qty %d", bucketCounts.Len(), explicitBounds.Len()))
}
fields := make(map[string]interface{}, len(scopeFields)+explicitBounds.Len()+6)
if dataPoint.StartTimestamp() != 0 {
fields[common.AttributeStartTimeUnixNano] = int64(dataPoint.StartTimestamp())
}
for k, v := range scopeFields {
fields[k] = v
}
for i := 0; i < explicitBounds.Len(); i++ {
boundStr := strconv.FormatFloat(explicitBounds.At(i), 'f', -1, 64)
k := fmt.Sprintf("%s_%s", temporality, boundStr)
fields[k] = bucketCounts.At(i)
}
fields["count"] = dataPoint.Count()
if dataPoint.HasSum() {
fields["sum"] = dataPoint.Sum()
}
if dataPoint.HasMin() && dataPoint.HasMax() {
fields["min"] = dataPoint.Min()
fields["max"] = dataPoint.Max()
}
tags := make(map[string]string, dataPoint.Attributes().Len()+len(resourceTags))
for k, v := range resourceTags {
tags[k] = v
}
dataPoint.Attributes().Range(func(k string, v pcommon.Value) bool {
tags[k] = v.AsString()
return true
})
err := batch.WritePoint(ctx, measurementName, tags, fields, dataPoint.Timestamp().AsTime(), common.InfluxMetricValueTypeUntyped)
if err != nil {
panic(err)
}
}
}