-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
metricfamily.go
373 lines (339 loc) · 12.1 KB
/
metricfamily.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// 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 (
"sort"
"strings"
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/textparse"
"github.com/prometheus/prometheus/scrape"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
// MetricFamily is unit which is corresponding to the metrics items which shared the same TYPE/UNIT/... metadata from
// a single scrape.
type MetricFamily interface {
Add(metricName string, ls labels.Labels, t int64, v float64) error
IsSameFamily(metricName string) bool
ToMetric() (*metricspb.Metric, int, int)
}
type metricFamily struct {
name string
mtype metricspb.MetricDescriptor_Type
mc MetadataCache
droppedTimeseries int
labelKeys map[string]bool
labelKeysOrdered []string
metadata *scrape.MetricMetadata
groupOrders map[string]int
groups map[string]*metricGroup
}
func newMetricFamily(metricName string, mc MetadataCache) MetricFamily {
familyName := normalizeMetricName(metricName)
// lookup metadata based on familyName
metadata, ok := mc.Metadata(familyName)
if !ok && metricName != familyName {
// use the original metricName as metricFamily
familyName = metricName
// perform a 2nd lookup with the original metric name. it can happen if there's a metric which is not histogram
// or summary, but ends with one of those _count/_sum suffixes
metadata, ok = mc.Metadata(metricName)
// still not found, this can happen when metric has no TYPE HINT
if !ok {
metadata.Metric = familyName
metadata.Type = textparse.MetricTypeUnknown
}
}
return &metricFamily{
name: familyName,
mtype: convToOCAMetricType(metadata.Type),
mc: mc,
droppedTimeseries: 0,
labelKeys: make(map[string]bool),
labelKeysOrdered: make([]string, 0),
metadata: &metadata,
groupOrders: make(map[string]int),
groups: make(map[string]*metricGroup),
}
}
func (mf *metricFamily) IsSameFamily(metricName string) bool {
// trim known suffix if necessary
familyName := normalizeMetricName(metricName)
return mf.name == familyName || familyName != metricName && mf.name == metricName
}
// updateLabelKeys is used to store all the label keys of a same metric family in observed order. since prometheus
// receiver removes any label with empty value before feeding it to an appender, in order to figure out all the labels
// from the same metric family we will need to keep track of what labels have ever been observed.
func (mf *metricFamily) updateLabelKeys(ls labels.Labels) {
for _, l := range ls {
if isUsefulLabel(mf.mtype, l.Name) {
if _, ok := mf.labelKeys[l.Name]; !ok {
mf.labelKeys[l.Name] = true
// use insertion sort to maintain order
i := sort.SearchStrings(mf.labelKeysOrdered, l.Name)
labelKeys := append(mf.labelKeysOrdered, "")
copy(labelKeys[i+1:], labelKeys[i:])
labelKeys[i] = l.Name
mf.labelKeysOrdered = labelKeys
}
}
}
}
func (mf *metricFamily) isCumulativeType() bool {
return mf.mtype == metricspb.MetricDescriptor_CUMULATIVE_DOUBLE ||
mf.mtype == metricspb.MetricDescriptor_CUMULATIVE_INT64 ||
mf.mtype == metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION ||
mf.mtype == metricspb.MetricDescriptor_SUMMARY
}
func (mf *metricFamily) getGroupKey(ls labels.Labels) string {
mf.updateLabelKeys(ls)
return dpgSignature(mf.labelKeysOrdered, ls)
}
// getGroups to return groups in insertion order
func (mf *metricFamily) getGroups() []*metricGroup {
groups := make([]*metricGroup, len(mf.groupOrders))
for k, v := range mf.groupOrders {
groups[v] = mf.groups[k]
}
return groups
}
func (mf *metricFamily) loadMetricGroupOrCreate(groupKey string, ls labels.Labels, ts int64) *metricGroup {
mg, ok := mf.groups[groupKey]
if !ok {
mg = &metricGroup{
family: mf,
ts: ts,
ls: ls,
complexValue: make([]*dataPoint, 0),
}
mf.groups[groupKey] = mg
// maintaining data insertion order is helpful to generate stable/reproducible metric output
mf.groupOrders[groupKey] = len(mf.groupOrders)
}
return mg
}
func (mf *metricFamily) getLabelKeys() []*metricspb.LabelKey {
lks := make([]*metricspb.LabelKey, len(mf.labelKeysOrdered))
for i, k := range mf.labelKeysOrdered {
lks[i] = &metricspb.LabelKey{Key: k}
}
return lks
}
func (mf *metricFamily) Add(metricName string, ls labels.Labels, t int64, v float64) error {
groupKey := mf.getGroupKey(ls)
mg := mf.loadMetricGroupOrCreate(groupKey, ls, t)
switch mf.mtype {
case metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION:
fallthrough
case metricspb.MetricDescriptor_SUMMARY:
switch {
case strings.HasSuffix(metricName, metricsSuffixSum):
// always use the timestamp from sum (count is ok too), because the startTs from quantiles won't be reliable
// in cases like remote server restart
mg.ts = t
mg.sum = v
mg.hasSum = true
case strings.HasSuffix(metricName, metricsSuffixCount):
mg.count = v
mg.hasCount = true
default:
boundary, err := getBoundary(mf.mtype, ls)
if err != nil {
mf.droppedTimeseries++
return err
}
mg.complexValue = append(mg.complexValue, &dataPoint{value: v, boundary: boundary})
}
default:
mg.value = v
}
return nil
}
func (mf *metricFamily) ToMetric() (*metricspb.Metric, int, int) {
timeseries := make([]*metricspb.TimeSeries, 0, len(mf.groups))
switch mf.mtype {
// not supported currently
// case metricspb.MetricDescriptor_GAUGE_DISTRIBUTION:
// return nil
case metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION:
for _, mg := range mf.getGroups() {
tss := mg.toDistributionTimeSeries(mf.labelKeysOrdered)
if tss != nil {
timeseries = append(timeseries, tss)
} else {
mf.droppedTimeseries++
}
}
case metricspb.MetricDescriptor_SUMMARY:
for _, mg := range mf.getGroups() {
tss := mg.toSummaryTimeSeries(mf.labelKeysOrdered)
if tss != nil {
timeseries = append(timeseries, tss)
} else {
mf.droppedTimeseries++
}
}
default:
for _, mg := range mf.getGroups() {
tss := mg.toDoubleValueTimeSeries(mf.labelKeysOrdered)
if tss != nil {
timeseries = append(timeseries, tss)
} else {
mf.droppedTimeseries++
}
}
}
// note: the total number of timeseries is the length of timeseries plus the number of dropped timeseries.
numTimeseries := len(timeseries)
if numTimeseries != 0 {
return &metricspb.Metric{
MetricDescriptor: &metricspb.MetricDescriptor{
Name: mf.name,
Description: mf.metadata.Help,
Unit: heuristicalMetricAndKnownUnits(mf.name, mf.metadata.Unit),
Type: mf.mtype,
LabelKeys: mf.getLabelKeys(),
},
Timeseries: timeseries,
},
numTimeseries + mf.droppedTimeseries,
mf.droppedTimeseries
}
return nil, mf.droppedTimeseries, mf.droppedTimeseries
}
type dataPoint struct {
value float64
boundary float64
}
// metricGroup, represents a single metric of a metric family. for example a histogram metric is usually represent by
// a couple data complexValue (buckets and count/sum), a group of a metric family always share a same set of tags. for
// simple types like counter and gauge, each data point is a group of itself
type metricGroup struct {
family *metricFamily
ts int64
ls labels.Labels
count float64
hasCount bool
sum float64
hasSum bool
value float64
complexValue []*dataPoint
}
func (mg *metricGroup) sortPoints() {
sort.Slice(mg.complexValue, func(i, j int) bool {
return mg.complexValue[i].boundary < mg.complexValue[j].boundary
})
}
func (mg *metricGroup) toDistributionTimeSeries(orderedLabelKeys []string) *metricspb.TimeSeries {
if !(mg.hasCount && mg.hasSum) || len(mg.complexValue) == 0 {
return nil
}
mg.sortPoints()
// for OCAgent Proto, the bounds won't include +inf
bounds := make([]float64, len(mg.complexValue)-1)
buckets := make([]*metricspb.DistributionValue_Bucket, len(mg.complexValue))
for i := 0; i < len(mg.complexValue); i++ {
if i != len(mg.complexValue)-1 {
// not need to add +inf as bound to oc proto
bounds[i] = mg.complexValue[i].boundary
}
adjustedCount := mg.complexValue[i].value
if i != 0 {
adjustedCount -= mg.complexValue[i-1].value
}
buckets[i] = &metricspb.DistributionValue_Bucket{Count: int64(adjustedCount)}
}
dv := &metricspb.DistributionValue{
BucketOptions: &metricspb.DistributionValue_BucketOptions{
Type: &metricspb.DistributionValue_BucketOptions_Explicit_{
Explicit: &metricspb.DistributionValue_BucketOptions_Explicit{
Bounds: bounds,
},
},
},
Count: int64(mg.count),
Sum: mg.sum,
Buckets: buckets,
// SumOfSquaredDeviation: // there's no way to compute this value from prometheus data
}
return &metricspb.TimeSeries{
StartTimestamp: timestampFromMs(mg.ts),
LabelValues: populateLabelValues(orderedLabelKeys, mg.ls),
Points: []*metricspb.Point{
{
Timestamp: timestampFromMs(mg.ts),
Value: &metricspb.Point_DistributionValue{DistributionValue: dv},
},
},
}
}
func (mg *metricGroup) toSummaryTimeSeries(orderedLabelKeys []string) *metricspb.TimeSeries {
// expecting count and sum to be provided, however, in the following two cases, they can be missed.
// 1. data is corrupted
// 2. ignored by startValue evaluation
if !(mg.hasCount && mg.hasSum) {
return nil
}
mg.sortPoints()
percentiles := make([]*metricspb.SummaryValue_Snapshot_ValueAtPercentile, len(mg.complexValue))
for i, p := range mg.complexValue {
percentiles[i] =
&metricspb.SummaryValue_Snapshot_ValueAtPercentile{Percentile: p.boundary * 100, Value: p.value}
}
// allow percentiles to be nil when no data provided from prometheus
var snapshot *metricspb.SummaryValue_Snapshot
if len(percentiles) != 0 {
snapshot = &metricspb.SummaryValue_Snapshot{
PercentileValues: percentiles,
}
}
// Based on the summary description from https://prometheus.io/docs/concepts/metric_types/#summary
// the quantiles are calculated over a sliding time window, however, the count is the total count of
// observations and the corresponding sum is a sum of all observed values, thus the sum and count used
// at the global level of the metricspb.SummaryValue
summaryValue := &metricspb.SummaryValue{
Sum: &wrapperspb.DoubleValue{Value: mg.sum},
Count: &wrapperspb.Int64Value{Value: int64(mg.count)},
Snapshot: snapshot,
}
return &metricspb.TimeSeries{
StartTimestamp: timestampFromMs(mg.ts),
LabelValues: populateLabelValues(orderedLabelKeys, mg.ls),
Points: []*metricspb.Point{
{Timestamp: timestampFromMs(mg.ts), Value: &metricspb.Point_SummaryValue{SummaryValue: summaryValue}},
},
}
}
func (mg *metricGroup) toDoubleValueTimeSeries(orderedLabelKeys []string) *metricspb.TimeSeries {
var startTs *timestamppb.Timestamp
// gauge/undefined types has no start time
if mg.family.isCumulativeType() {
startTs = timestampFromMs(mg.ts)
}
return &metricspb.TimeSeries{
StartTimestamp: startTs,
Points: []*metricspb.Point{{Timestamp: timestampFromMs(mg.ts), Value: &metricspb.Point_DoubleValue{DoubleValue: mg.value}}},
LabelValues: populateLabelValues(orderedLabelKeys, mg.ls),
}
}
func populateLabelValues(orderedKeys []string, ls labels.Labels) []*metricspb.LabelValue {
lvs := make([]*metricspb.LabelValue, len(orderedKeys))
lmap := ls.Map()
for i, k := range orderedKeys {
value := lmap[k]
lvs[i] = &metricspb.LabelValue{Value: value, HasValue: value != ""}
}
return lvs
}