-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
otlp_metricsbuilder.go
212 lines (192 loc) · 7.21 KB
/
otlp_metricsbuilder.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
// 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 internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver/internal"
import (
"fmt"
"regexp"
"sort"
"strconv"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/textparse"
"github.com/prometheus/prometheus/model/value"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
)
func isUsefulLabel(mType pmetric.MetricDataType, labelKey string) bool {
switch labelKey {
case model.MetricNameLabel, model.InstanceLabel, model.SchemeLabel, model.MetricsPathLabel, model.JobLabel:
return false
case model.BucketLabel:
return mType != pmetric.MetricDataTypeHistogram
case model.QuantileLabel:
return mType != pmetric.MetricDataTypeSummary
}
return true
}
func getBoundary(metricType pmetric.MetricDataType, labels labels.Labels) (float64, error) {
labelName := ""
switch metricType {
case pmetric.MetricDataTypeHistogram:
labelName = model.BucketLabel
case pmetric.MetricDataTypeSummary:
labelName = model.QuantileLabel
default:
return 0, errNoBoundaryLabel
}
v := labels.Get(labelName)
if v == "" {
return 0, errEmptyBoundaryLabel
}
return strconv.ParseFloat(v, 64)
}
// convToMetricType returns the data type and if it is monotonic
func convToMetricType(metricType textparse.MetricType) (pmetric.MetricDataType, bool) {
switch metricType {
case textparse.MetricTypeCounter:
// always use float64, as it's the internal data type used in prometheus
return pmetric.MetricDataTypeSum, true
// textparse.MetricTypeUnknown is converted to gauge by default to prevent Prometheus untyped metrics from being dropped
case textparse.MetricTypeGauge, textparse.MetricTypeUnknown:
return pmetric.MetricDataTypeGauge, false
case textparse.MetricTypeHistogram:
return pmetric.MetricDataTypeHistogram, true
// dropping support for gaugehistogram for now until we have an official spec of its implementation
// a draft can be found in: https://docs.google.com/document/d/1KwV0mAXwwbvvifBvDKH_LU1YjyXE_wxCkHNoCGq1GX0/edit#heading=h.1cvzqd4ksd23
// case textparse.MetricTypeGaugeHistogram:
// return <pdata gauge histogram type>
case textparse.MetricTypeSummary:
return pmetric.MetricDataTypeSummary, true
case textparse.MetricTypeInfo, textparse.MetricTypeStateset:
return pmetric.MetricDataTypeSum, false
default:
// including: textparse.MetricTypeGaugeHistogram
return pmetric.MetricDataTypeNone, false
}
}
type metricBuilder struct {
metrics pmetric.MetricSlice
families map[string]*metricFamily
hasData bool
hasInternalMetric bool
mc MetadataCache
numTimeseries int
droppedTimeseries int
useStartTimeMetric bool
startTimeMetricRegex *regexp.Regexp
startTime float64
intervalStartTimeMs int64
logger *zap.Logger
}
// newMetricBuilder creates a MetricBuilder which is allowed to feed all the datapoints from a single prometheus
// scraped page by calling its AddDataPoint function, and turn them into a pmetric.Metrics object.
// by calling its Build function
func newMetricBuilder(mc MetadataCache, useStartTimeMetric bool, startTimeMetricRegex string, logger *zap.Logger, intervalStartTimeMs int64) *metricBuilder {
var regex *regexp.Regexp
if startTimeMetricRegex != "" {
regex, _ = regexp.Compile(startTimeMetricRegex)
}
return &metricBuilder{
metrics: pmetric.NewMetricSlice(),
families: map[string]*metricFamily{},
mc: mc,
logger: logger,
numTimeseries: 0,
droppedTimeseries: 0,
useStartTimeMetric: useStartTimeMetric,
startTimeMetricRegex: regex,
intervalStartTimeMs: intervalStartTimeMs,
}
}
func (b *metricBuilder) matchStartTimeMetric(metricName string) bool {
if b.startTimeMetricRegex != nil {
return b.startTimeMetricRegex.MatchString(metricName)
}
return metricName == startTimeMetricName
}
// AddDataPoint is for feeding prometheus data complexValue in its processing order
func (b *metricBuilder) AddDataPoint(ls labels.Labels, t int64, v float64) error {
// Any datapoint with duplicate labels MUST be rejected per:
// * https://github.com/open-telemetry/wg-prometheus/issues/44
// * https://github.com/open-telemetry/opentelemetry-collector/issues/3407
// as Prometheus rejects such too as of version 2.16.0, released on 2020-02-13.
seen := make(map[string]bool)
dupLabels := make([]string, 0, len(ls))
for _, label := range ls {
if _, ok := seen[label.Name]; ok {
dupLabels = append(dupLabels, label.Name)
}
seen[label.Name] = true
}
if len(dupLabels) != 0 {
sort.Strings(dupLabels)
return fmt.Errorf("invalid sample: non-unique label names: %q", dupLabels)
}
metricName := ls.Get(model.MetricNameLabel)
switch {
case metricName == "":
b.numTimeseries++
b.droppedTimeseries++
return errMetricNameNotFound
case isInternalMetric(metricName):
b.hasInternalMetric = true
lm := ls.Map()
// See https://www.prometheus.io/docs/concepts/jobs_instances/#automatically-generated-labels-and-time-series
// up: 1 if the instance is healthy, i.e. reachable, or 0 if the scrape failed.
// But it can also be a staleNaN, which is inserted when the target goes away.
if metricName == scrapeUpMetricName && v != 1.0 && !value.IsStaleNaN(v) {
if v == 0.0 {
b.logger.Warn("Failed to scrape Prometheus endpoint",
zap.Int64("scrape_timestamp", t),
zap.String("target_labels", fmt.Sprintf("%v", lm)))
} else {
b.logger.Warn("The 'up' metric contains invalid value",
zap.Float64("value", v),
zap.Int64("scrape_timestamp", t),
zap.String("target_labels", fmt.Sprintf("%v", lm)))
}
}
case b.useStartTimeMetric && b.matchStartTimeMetric(metricName):
b.startTime = v
}
b.hasData = true
curMF, ok := b.families[metricName]
if !ok {
familyName := normalizeMetricName(metricName)
if mf, ok := b.families[familyName]; ok && mf.includesMetric(metricName) {
curMF = mf
} else {
curMF = newMetricFamily(metricName, b.mc, b.logger)
b.families[curMF.name] = curMF
}
}
return curMF.Add(metricName, ls, t, v)
}
// Build an pmetric.MetricSlice based on all added data complexValue.
// The only error returned by this function is errNoDataToBuild.
func (b *metricBuilder) Build() (*pmetric.MetricSlice, int, int, error) {
if !b.hasData {
if b.hasInternalMetric {
metricsL := pmetric.NewMetricSlice()
return &metricsL, 0, 0, nil
}
return nil, 0, 0, errNoDataToBuild
}
for _, mf := range b.families {
ts, dts := mf.ToMetric(&b.metrics)
b.numTimeseries += ts
b.droppedTimeseries += dts
}
return &b.metrics, b.numTimeseries, b.droppedTimeseries, nil
}