fix(serializers.prometheus): Use legacy validation for metric name (#16557)

This commit is contained in:
Sven Rebhan 2025-03-04 16:20:15 +01:00 committed by GitHub
parent 3b83f6adc8
commit 0551f22bd3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 5 additions and 5 deletions

View File

@ -88,7 +88,7 @@ func sanitize(name string, table Table) (string, bool) {
// not, it attempts to replaces invalid runes with an underscore to create a // not, it attempts to replaces invalid runes with an underscore to create a
// valid name. // valid name.
func SanitizeMetricName(name string) (string, bool) { func SanitizeMetricName(name string) (string, bool) {
if model.IsValidMetricName(model.LabelValue(name)) { if model.IsValidLegacyMetricName(name) {
return name, true return name, true
} }
return sanitize(name, MetricNameTable) return sanitize(name, MetricNameTable)

View File

@ -46,10 +46,10 @@ func (s *Serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) {
var metrickey MetricKey var metrickey MetricKey
var promts prompb.TimeSeries var promts prompb.TimeSeries
for _, field := range metric.FieldList() { for _, field := range metric.FieldList() {
metricName := prometheus.MetricName(metric.Name(), field.Key, metric.Type()) rawName := prometheus.MetricName(metric.Name(), field.Key, metric.Type())
metricName, ok := prometheus.SanitizeMetricName(metricName) metricName, ok := prometheus.SanitizeMetricName(rawName)
if !ok { if !ok {
traceAndKeepErr("failed to parse metric name %q", metricName) traceAndKeepErr("failed to parse metric name %q", rawName)
continue continue
} }

View File

@ -220,7 +220,7 @@ func TestRemoteWriteSerializeNegative(t *testing.T) {
m := testutil.MustMetric("@@!!", nil, map[string]interface{}{"!!": "@@"}, time.Unix(0, 0)) m := testutil.MustMetric("@@!!", nil, map[string]interface{}{"!!": "@@"}, time.Unix(0, 0))
_, err := s.Serialize(m) _, err := s.Serialize(m)
assert("failed to parse \"@@!!_!!\"", err) assert("failed to parse metric name \"@@!!_!!\"", err)
m = testutil.MustMetric("prometheus", nil, m = testutil.MustMetric("prometheus", nil,
map[string]interface{}{ map[string]interface{}{