/
extract.go
66 lines (56 loc) · 1.99 KB
/
extract.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
package extract
import (
"fmt"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/cortexproject/cortex/pkg/ingester/client"
)
var (
errNoMetricNameLabel = fmt.Errorf("No metric name label")
)
// MetricNameFromLabelAdapters extracts the metric name from a list of LabelPairs.
func MetricNameFromLabelAdapters(labels []client.LabelAdapter) (string, error) {
for _, label := range labels {
if label.Name == model.MetricNameLabel {
// Force a string copy since LabelAdapter is often a pointer into
// a large gRPC buffer which we don't want to keep alive on the heap.
return string([]byte(label.Value)), nil
}
}
return "", errNoMetricNameLabel
}
// MetricNameFromMetric extract the metric name from a model.Metric
func MetricNameFromMetric(m model.Metric) (model.LabelValue, error) {
if value, found := m[model.MetricNameLabel]; found {
return value, nil
}
return "", fmt.Errorf("no MetricNameLabel for chunk")
}
// MetricNameMatcherFromMatchers extracts the metric name from a set of matchers
func MetricNameMatcherFromMatchers(matchers []*labels.Matcher) (*labels.Matcher, []*labels.Matcher, bool) {
// Handle the case where there is no metric name and all matchers have been
// filtered out e.g. {foo=""}.
if len(matchers) == 0 {
return nil, matchers, false
}
outMatchers := make([]*labels.Matcher, len(matchers)-1)
for i, matcher := range matchers {
if matcher.Name != model.MetricNameLabel {
continue
}
// Copy other matchers, excluding the found metric name matcher
copy(outMatchers, matchers[:i])
copy(outMatchers[i:], matchers[i+1:])
return matcher, outMatchers, true
}
// Return all matchers if none are metric name matchers
return nil, matchers, false
}
// MetricNameFromLabels extracts the metric name from a list of Prometheus Labels.
func MetricNameFromLabels(lbls labels.Labels) (metricName string, err error) {
metricName = lbls.Get(model.MetricNameLabel)
if metricName == "" {
err = errNoMetricNameLabel
}
return
}