master
go 45 lines 1.2 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package prometheus
4
5 import "github.com/prometheus/prometheus/model/labels"
6
7 func copyLabels(lbs []labels.Label) []labels.Label {
8 return append([]labels.Label(nil), lbs...)
9 }
10
11 // copyLabelsWithoutName returns a fresh copy of lbs with __name__ removed. In the
12 // common case __name__ sorts first (it precedes lowercase label names), so the
13 // remainder is contiguous and copied directly; otherwise a rare label that sorts
14 // before __name__ (e.g. "UUID") is skipped element by element.
15 func copyLabelsWithoutName(lbs labels.Labels) labels.Labels {
16 if len(lbs) > 0 && lbs[0].Name == labels.MetricName {
17 return copyLabels(lbs[1:])
18 }
19 out := make([]labels.Label, 0, len(lbs))
20 for _, lb := range lbs {
21 if lb.Name == labels.MetricName {
22 continue
23 }
24 out = append(out, lb)
25 }
26 return out
27 }
28
29 func removeLabel(lbs labels.Labels, name string) (labels.Labels, string, bool) {
30 for i, v := range lbs {
31 if v.Name == name {
32 return append(lbs[:i], lbs[i+1:]...), v.Value, true
33 }
34 }
35 return lbs, "", false
36 }
37
38 func metricNameValue(lbs labels.Labels) (string, bool) {
39 for _, v := range lbs {
40 if v.Name == labels.MetricName {
41 return v.Value, true
42 }
43 }
44 return "", false
45 }