-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlabel_namer.go
More file actions
109 lines (98 loc) · 4.14 KB
/
Copy pathlabel_namer.go
File metadata and controls
109 lines (98 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2025 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Provenance-includes-location: https://github.com/prometheus/prometheus/blob/93e991ef7ed19cc997a9360c8016cac3767b8057/storage/remote/otlptranslator/prometheus/normalize_label.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: Copyright The Prometheus Authors
// Provenance-includes-location: https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/95e8f8fdc2a9dc87230406c9a3cf02be4fd68bea/pkg/translator/prometheus/normalize_label.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: Copyright The OpenTelemetry Authors.
package otlptranslator
import (
"errors"
"fmt"
"strings"
"unicode"
)
// LabelNamer is a helper struct to build label names.
// It translates OpenTelemetry Protocol (OTLP) attribute names to Prometheus-compliant label names.
//
// Example usage:
//
// namer := LabelNamer{UTF8Allowed: false}
// result, err := namer.Build("http.method")
// if err != nil {
// // handle err
// }
// // result == "http_method"
type LabelNamer struct {
UTF8Allowed bool
// UnderscoreLabelSanitization, if true, enabled prepending 'key' to labels
// starting with '_'. Reserved labels starting with `__` are not modified.
//
// Deprecated: This will be removed in a future version of otlptranslator.
UnderscoreLabelSanitization bool
// PreserveMultipleUnderscores enables preserving of multiple
// consecutive underscores in label names when UTF8Allowed is false.
// This option is discouraged as it violates the OpenTelemetry to Prometheus
// specification https://github.com/open-telemetry/opentelemetry-specification/blob/v1.38.0/specification/compatibility/prometheus_and_openmetrics.md#otlp-metric-points-to-prometheus),
// but may be needed for compatibility with legacy systems that rely on the old behavior.
PreserveMultipleUnderscores bool
}
// Build normalizes the specified label to follow Prometheus label names standard.
//
// Translation rules:
// - Replaces invalid characters with underscores
// - Prefixes labels starting with a digit with "key_"
// - With the deprecated UnderscoreLabelSanitization option, prefixes labels starting with a single underscore with "key"
// - Preserves double underscore labels (reserved names)
// - If UTF8Allowed is true, returns label as-is
//
// Examples:
//
// namer := LabelNamer{UTF8Allowed: false}
// namer.Build("http.method") // "http_method"
// namer.Build("123invalid") // "key_123invalid"
// namer.Build("__reserved__") // "__reserved__" (preserved)
func (ln *LabelNamer) Build(label string) (string, error) {
if len(label) == 0 {
return "", errors.New("label name is empty")
}
if ln.UTF8Allowed {
if hasUnderscoresOnly(label) {
return "", fmt.Errorf("label name %q contains only underscores", label)
}
return label, nil
}
if canFastPathLabel(label, ln.PreserveMultipleUnderscores, ln.UnderscoreLabelSanitization) {
return label, nil
}
normalizedName := sanitizeLabelName(label, ln.PreserveMultipleUnderscores)
// If label starts with a number, prepend with "key_".
if unicode.IsDigit(rune(normalizedName[0])) {
normalizedName = "key_" + normalizedName
} else if ln.UnderscoreLabelSanitization && strings.HasPrefix(normalizedName, "_") && !strings.HasPrefix(normalizedName, "__") {
normalizedName = "key" + normalizedName
}
if hasUnderscoresOnly(normalizedName) {
return "", fmt.Errorf("normalization for label name %q resulted in invalid name %q", label, normalizedName)
}
return normalizedName, nil
}
func hasUnderscoresOnly(label string) bool {
for _, c := range label {
if c != '_' {
return false
}
}
return true
}