Skip to content

Commit

Permalink
Improve thresholds.Validate and ParseMetricName error messages clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiade committed May 5, 2022
1 parent 2546bd0 commit 4caf161
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions metrics/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,22 +147,25 @@ func ParseMetricName(name string) (string, []string, error) {
// its counterpart, the expression is malformed.
if (containsOpeningToken && !containsClosingToken) ||
(!containsOpeningToken && containsClosingToken) {
return "", nil, fmt.Errorf("%w; reason: unmatched opening/close curly brace", ErrMetricNameParsing)
return "", nil, fmt.Errorf(
"%w, metric %q has unmatched opening/close curly brace",
ErrMetricNameParsing, name,
)
}

// If the closing brace token appears before the opening one,
// the expression is malformed
if closingTokenPos < openingTokenPos {
return "", nil, fmt.Errorf("%w; reason: closing curly brace appears before opening one", ErrMetricNameParsing)
return "", nil, fmt.Errorf("%w, metric %q closing curly brace appears before opening one", ErrMetricNameParsing, name)
}

// If the last character is not a closing brace token,
// the expression is malformed.
if closingTokenPos != (len(name) - 1) {
err := fmt.Errorf(
"%w; reason: missing closing curly brace in last position"+
"of the threshold expression",
"%w, metric %q lacks a closing curly brace in its last position",
ErrMetricNameParsing,
name,
)
return "", nil, err
}
Expand All @@ -177,7 +180,7 @@ func ParseMetricName(name string) (string, []string, error) {
keyValue := strings.SplitN(t, ":", 2)

if len(keyValue) != 2 || keyValue[1] == "" {
return "", nil, fmt.Errorf("%w; reason: malformed tag expression %q", ErrMetricNameParsing, t)
return "", nil, fmt.Errorf("%w, metric %q tag expression is malformed", ErrMetricNameParsing, t)
}

tags[i] = strings.TrimSpace(t)
Expand Down
2 changes: 1 addition & 1 deletion metrics/thresholds.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ var ErrInvalidThreshold = errors.New("invalid threshold")
func (ts *Thresholds) Validate(metricName string, r *Registry) error {
parsedMetricName, _, err := ParseMetricName(metricName)
if err != nil {
err := fmt.Errorf("unable to validate threshold expressions: %w", err)
err := fmt.Errorf("unable to validate threshold expressions; reason: %w", err)
return errext.WithExitCodeIfNone(err, exitcodes.InvalidConfig)
}

Expand Down

0 comments on commit 4caf161

Please sign in to comment.