-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
metricsbuilder.go
303 lines (272 loc) · 9.2 KB
/
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
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
297
298
299
300
301
302
303
// 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 (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/textparse"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/timestamppb"
)
const (
metricsSuffixCount = "_count"
metricsSuffixBucket = "_bucket"
metricsSuffixSum = "_sum"
startTimeMetricName = "process_start_time_seconds"
scrapeUpMetricName = "up"
)
var (
trimmableSuffixes = []string{metricsSuffixBucket, metricsSuffixCount, metricsSuffixSum}
errNoDataToBuild = errors.New("there's no data to build")
errNoBoundaryLabel = errors.New("given metricType has no BucketLabel or QuantileLabel")
errEmptyBoundaryLabel = errors.New("BucketLabel or QuantileLabel is empty")
)
type metricBuilder struct {
hasData bool
hasInternalMetric bool
mc MetadataCache
metrics []*metricspb.Metric
numTimeseries int
droppedTimeseries int
useStartTimeMetric bool
startTimeMetricRegex *regexp.Regexp
startTime float64
logger *zap.Logger
currentMf MetricFamily
}
// 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 an opencensus data.MetricsData object
// by calling its Build function
func newMetricBuilder(mc MetadataCache, useStartTimeMetric bool, startTimeMetricRegex string, logger *zap.Logger) *metricBuilder {
var regex *regexp.Regexp
if startTimeMetricRegex != "" {
regex, _ = regexp.Compile(startTimeMetricRegex)
}
return &metricBuilder{
mc: mc,
metrics: make([]*metricspb.Metric, 0),
logger: logger,
numTimeseries: 0,
droppedTimeseries: 0,
useStartTimeMetric: useStartTimeMetric,
startTimeMetricRegex: regex,
}
}
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 {
metricName := ls.Get(model.MetricNameLabel)
switch {
case metricName == "":
b.numTimeseries++
b.droppedTimeseries++
return errMetricNameNotFound
case isInternalMetric(metricName):
b.hasInternalMetric = true
lm := ls.Map()
delete(lm, model.MetricNameLabel)
// 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.
if metricName == scrapeUpMetricName && v != 1.0 {
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)))
}
}
return nil
case b.useStartTimeMetric && b.matchStartTimeMetric(metricName):
b.startTime = v
}
b.hasData = true
if b.currentMf != nil && !b.currentMf.IsSameFamily(metricName) {
m, ts, dts := b.currentMf.ToMetric()
b.numTimeseries += ts
b.droppedTimeseries += dts
if m != nil {
b.metrics = append(b.metrics, m)
}
b.currentMf = newMetricFamily(metricName, b.mc)
} else if b.currentMf == nil {
b.currentMf = newMetricFamily(metricName, b.mc)
}
return b.currentMf.Add(metricName, ls, t, v)
}
// Build an opencensus data.MetricsData based on all added data complexValue.
// The only error returned by this function is errNoDataToBuild.
func (b *metricBuilder) Build() ([]*metricspb.Metric, int, int, error) {
if !b.hasData {
if b.hasInternalMetric {
return make([]*metricspb.Metric, 0), 0, 0, nil
}
return nil, 0, 0, errNoDataToBuild
}
if b.currentMf != nil {
m, ts, dts := b.currentMf.ToMetric()
b.numTimeseries += ts
b.droppedTimeseries += dts
if m != nil {
b.metrics = append(b.metrics, m)
}
b.currentMf = nil
}
return b.metrics, b.numTimeseries, b.droppedTimeseries, nil
}
// TODO: move the following helper functions to a proper place, as they are not called directly in this go file
func isUsefulLabel(mType metricspb.MetricDescriptor_Type, labelKey string) bool {
result := false
switch labelKey {
case model.MetricNameLabel:
case model.InstanceLabel:
case model.SchemeLabel:
case model.MetricsPathLabel:
case model.JobLabel:
case model.BucketLabel:
result = mType != metricspb.MetricDescriptor_GAUGE_DISTRIBUTION &&
mType != metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION
case model.QuantileLabel:
result = mType != metricspb.MetricDescriptor_SUMMARY
default:
result = true
}
return result
}
// dpgSignature is used to create a key for data complexValue belong to a same group of a metric family
func dpgSignature(orderedKnownLabelKeys []string, ls labels.Labels) string {
sign := make([]string, 0, len(orderedKnownLabelKeys))
for _, k := range orderedKnownLabelKeys {
v := ls.Get(k)
if v == "" {
continue
}
sign = append(sign, k+"="+v)
}
return fmt.Sprintf("%#v", sign)
}
func normalizeMetricName(name string) string {
for _, s := range trimmableSuffixes {
if strings.HasSuffix(name, s) && name != s {
return strings.TrimSuffix(name, s)
}
}
return name
}
func getBoundary(metricType metricspb.MetricDescriptor_Type, labels labels.Labels) (float64, error) {
labelName := ""
switch metricType {
case metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION,
metricspb.MetricDescriptor_GAUGE_DISTRIBUTION:
labelName = model.BucketLabel
case metricspb.MetricDescriptor_SUMMARY:
labelName = model.QuantileLabel
default:
return 0, errNoBoundaryLabel
}
v := labels.Get(labelName)
if v == "" {
return 0, errEmptyBoundaryLabel
}
return strconv.ParseFloat(v, 64)
}
func convToOCAMetricType(metricType textparse.MetricType) metricspb.MetricDescriptor_Type {
switch metricType {
case textparse.MetricTypeCounter:
// always use float64, as it's the internal data type used in prometheus
return metricspb.MetricDescriptor_CUMULATIVE_DOUBLE
// textparse.MetricTypeUnknown is converted to gauge by default to fix Prometheus untyped metrics from being dropped
case textparse.MetricTypeGauge, textparse.MetricTypeUnknown:
return metricspb.MetricDescriptor_GAUGE_DOUBLE
case textparse.MetricTypeHistogram:
return metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION
// 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 metricspb.MetricDescriptor_GAUGE_DISTRIBUTION
case textparse.MetricTypeSummary:
return metricspb.MetricDescriptor_SUMMARY
default:
// including: textparse.MetricTypeInfo, textparse.MetricTypeStateset
return metricspb.MetricDescriptor_UNSPECIFIED
}
}
/*
code borrowed from the original promreceiver
*/
func heuristicalMetricAndKnownUnits(metricName, parsedUnit string) string {
if parsedUnit != "" {
return parsedUnit
}
lastUnderscoreIndex := strings.LastIndex(metricName, "_")
if lastUnderscoreIndex <= 0 || lastUnderscoreIndex >= len(metricName)-1 {
return ""
}
unit := ""
supposedUnit := metricName[lastUnderscoreIndex+1:]
switch strings.ToLower(supposedUnit) {
case "millisecond", "milliseconds", "ms":
unit = "ms"
case "second", "seconds", "s":
unit = "s"
case "microsecond", "microseconds", "us":
unit = "us"
case "nanosecond", "nanoseconds", "ns":
unit = "ns"
case "byte", "bytes", "by":
unit = "By"
case "bit", "bits":
unit = "Bi"
case "kilogram", "kilograms", "kg":
unit = "kg"
case "gram", "grams", "g":
unit = "g"
case "meter", "meters", "metre", "metres", "m":
unit = "m"
case "kilometer", "kilometers", "kilometre", "kilometres", "km":
unit = "km"
case "milimeter", "milimeters", "milimetre", "milimetres", "mm":
unit = "mm"
case "nanogram", "ng", "nanograms":
unit = "ng"
}
return unit
}
func timestampFromMs(timeAtMs int64) *timestamppb.Timestamp {
secs, ns := timeAtMs/1e3, (timeAtMs%1e3)*1e6
return ×tamppb.Timestamp{
Seconds: secs,
Nanos: int32(ns),
}
}
func isInternalMetric(metricName string) bool {
if metricName == scrapeUpMetricName || strings.HasPrefix(metricName, "scrape_") {
return true
}
return false
}