Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions expfmt/openmetrics_2_0_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`,
},
}
Expand Down
2 changes: 1 addition & 1 deletion expfmt/openmetrics_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions expfmt/openmetrics_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`,
},
}
Expand Down
21 changes: 18 additions & 3 deletions expfmt/text_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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('"')
Expand Down
18 changes: 18 additions & 0 deletions expfmt/text_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`,
},
}
Expand Down
Loading