From c86672afada62e8f387f6b6bfcd0d63637f7ff06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gy=C3=B6rgy=20Krajcsovits?= Date: Fri, 13 Nov 2026 09:10:38 +0100 Subject: [PATCH] expfmt: quote label names that contain colons MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Label names were checked against the legacy metric name rules, which allow ':'. Colons are not valid in legacy label names, so a label such as "a:b" was written unquoted as m{a:b="v"}, which the text and OpenMetrics parsers reject. Check label names against the legacy label name rules instead and quote them when needed. Signed-off-by: György Krajcsovits Co-Authored-By: Claude Opus 5.5 (1M context) --- expfmt/openmetrics_2_0_create_test.go | 18 ++++++++++++++++++ expfmt/openmetrics_create.go | 2 +- expfmt/openmetrics_create_test.go | 18 ++++++++++++++++++ expfmt/text_create.go | 21 ++++++++++++++++++--- expfmt/text_create_test.go | 18 ++++++++++++++++++ 5 files changed, 73 insertions(+), 4 deletions(-) diff --git a/expfmt/openmetrics_2_0_create_test.go b/expfmt/openmetrics_2_0_create_test.go index 4d1619756..406e458d8 100644 --- a/expfmt/openmetrics_2_0_create_test.go +++ b/expfmt/openmetrics_2_0_create_test.go @@ -1221,6 +1221,24 @@ temp_delta {count:5,sum:-10.5,quantile:[0.5:-2.5,0.99:1]} # UNIT acme_http_router_request_seconds seconds acme_http_router_request_seconds{path="/api/v1",method="GET"} {count:807283,sum:9036.32,quantile:[0.95:2,0.99:20]} acme_http_router_request_seconds{path="/api/v2",method="GET"} {count:34,sum:479.3,quantile:[0.95:2.5,0.99:2.9]} +`, + }, + { + name: "LabelNameWithColon", + in: &dto.MetricFamily{ + Name: proto.String("name:with:colons"), + Type: dto.MetricType_GAUGE.Enum(), + Metric: []*dto.Metric{ + { + Label: []*dto.LabelPair{ + {Name: proto.String("label:name"), Value: proto.String("val")}, + }, + Gauge: &dto.Gauge{Value: proto.Float64(1)}, + }, + }, + }, + out: `# TYPE name:with:colons gauge +name:with:colons{"label:name"="val"} 1.0 `, }, } diff --git a/expfmt/openmetrics_create.go b/expfmt/openmetrics_create.go index 0480e7af5..1af167f10 100644 --- a/expfmt/openmetrics_create.go +++ b/expfmt/openmetrics_create.go @@ -503,7 +503,7 @@ func writeOpenMetricsNameAndLabelPairs( if err != nil { return written, err } - n, err := writeName(w, lp.GetName()) + n, err := writeLabelName(w, lp.GetName()) written += n if err != nil { return written, err diff --git a/expfmt/openmetrics_create_test.go b/expfmt/openmetrics_create_test.go index 8419a698f..39fc40cc9 100644 --- a/expfmt/openmetrics_create_test.go +++ b/expfmt/openmetrics_create_test.go @@ -758,6 +758,24 @@ name_bucket{le="172.8"} 1524 name_bucket{le="+Inf"} 2693 name_sum 1.7560473e+06 name_count 2693 +`, + }, + // 18: Colons are valid in legacy metric names but not in legacy label names. + { + in: &dto.MetricFamily{ + Name: proto.String("name:with:colons"), + Type: dto.MetricType_GAUGE.Enum(), + Metric: []*dto.Metric{ + { + Label: []*dto.LabelPair{ + {Name: proto.String("label:name"), Value: proto.String("val")}, + }, + Gauge: &dto.Gauge{Value: proto.Float64(1)}, + }, + }, + }, + out: `# TYPE name:with:colons gauge +name:with:colons{"label:name"="val"} 1.0 `, }, } diff --git a/expfmt/text_create.go b/expfmt/text_create.go index f4074ae9a..39dbc3486 100644 --- a/expfmt/text_create.go +++ b/expfmt/text_create.go @@ -399,7 +399,7 @@ func writeNameAndLabelPairs( if err != nil { return written, err } - n, err := writeName(w, lp.GetName()) + n, err := writeLabelName(w, lp.GetName()) written += n if err != nil { return written, err @@ -507,12 +507,27 @@ func writeInt(w enhancedWriter, i int64) (int, error) { return written, err } -// writeName writes a string as-is if it complies with the legacy naming -// scheme, or escapes it in double quotes if not. +// writeName writes a metric name as-is if it complies with the legacy metric +// naming scheme, or escapes it in double quotes if not. func writeName(w enhancedWriter, name string) (int, error) { if model.LegacyValidation.IsValidMetricName(name) { return w.WriteString(name) } + return writeQuotedName(w, name) +} + +// writeLabelName writes a label name as-is if it complies with the legacy +// label naming scheme, or escapes it in double quotes if not. Unlike metric +// names, legacy label names must not contain colons. +func writeLabelName(w enhancedWriter, name string) (int, error) { + if model.LegacyValidation.IsValidLabelName(name) { + return w.WriteString(name) + } + return writeQuotedName(w, name) +} + +// writeQuotedName writes a name escaped in double quotes. +func writeQuotedName(w enhancedWriter, name string) (int, error) { var written int var err error err = w.WriteByte('"') diff --git a/expfmt/text_create_test.go b/expfmt/text_create_test.go index 82ac55d7a..56ad26898 100644 --- a/expfmt/text_create_test.go +++ b/expfmt/text_create_test.go @@ -563,6 +563,24 @@ name_bucket{le="172.8"} 1524 name_bucket{le="+Inf"} 2693.123 name_sum 1.7560473e+06 name_count 2693.123 +`, + }, + // 12: Colons are valid in legacy metric names but not in legacy label names. + { + in: &dto.MetricFamily{ + Name: proto.String("name:with:colons"), + Type: dto.MetricType_GAUGE.Enum(), + Metric: []*dto.Metric{ + { + Label: []*dto.LabelPair{ + {Name: proto.String("label:name"), Value: proto.String("val")}, + }, + Gauge: &dto.Gauge{Value: proto.Float64(1)}, + }, + }, + }, + out: `# TYPE name:with:colons gauge +name:with:colons{"label:name"="val"} 1 `, }, }