| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 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 | package model |
| 15 | |
| 16 | import ( |
| 17 | "bytes" |
| 18 | "slices" |
| 19 | "strconv" |
| 20 | ) |
| 21 | |
| 22 | // String will look like `{foo="bar", more="less"}`. Names are sorted alphabetically. |
| 23 | func (l LabelSet) String() string { |
| 24 | var lna [32]string // On stack to avoid memory allocation for sorting names. |
| 25 | labelNames := lna[:0] |
| 26 | for name := range l { |
| 27 | labelNames = append(labelNames, string(name)) |
| 28 | } |
| 29 | slices.Sort(labelNames) |
| 30 | var bytea [1024]byte // On stack to avoid memory allocation while building the output. |
| 31 | b := bytes.NewBuffer(bytea[:0]) |
| 32 | b.WriteByte('{') |
| 33 | for i, name := range labelNames { |
| 34 | if i > 0 { |
| 35 | b.WriteString(", ") |
| 36 | } |
| 37 | b.WriteString(name) |
| 38 | b.WriteByte('=') |
| 39 | b.Write(strconv.AppendQuote(b.AvailableBuffer(), string(l[LabelName(name)]))) |
| 40 | } |
| 41 | b.WriteByte('}') |
| 42 | return b.String() |
| 43 | } |