Skip to content

Commit

Permalink
remove non-ascii characters from invalid metric names in error message (
Browse files Browse the repository at this point in the history
#7146)

* remove non-ascii characters from invalid metric names

Signed-off-by: Mauro Stettler <[email protected]>

* update changelog

Signed-off-by: Mauro Stettler <[email protected]>

* linter

Signed-off-by: Mauro Stettler <[email protected]>

---------

Signed-off-by: Mauro Stettler <[email protected]>
  • Loading branch information
replay authored Jan 17, 2024
1 parent 4fbea7c commit ca1e7bc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* [ENHANCEMENT] Query-frontend: add experimental support for sharding active series queries via `-query-frontend.shard-active-series-queries`. #6784
* [ENHANCEMENT] Distributor: set `-distributor.reusable-ingester-push-workers=2000` by default and mark feature as `advanced`. #7128
* [ENHANCEMENT] All: set `-server.grpc.num-workers=100` by default and mark feature as `advanced`. #7131
* [ENHANCEMENT] Distributor: invalid metric name error message gets cleaned up to not include non-ascii strings. #7146
* [BUGFIX] Ingester: don't ignore errors encountered while iterating through chunks or samples in response to a query request. #6451
* [BUGFIX] Fix issue where queries can fail or omit OOO samples if OOO head compaction occurs between creating a querier and reading chunks #6766
* [BUGFIX] Fix issue where concatenatingChunkIterator can obscure errors #6766
Expand Down
23 changes: 22 additions & 1 deletion pkg/distributor/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ package distributor
import (
"errors"
"fmt"
"strings"
"time"
"unicode"
"unicode/utf8"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -310,6 +312,25 @@ type labelValidationConfig interface {
MaxLabelValueLength(userID string) int
}

func removeNonASCIIChars(in string) (out string) {
foundNonASCII := false

out = strings.Map(func(r rune) rune {
if r <= unicode.MaxASCII {
return r
}

foundNonASCII = true
return -1
}, in)

if foundNonASCII {
out = out + " (non-ascii characters removed)"
}

return out
}

// validateLabels returns an err if the labels are invalid.
// The returned error may retain the provided series labels.
func validateLabels(m *sampleValidationMetrics, cfg labelValidationConfig, userID, group string, ls []mimirpb.LabelAdapter, skipLabelNameValidation bool) error {
Expand All @@ -321,7 +342,7 @@ func validateLabels(m *sampleValidationMetrics, cfg labelValidationConfig, userI

if !model.IsValidMetricName(model.LabelValue(unsafeMetricName)) {
m.invalidMetricName.WithLabelValues(userID, group).Inc()
return fmt.Errorf(invalidMetricNameMsgFormat, unsafeMetricName)
return fmt.Errorf(invalidMetricNameMsgFormat, removeNonASCIIChars(unsafeMetricName))
}

numLabelNames := len(ls)
Expand Down
7 changes: 6 additions & 1 deletion pkg/distributor/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ func TestValidateLabels(t *testing.T) {
false,
fmt.Errorf(invalidMetricNameMsgFormat, " "),
},
{
map[model.LabelName]model.LabelValue{model.MetricNameLabel: "metric_name_with_\xb0_invalid_utf8_\xb0"},
false,
fmt.Errorf(invalidMetricNameMsgFormat, "metric_name_with__invalid_utf8_ (non-ascii characters removed)"),
},
{
map[model.LabelName]model.LabelValue{model.MetricNameLabel: "valid", "foo ": "bar"},
false,
Expand Down Expand Up @@ -162,7 +167,7 @@ func TestValidateLabels(t *testing.T) {
cortex_discarded_samples_total{group="custom label",reason="label_name_too_long",user="testUser"} 1
cortex_discarded_samples_total{group="custom label",reason="label_value_too_long",user="testUser"} 1
cortex_discarded_samples_total{group="custom label",reason="max_label_names_per_series",user="testUser"} 1
cortex_discarded_samples_total{group="custom label",reason="metric_name_invalid",user="testUser"} 1
cortex_discarded_samples_total{group="custom label",reason="metric_name_invalid",user="testUser"} 2
cortex_discarded_samples_total{group="custom label",reason="missing_metric_name",user="testUser"} 1
cortex_discarded_samples_total{group="custom label",reason="random reason",user="different user"} 1
Expand Down

0 comments on commit ca1e7bc

Please sign in to comment.