...

Source file src/github.com/prometheus/common/model/labelset_string.go

Documentation: github.com/prometheus/common/model

     1  // Copyright 2024 The Prometheus Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  //go:build go1.21
    15  
    16  package model
    17  
    18  import (
    19  	"bytes"
    20  	"slices"
    21  	"strconv"
    22  )
    23  
    24  // String will look like `{foo="bar", more="less"}`. Names are sorted alphabetically.
    25  func (l LabelSet) String() string {
    26  	var lna [32]string // On stack to avoid memory allocation for sorting names.
    27  	labelNames := lna[:0]
    28  	for name := range l {
    29  		labelNames = append(labelNames, string(name))
    30  	}
    31  	slices.Sort(labelNames)
    32  	var bytea [1024]byte // On stack to avoid memory allocation while building the output.
    33  	b := bytes.NewBuffer(bytea[:0])
    34  	b.WriteByte('{')
    35  	for i, name := range labelNames {
    36  		if i > 0 {
    37  			b.WriteString(", ")
    38  		}
    39  		b.WriteString(name)
    40  		b.WriteByte('=')
    41  		b.Write(strconv.AppendQuote(b.AvailableBuffer(), string(l[LabelName(name)])))
    42  	}
    43  	b.WriteByte('}')
    44  	return b.String()
    45  }
    46  

View as plain text