Control allocation and copy of labelNamesValues type

Co-authored-by: Kevin Pollet <pollet.kevin@gmail.com>
This commit is contained in:
Romain 2022-08-11 10:58:09 +02:00 committed by GitHub
parent 45453b20fa
commit 4755bb2f33
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -471,7 +471,6 @@ func newCounterFrom(opts stdprometheus.CounterOpts, labelNames []string) *counte
c := &counter{ c := &counter{
name: opts.Name, name: opts.Name,
cv: cv, cv: cv,
labelNamesValues: make([]string, 0, 16),
} }
if len(labelNames) == 0 { if len(labelNames) == 0 {
c.collector = cv.WithLabelValues() c.collector = cv.WithLabelValues()
@ -510,7 +509,6 @@ func newGaugeFrom(opts stdprometheus.GaugeOpts, labelNames []string) *gauge {
g := &gauge{ g := &gauge{
name: opts.Name, name: opts.Name,
gv: gv, gv: gv,
labelNamesValues: make([]string, 0, 16),
} }
if len(labelNames) == 0 { if len(labelNames) == 0 {
@ -554,7 +552,6 @@ func newHistogramFrom(opts stdprometheus.HistogramOpts, labelNames []string) *hi
return &histogram{ return &histogram{
name: opts.Name, name: opts.Name,
hv: hv, hv: hv,
labelNamesValues: make([]string, 0, 16),
} }
} }
@ -593,7 +590,12 @@ func (lvs labelNamesValues) With(labelValues ...string) labelNamesValues {
if len(labelValues)%2 != 0 { if len(labelValues)%2 != 0 {
labelValues = append(labelValues, "unknown") labelValues = append(labelValues, "unknown")
} }
return append(lvs, labelValues...)
labels := make([]string, len(lvs)+len(labelValues))
n := copy(labels, lvs)
copy(labels[n:], labelValues)
return labels
} }
// ToLabels is a convenience method to convert a labelNamesValues // ToLabels is a convenience method to convert a labelNamesValues