| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package metrix |
| 4 | |
| 5 | // compileLabelSet validates and canonicalizes labels into a reusable immutable handle. |
| 6 | func (c *storeCore) compileLabelSet(labels ...Label) LabelSet { |
| 7 | return compileLabelSetForOwner(c, labels...) |
| 8 | } |
| 9 | |
| 10 | func compileLabelSetForOwner(owner meterBackend, labels ...Label) LabelSet { |
| 11 | if len(labels) == 0 { |
| 12 | return LabelSet{set: &compiledLabelSet{owner: owner}} |
| 13 | } |
| 14 | |
| 15 | m := make(map[string]string, len(labels)) |
| 16 | for _, l := range labels { |
| 17 | if l.Key == "" { |
| 18 | panic(errInvalidLabelKey) |
| 19 | } |
| 20 | if _, ok := m[l.Key]; ok { |
| 21 | panic(errDuplicateLabelKey) |
| 22 | } |
| 23 | m[l.Key] = l.Value |
| 24 | } |
| 25 | |
| 26 | items, key, err := canonicalizeLabels(m) |
| 27 | if err != nil { |
| 28 | panic(err) |
| 29 | } |
| 30 | |
| 31 | return LabelSet{set: &compiledLabelSet{owner: owner, items: items, key: key}} |
| 32 | } |