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

@ -469,9 +469,8 @@ func (d *dynamicConfig) hasServerURL(serviceName, serverURL string) bool {
func newCounterFrom(opts stdprometheus.CounterOpts, labelNames []string) *counter { func newCounterFrom(opts stdprometheus.CounterOpts, labelNames []string) *counter {
cv := stdprometheus.NewCounterVec(opts, labelNames) cv := stdprometheus.NewCounterVec(opts, labelNames)
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()
@ -508,9 +507,8 @@ func (c *counter) Describe(ch chan<- *stdprometheus.Desc) {
func newGaugeFrom(opts stdprometheus.GaugeOpts, labelNames []string) *gauge { func newGaugeFrom(opts stdprometheus.GaugeOpts, labelNames []string) *gauge {
gv := stdprometheus.NewGaugeVec(opts, labelNames) gv := stdprometheus.NewGaugeVec(opts, labelNames)
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 {
@ -552,9 +550,8 @@ func (g *gauge) Describe(ch chan<- *stdprometheus.Desc) {
func newHistogramFrom(opts stdprometheus.HistogramOpts, labelNames []string) *histogram { func newHistogramFrom(opts stdprometheus.HistogramOpts, labelNames []string) *histogram {
hv := stdprometheus.NewHistogramVec(opts, labelNames) hv := stdprometheus.NewHistogramVec(opts, labelNames)
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