@@ -20,6 +20,24 @@ import (
20
20
"github.com/prometheus/common/model"
21
21
)
22
22
23
+ var labelsPool = & sync.Pool {
24
+ New : func () interface {} {
25
+ return make (Labels )
26
+ },
27
+ }
28
+
29
+ func getLabelsFromPool () Labels {
30
+ return labelsPool .Get ().(Labels )
31
+ }
32
+
33
+ func putLabelsToPool (labels Labels ) {
34
+ for k := range labels {
35
+ delete (labels , k )
36
+ }
37
+
38
+ labelsPool .Put (labels )
39
+ }
40
+
23
41
// MetricVec is a Collector to bundle metrics of the same name that differ in
24
42
// their label values. MetricVec is not used directly but as a building block
25
43
// for implementations of vectors of a given metric type, like GaugeVec,
@@ -93,6 +111,8 @@ func (m *MetricVec) DeleteLabelValues(lvs ...string) bool {
93
111
// there for pros and cons of the two methods.
94
112
func (m * MetricVec ) Delete (labels Labels ) bool {
95
113
labels = constrainLabels (m .desc , labels )
114
+ defer putLabelsToPool (labels )
115
+
96
116
h , err := m .hashLabels (labels )
97
117
if err != nil {
98
118
return false
@@ -109,6 +129,8 @@ func (m *MetricVec) Delete(labels Labels) bool {
109
129
// To match curried labels with DeletePartialMatch, it must be called on the base vector.
110
130
func (m * MetricVec ) DeletePartialMatch (labels Labels ) int {
111
131
labels = constrainLabels (m .desc , labels )
132
+ defer putLabelsToPool (labels )
133
+
112
134
return m .metricMap .deleteByLabels (labels , m .curry )
113
135
}
114
136
@@ -229,6 +251,8 @@ func (m *MetricVec) GetMetricWithLabelValues(lvs ...string) (Metric, error) {
229
251
// for example GaugeVec.
230
252
func (m * MetricVec ) GetMetricWith (labels Labels ) (Metric , error ) {
231
253
labels = constrainLabels (m .desc , labels )
254
+ defer putLabelsToPool (labels )
255
+
232
256
h , err := m .hashLabels (labels )
233
257
if err != nil {
234
258
return nil , err
@@ -647,15 +671,16 @@ func inlineLabelValues(lvs []string, curry []curriedLabelValue) []string {
647
671
}
648
672
649
673
func constrainLabels (desc * Desc , labels Labels ) Labels {
650
- constrainedValues := make ( Labels , len ( labels ) )
674
+ constrainedLabels := getLabelsFromPool ( )
651
675
for l , v := range labels {
652
676
if i , ok := indexOf (l , desc .variableLabels .labelNames ()); ok {
653
- constrainedValues [l ] = desc .variableLabels [i ].Constrain (v )
654
- continue
677
+ v = desc .variableLabels [i ].Constrain (v )
655
678
}
656
- constrainedValues [l ] = v
679
+
680
+ constrainedLabels [l ] = v
657
681
}
658
- return constrainedValues
682
+
683
+ return constrainedLabels
659
684
}
660
685
661
686
func constrainLabelValues (desc * Desc , lvs []string , curry []curriedLabelValue ) []string {
0 commit comments