refactor (package model): move from github.com/pkg/errors to 'errors' and 'fmt' packages (#10747)

Signed-off-by: Matthieu MOREL <mmorel-35@users.noreply.github.com>
Co-Authored-By: Julien Pivotto <roidelapluie@gmail.com>

Co-authored-by: Julien Pivotto <roidelapluie@gmail.com>
This commit is contained in:
Matthieu MOREL 2022-06-27 21:29:19 +02:00 committed by GitHub
parent 6375417324
commit c2b4de3611
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 159 additions and 90 deletions

View file

@ -15,11 +15,10 @@ package labels
import ( import (
"bufio" "bufio"
"fmt"
"os" "os"
"sort" "sort"
"strings" "strings"
"github.com/pkg/errors"
) )
// Slice is a sortable slice of label sets. // Slice is a sortable slice of label sets.
@ -81,7 +80,7 @@ func ReadLabels(fn string, n int) ([]Labels, error) {
} }
if i != n { if i != n {
return mets, errors.Errorf("requested %d metrics but found %d", n, i) return mets, fmt.Errorf("requested %d metrics but found %d", n, i)
} }
return mets, nil return mets, nil
} }

View file

@ -19,7 +19,6 @@ import (
"strings" "strings"
"github.com/grafana/regexp" "github.com/grafana/regexp"
"github.com/pkg/errors"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/model/labels"
@ -71,7 +70,7 @@ func (a *Action) UnmarshalYAML(unmarshal func(interface{}) error) error {
*a = act *a = act
return nil return nil
} }
return errors.Errorf("unknown relabel action %q", s) return fmt.Errorf("unknown relabel action %q", s)
} }
// Config is the configuration for relabeling of target label sets. // Config is the configuration for relabeling of target label sets.
@ -105,25 +104,25 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
c.Regex = MustNewRegexp("") c.Regex = MustNewRegexp("")
} }
if c.Action == "" { if c.Action == "" {
return errors.Errorf("relabel action cannot be empty") return fmt.Errorf("relabel action cannot be empty")
} }
if c.Modulus == 0 && c.Action == HashMod { if c.Modulus == 0 && c.Action == HashMod {
return errors.Errorf("relabel configuration for hashmod requires non-zero modulus") return fmt.Errorf("relabel configuration for hashmod requires non-zero modulus")
} }
if (c.Action == Replace || c.Action == HashMod || c.Action == Lowercase || c.Action == Uppercase) && c.TargetLabel == "" { if (c.Action == Replace || c.Action == HashMod || c.Action == Lowercase || c.Action == Uppercase) && c.TargetLabel == "" {
return errors.Errorf("relabel configuration for %s action requires 'target_label' value", c.Action) return fmt.Errorf("relabel configuration for %s action requires 'target_label' value", c.Action)
} }
if (c.Action == Replace || c.Action == Lowercase || c.Action == Uppercase) && !relabelTarget.MatchString(c.TargetLabel) { if (c.Action == Replace || c.Action == Lowercase || c.Action == Uppercase) && !relabelTarget.MatchString(c.TargetLabel) {
return errors.Errorf("%q is invalid 'target_label' for %s action", c.TargetLabel, c.Action) return fmt.Errorf("%q is invalid 'target_label' for %s action", c.TargetLabel, c.Action)
} }
if (c.Action == Lowercase || c.Action == Uppercase) && c.Replacement != DefaultRelabelConfig.Replacement { if (c.Action == Lowercase || c.Action == Uppercase) && c.Replacement != DefaultRelabelConfig.Replacement {
return errors.Errorf("'replacement' can not be set for %s action", c.Action) return fmt.Errorf("'replacement' can not be set for %s action", c.Action)
} }
if c.Action == LabelMap && !relabelTarget.MatchString(c.Replacement) { if c.Action == LabelMap && !relabelTarget.MatchString(c.Replacement) {
return errors.Errorf("%q is invalid 'replacement' for %s action", c.Replacement, c.Action) return fmt.Errorf("%q is invalid 'replacement' for %s action", c.Replacement, c.Action)
} }
if c.Action == HashMod && !model.LabelName(c.TargetLabel).IsValid() { if c.Action == HashMod && !model.LabelName(c.TargetLabel).IsValid() {
return errors.Errorf("%q is invalid 'target_label' for %s action", c.TargetLabel, c.Action) return fmt.Errorf("%q is invalid 'target_label' for %s action", c.TargetLabel, c.Action)
} }
if c.Action == LabelDrop || c.Action == LabelKeep { if c.Action == LabelDrop || c.Action == LabelKeep {
@ -132,7 +131,7 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
c.Modulus != DefaultRelabelConfig.Modulus || c.Modulus != DefaultRelabelConfig.Modulus ||
c.Separator != DefaultRelabelConfig.Separator || c.Separator != DefaultRelabelConfig.Separator ||
c.Replacement != DefaultRelabelConfig.Replacement { c.Replacement != DefaultRelabelConfig.Replacement {
return errors.Errorf("%s action requires only 'regex', and no other fields", c.Action) return fmt.Errorf("%s action requires only 'regex', and no other fields", c.Action)
} }
} }
@ -265,7 +264,7 @@ func relabel(lset labels.Labels, cfg *Config) labels.Labels {
} }
} }
default: default:
panic(errors.Errorf("relabel: unknown relabel action type %q", cfg.Action)) panic(fmt.Errorf("relabel: unknown relabel action type %q", cfg.Action))
} }
return lb.Labels() return lb.Labels()

View file

@ -16,12 +16,13 @@ package rulefmt
import ( import (
"bytes" "bytes"
"context" "context"
"errors"
"fmt"
"io" "io"
"os" "os"
"strings" "strings"
"time" "time"
"github.com/pkg/errors"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
yaml "gopkg.in/yaml.v3" yaml "gopkg.in/yaml.v3"
@ -40,12 +41,16 @@ type Error struct {
// Error prints the error message in a formatted string. // Error prints the error message in a formatted string.
func (err *Error) Error() string { func (err *Error) Error() string {
if err.Err.nodeAlt != nil { if err.Err.err == nil {
return errors.Wrapf(err.Err.err, "%d:%d: %d:%d: group %q, rule %d, %q", err.Err.node.Line, err.Err.node.Column, err.Err.nodeAlt.Line, err.Err.nodeAlt.Column, err.Group, err.Rule, err.RuleName).Error() return ""
} else if err.Err.node != nil {
return errors.Wrapf(err.Err.err, "%d:%d: group %q, rule %d, %q", err.Err.node.Line, err.Err.node.Column, err.Group, err.Rule, err.RuleName).Error()
} }
return errors.Wrapf(err.Err.err, "group %q, rule %d, %q", err.Group, err.Rule, err.RuleName).Error() if err.Err.nodeAlt != nil {
return fmt.Sprintf("%d:%d: %d:%d: group %q, rule %d, %q: %v", err.Err.node.Line, err.Err.node.Column, err.Err.nodeAlt.Line, err.Err.nodeAlt.Column, err.Group, err.Rule, err.RuleName, err.Err.err)
}
if err.Err.node != nil {
return fmt.Sprintf("%d:%d: group %q, rule %d, %q: %v", err.Err.node.Line, err.Err.node.Column, err.Group, err.Rule, err.RuleName, err.Err.err)
}
return fmt.Sprintf("group %q, rule %d, %q: %v", err.Group, err.Rule, err.RuleName, err.Err.err)
} }
// WrappedError wraps error with the yaml node which can be used to represent // WrappedError wraps error with the yaml node which can be used to represent
@ -58,10 +63,14 @@ type WrappedError struct {
// Error prints the error message in a formatted string. // Error prints the error message in a formatted string.
func (we *WrappedError) Error() string { func (we *WrappedError) Error() string {
if we.err == nil {
return ""
}
if we.nodeAlt != nil { if we.nodeAlt != nil {
return errors.Wrapf(we.err, "%d:%d: %d:%d", we.node.Line, we.node.Column, we.nodeAlt.Line, we.nodeAlt.Column).Error() return fmt.Sprintf("%d:%d: %d:%d: %v", we.node.Line, we.node.Column, we.nodeAlt.Line, we.nodeAlt.Column, we.err)
} else if we.node != nil { }
return errors.Wrapf(we.err, "%d:%d", we.node.Line, we.node.Column).Error() if we.node != nil {
return fmt.Sprintf("%d:%d: %v", we.node.Line, we.node.Column, we.err)
} }
return we.err.Error() return we.err.Error()
} }
@ -81,13 +90,13 @@ func (g *RuleGroups) Validate(node ruleGroups) (errs []error) {
for j, g := range g.Groups { for j, g := range g.Groups {
if g.Name == "" { if g.Name == "" {
errs = append(errs, errors.Errorf("%d:%d: Groupname must not be empty", node.Groups[j].Line, node.Groups[j].Column)) errs = append(errs, fmt.Errorf("%d:%d: Groupname must not be empty", node.Groups[j].Line, node.Groups[j].Column))
} }
if _, ok := set[g.Name]; ok { if _, ok := set[g.Name]; ok {
errs = append( errs = append(
errs, errs,
errors.Errorf("%d:%d: groupname: \"%s\" is repeated in the same file", node.Groups[j].Line, node.Groups[j].Column, g.Name), fmt.Errorf("%d:%d: groupname: \"%s\" is repeated in the same file", node.Groups[j].Line, node.Groups[j].Column, g.Name),
) )
} }
@ -146,7 +155,7 @@ type RuleNode struct {
func (r *RuleNode) Validate() (nodes []WrappedError) { func (r *RuleNode) Validate() (nodes []WrappedError) {
if r.Record.Value != "" && r.Alert.Value != "" { if r.Record.Value != "" && r.Alert.Value != "" {
nodes = append(nodes, WrappedError{ nodes = append(nodes, WrappedError{
err: errors.Errorf("only one of 'record' and 'alert' must be set"), err: fmt.Errorf("only one of 'record' and 'alert' must be set"),
node: &r.Record, node: &r.Record,
nodeAlt: &r.Alert, nodeAlt: &r.Alert,
}) })
@ -154,12 +163,12 @@ func (r *RuleNode) Validate() (nodes []WrappedError) {
if r.Record.Value == "" && r.Alert.Value == "" { if r.Record.Value == "" && r.Alert.Value == "" {
if r.Record.Value == "0" { if r.Record.Value == "0" {
nodes = append(nodes, WrappedError{ nodes = append(nodes, WrappedError{
err: errors.Errorf("one of 'record' or 'alert' must be set"), err: fmt.Errorf("one of 'record' or 'alert' must be set"),
node: &r.Alert, node: &r.Alert,
}) })
} else { } else {
nodes = append(nodes, WrappedError{ nodes = append(nodes, WrappedError{
err: errors.Errorf("one of 'record' or 'alert' must be set"), err: fmt.Errorf("one of 'record' or 'alert' must be set"),
node: &r.Record, node: &r.Record,
}) })
} }
@ -167,31 +176,31 @@ func (r *RuleNode) Validate() (nodes []WrappedError) {
if r.Expr.Value == "" { if r.Expr.Value == "" {
nodes = append(nodes, WrappedError{ nodes = append(nodes, WrappedError{
err: errors.Errorf("field 'expr' must be set in rule"), err: fmt.Errorf("field 'expr' must be set in rule"),
node: &r.Expr, node: &r.Expr,
}) })
} else if _, err := parser.ParseExpr(r.Expr.Value); err != nil { } else if _, err := parser.ParseExpr(r.Expr.Value); err != nil {
nodes = append(nodes, WrappedError{ nodes = append(nodes, WrappedError{
err: errors.Wrapf(err, "could not parse expression"), err: fmt.Errorf("could not parse expression: %w", err),
node: &r.Expr, node: &r.Expr,
}) })
} }
if r.Record.Value != "" { if r.Record.Value != "" {
if len(r.Annotations) > 0 { if len(r.Annotations) > 0 {
nodes = append(nodes, WrappedError{ nodes = append(nodes, WrappedError{
err: errors.Errorf("invalid field 'annotations' in recording rule"), err: fmt.Errorf("invalid field 'annotations' in recording rule"),
node: &r.Record, node: &r.Record,
}) })
} }
if r.For != 0 { if r.For != 0 {
nodes = append(nodes, WrappedError{ nodes = append(nodes, WrappedError{
err: errors.Errorf("invalid field 'for' in recording rule"), err: fmt.Errorf("invalid field 'for' in recording rule"),
node: &r.Record, node: &r.Record,
}) })
} }
if !model.IsValidMetricName(model.LabelValue(r.Record.Value)) { if !model.IsValidMetricName(model.LabelValue(r.Record.Value)) {
nodes = append(nodes, WrappedError{ nodes = append(nodes, WrappedError{
err: errors.Errorf("invalid recording rule name: %s", r.Record.Value), err: fmt.Errorf("invalid recording rule name: %s", r.Record.Value),
node: &r.Record, node: &r.Record,
}) })
} }
@ -200,13 +209,13 @@ func (r *RuleNode) Validate() (nodes []WrappedError) {
for k, v := range r.Labels { for k, v := range r.Labels {
if !model.LabelName(k).IsValid() || k == model.MetricNameLabel { if !model.LabelName(k).IsValid() || k == model.MetricNameLabel {
nodes = append(nodes, WrappedError{ nodes = append(nodes, WrappedError{
err: errors.Errorf("invalid label name: %s", k), err: fmt.Errorf("invalid label name: %s", k),
}) })
} }
if !model.LabelValue(v).IsValid() { if !model.LabelValue(v).IsValid() {
nodes = append(nodes, WrappedError{ nodes = append(nodes, WrappedError{
err: errors.Errorf("invalid label value: %s", v), err: fmt.Errorf("invalid label value: %s", v),
}) })
} }
} }
@ -214,7 +223,7 @@ func (r *RuleNode) Validate() (nodes []WrappedError) {
for k := range r.Annotations { for k := range r.Annotations {
if !model.LabelName(k).IsValid() { if !model.LabelName(k).IsValid() {
nodes = append(nodes, WrappedError{ nodes = append(nodes, WrappedError{
err: errors.Errorf("invalid annotation name: %s", k), err: fmt.Errorf("invalid annotation name: %s", k),
}) })
} }
} }
@ -260,7 +269,7 @@ func testTemplateParsing(rl *RuleNode) (errs []error) {
for k, val := range rl.Labels { for k, val := range rl.Labels {
err := parseTest(val) err := parseTest(val)
if err != nil { if err != nil {
errs = append(errs, errors.Wrapf(err, "label %q", k)) errs = append(errs, fmt.Errorf("label %q: %w", k, err))
} }
} }
@ -268,7 +277,7 @@ func testTemplateParsing(rl *RuleNode) (errs []error) {
for k, val := range rl.Annotations { for k, val := range rl.Annotations {
err := parseTest(val) err := parseTest(val)
if err != nil { if err != nil {
errs = append(errs, errors.Wrapf(err, "annotation %q", k)) errs = append(errs, fmt.Errorf("annotation %q: %w", k, err))
} }
} }
@ -287,7 +296,7 @@ func Parse(content []byte) (*RuleGroups, []error) {
decoder.KnownFields(true) decoder.KnownFields(true)
err := decoder.Decode(&groups) err := decoder.Decode(&groups)
// Ignore io.EOF which happens with empty input. // Ignore io.EOF which happens with empty input.
if err != nil && err != io.EOF { if err != nil && !errors.Is(err, io.EOF) {
errs = append(errs, err) errs = append(errs, err)
} }
err = yaml.Unmarshal(content, &node) err = yaml.Unmarshal(content, &node)
@ -306,11 +315,11 @@ func Parse(content []byte) (*RuleGroups, []error) {
func ParseFile(file string) (*RuleGroups, []error) { func ParseFile(file string) (*RuleGroups, []error) {
b, err := os.ReadFile(file) b, err := os.ReadFile(file)
if err != nil { if err != nil {
return nil, []error{errors.Wrap(err, file)} return nil, []error{fmt.Errorf("%s: %w", file, err)}
} }
rgs, errs := Parse(b) rgs, errs := Parse(b)
for i := range errs { for i := range errs {
errs[i] = errors.Wrap(errs[i], file) errs[i] = fmt.Errorf("%s: %w", file, errs[i])
} }
return rgs, errs return rgs, errs
} }

View file

@ -182,8 +182,12 @@ groups:
` `
_, errs := Parse([]byte(group)) _, errs := Parse([]byte(group))
require.Len(t, errs, 2, "Expected two errors") require.Len(t, errs, 2, "Expected two errors")
err0 := errs[0].(*Error).Err.node var err00 *Error
err1 := errs[1].(*Error).Err.node require.True(t, errors.As(errs[0], &err00))
err0 := err00.Err.node
var err01 *Error
require.True(t, errors.As(errs[1], &err01))
err1 := err01.Err.node
require.NotEqual(t, err0, err1, "Error nodes should not be the same") require.NotEqual(t, err0, err1, "Error nodes should not be the same")
} }

View file

@ -58,8 +58,6 @@ yystate0:
goto yystart61 goto yystart61
} }
goto yystate0 // silence unused label error
goto yystate1 // silence unused label error
yystate1: yystate1:
c = l.next() c = l.next()
yystart1: yystart1:
@ -94,7 +92,6 @@ yystate4:
goto yystate4 goto yystate4
} }
goto yystate5 // silence unused label error
yystate5: yystate5:
c = l.next() c = l.next()
yystart5: yystart5:
@ -262,7 +259,6 @@ yystate24:
c = l.next() c = l.next()
goto yyrule4 goto yyrule4
goto yystate25 // silence unused label error
yystate25: yystate25:
c = l.next() c = l.next()
yystart25: yystart25:
@ -282,7 +278,6 @@ yystate26:
goto yystate26 goto yystate26
} }
goto yystate27 // silence unused label error
yystate27: yystate27:
c = l.next() c = l.next()
yystart27: yystart27:
@ -308,7 +303,6 @@ yystate29:
c = l.next() c = l.next()
goto yyrule7 goto yyrule7
goto yystate30 // silence unused label error
yystate30: yystate30:
c = l.next() c = l.next()
yystart30: yystart30:
@ -346,7 +340,6 @@ yystate34:
c = l.next() c = l.next()
goto yyrule11 goto yyrule11
goto yystate35 // silence unused label error
yystate35: yystate35:
c = l.next() c = l.next()
yystart35: yystart35:
@ -383,7 +376,6 @@ yystate38:
goto yystate36 goto yystate36
} }
goto yystate39 // silence unused label error
yystate39: yystate39:
c = l.next() c = l.next()
yystart39: yystart39:
@ -418,7 +410,6 @@ yystate42:
c = l.next() c = l.next()
goto yyrule9 goto yyrule9
goto yystate43 // silence unused label error
yystate43: yystate43:
c = l.next() c = l.next()
yystart43: yystart43:
@ -479,7 +470,6 @@ yystate49:
c = l.next() c = l.next()
goto yyrule18 goto yyrule18
goto yystate50 // silence unused label error
yystate50: yystate50:
c = l.next() c = l.next()
yystart50: yystart50:
@ -517,7 +507,6 @@ yystate54:
c = l.next() c = l.next()
goto yyrule20 goto yyrule20
goto yystate55 // silence unused label error
yystate55: yystate55:
c = l.next() c = l.next()
yystart55: yystart55:
@ -574,7 +563,6 @@ yystate60:
goto yystate58 goto yystate58
} }
goto yystate61 // silence unused label error
yystate61: yystate61:
c = l.next() c = l.next()
yystart61: yystart61:
@ -747,16 +735,58 @@ yyrule25: // {S}[^ \n]+
return tTimestamp return tTimestamp
} }
yyrule26: // \n yyrule26: // \n
{ if true { // avoid go vet determining the below panic will not be reached
l.state = sInit l.state = sInit
return tLinebreak return tLinebreak
goto yystate0 goto yystate0
} }
panic("unreachable") panic("unreachable")
goto yyabort // silence unused label error
yyabort: // no lexem recognized yyabort: // no lexem recognized
//
// silence unused label errors for build and satisfy go vet reachability analysis
//
{
if false {
goto yyabort
}
if false {
goto yystate0
}
if false {
goto yystate1
}
if false {
goto yystate5
}
if false {
goto yystate25
}
if false {
goto yystate27
}
if false {
goto yystate30
}
if false {
goto yystate35
}
if false {
goto yystate39
}
if false {
goto yystate43
}
if false {
goto yystate50
}
if false {
goto yystate55
}
if false {
goto yystate61
}
}
return tInvalid return tInvalid
} }

View file

@ -18,6 +18,7 @@ package textparse
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io" "io"
"math" "math"
@ -25,8 +26,6 @@ import (
"strings" "strings"
"unicode/utf8" "unicode/utf8"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/model/exemplar" "github.com/prometheus/prometheus/model/exemplar"
"github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/value" "github.com/prometheus/prometheus/model/value"
@ -276,7 +275,7 @@ func (p *OpenMetricsParser) Next() (Entry, error) {
case "unknown": case "unknown":
p.mtype = MetricTypeUnknown p.mtype = MetricTypeUnknown
default: default:
return EntryInvalid, errors.Errorf("invalid metric type %q", s) return EntryInvalid, fmt.Errorf("invalid metric type %q", s)
} }
case tHelp: case tHelp:
if !utf8.Valid(p.text) { if !utf8.Valid(p.text) {
@ -293,7 +292,7 @@ func (p *OpenMetricsParser) Next() (Entry, error) {
u := yoloString(p.text) u := yoloString(p.text)
if len(u) > 0 { if len(u) > 0 {
if !strings.HasSuffix(m, u) || len(m) < len(u)+1 || p.l.b[p.offsets[1]-len(u)-1] != '_' { if !strings.HasSuffix(m, u) || len(m) < len(u)+1 || p.l.b[p.offsets[1]-len(u)-1] != '_' {
return EntryInvalid, errors.Errorf("unit not a suffix of metric %q", m) return EntryInvalid, fmt.Errorf("unit not a suffix of metric %q", m)
} }
} }
return EntryUnit, nil return EntryUnit, nil
@ -353,7 +352,7 @@ func (p *OpenMetricsParser) Next() (Entry, error) {
return EntrySeries, nil return EntrySeries, nil
default: default:
err = errors.Errorf("%q %q is not a valid start token", t, string(p.l.cur())) err = fmt.Errorf("%q %q is not a valid start token", t, string(p.l.cur()))
} }
return EntryInvalid, err return EntryInvalid, err
} }

View file

@ -14,6 +14,7 @@
package textparse package textparse
import ( import (
"errors"
"io" "io"
"testing" "testing"
@ -223,7 +224,7 @@ foo_total 17.0 1520879607.789 # {xx="yy"} 5`
for { for {
et, err := p.Next() et, err := p.Next()
if err == io.EOF { if errors.Is(err, io.EOF) {
break break
} }
require.NoError(t, err) require.NoError(t, err)

View file

@ -27,6 +27,9 @@ const (
sLValue sLValue
sValue sValue
sTimestamp sTimestamp
sExemplar
sEValue
sETimestamp
) )
// Lex is called by the parser generated by "go tool yacc" to obtain each // Lex is called by the parser generated by "go tool yacc" to obtain each

View file

@ -1,4 +1,4 @@
// CAUTION: Generated file - DO NOT EDIT. // Code generated by golex. DO NOT EDIT.
// Copyright 2017 The Prometheus Authors // Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
@ -16,7 +16,7 @@
package textparse package textparse
import ( import (
"github.com/pkg/errors" "fmt"
) )
const ( const (
@ -47,7 +47,7 @@ yystate0:
switch yyt := l.state; yyt { switch yyt := l.state; yyt {
default: default:
panic(errors.Errorf(`invalid start condition %d`, yyt)) panic(fmt.Errorf(`invalid start condition %d`, yyt))
case 0: // start condition: INITIAL case 0: // start condition: INITIAL
goto yystart1 goto yystart1
case 1: // start condition: sComment case 1: // start condition: sComment
@ -66,8 +66,6 @@ yystate0:
goto yystart36 goto yystart36
} }
goto yystate0 // silence unused label error
goto yystate1 // silence unused label error
yystate1: yystate1:
c = l.next() c = l.next()
yystart1: yystart1:
@ -130,7 +128,6 @@ yystate7:
goto yystate7 goto yystate7
} }
goto yystate8 // silence unused label error
yystate8: yystate8:
c = l.next() c = l.next()
yystart8: yystart8:
@ -235,7 +232,6 @@ yystate18:
goto yystate18 goto yystate18
} }
goto yystate19 // silence unused label error
yystate19: yystate19:
c = l.next() c = l.next()
yystart19: yystart19:
@ -257,7 +253,6 @@ yystate20:
goto yystate20 goto yystate20
} }
goto yystate21 // silence unused label error
yystate21: yystate21:
c = l.next() c = l.next()
yystart21: yystart21:
@ -290,7 +285,6 @@ yystate23:
goto yystate22 goto yystate22
} }
goto yystate24 // silence unused label error
yystate24: yystate24:
c = l.next() c = l.next()
yystart24: yystart24:
@ -330,7 +324,6 @@ yystate28:
c = l.next() c = l.next()
goto yyrule13 goto yyrule13
goto yystate29 // silence unused label error
yystate29: yystate29:
c = l.next() c = l.next()
yystart29: yystart29:
@ -369,7 +362,6 @@ yystate32:
goto yystate30 goto yystate30
} }
goto yystate33 // silence unused label error
yystate33: yystate33:
c = l.next() c = l.next()
yystart33: yystart33:
@ -397,7 +389,6 @@ yystate35:
c = l.next() c = l.next()
goto yyrule11 goto yyrule11
goto yystate36 // silence unused label error
yystate36: yystate36:
c = l.next() c = l.next()
yystart36: yystart36:
@ -521,16 +512,50 @@ yyrule18: // {D}+
return tTimestamp return tTimestamp
} }
yyrule19: // \n yyrule19: // \n
{ if true { // avoid go vet determining the below panic will not be reached
l.state = sInit l.state = sInit
return tLinebreak return tLinebreak
goto yystate0 goto yystate0
} }
panic("unreachable") panic("unreachable")
goto yyabort // silence unused label error
yyabort: // no lexem recognized yyabort: // no lexem recognized
//
// silence unused label errors for build and satisfy go vet reachability analysis
//
{
if false {
goto yyabort
}
if false {
goto yystate0
}
if false {
goto yystate1
}
if false {
goto yystate8
}
if false {
goto yystate19
}
if false {
goto yystate21
}
if false {
goto yystate24
}
if false {
goto yystate29
}
if false {
goto yystate33
}
if false {
goto yystate36
}
}
// Workaround to gobble up comments that started with a HELP or TYPE // Workaround to gobble up comments that started with a HELP or TYPE
// prefix. We just consume all characters until we reach a newline. // prefix. We just consume all characters until we reach a newline.
// This saves us from adding disproportionate complexity to the parser. // This saves us from adding disproportionate complexity to the parser.

View file

@ -17,6 +17,7 @@
package textparse package textparse
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"math" "math"
@ -26,8 +27,6 @@ import (
"unicode/utf8" "unicode/utf8"
"unsafe" "unsafe"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/model/exemplar" "github.com/prometheus/prometheus/model/exemplar"
"github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/value" "github.com/prometheus/prometheus/model/value"
@ -252,7 +251,7 @@ func (p *PromParser) nextToken() token {
} }
func parseError(exp string, got token) error { func parseError(exp string, got token) error {
return errors.Errorf("%s, got %q", exp, got) return fmt.Errorf("%s, got %q", exp, got)
} }
// Next advances the parser to the next sample. It returns false if no // Next advances the parser to the next sample. It returns false if no
@ -301,11 +300,11 @@ func (p *PromParser) Next() (Entry, error) {
case "untyped": case "untyped":
p.mtype = MetricTypeUnknown p.mtype = MetricTypeUnknown
default: default:
return EntryInvalid, errors.Errorf("invalid metric type %q", s) return EntryInvalid, fmt.Errorf("invalid metric type %q", s)
} }
case tHelp: case tHelp:
if !utf8.Valid(p.text) { if !utf8.Valid(p.text) {
return EntryInvalid, errors.Errorf("help text is not a valid utf8 string") return EntryInvalid, fmt.Errorf("help text is not a valid utf8 string")
} }
} }
if t := p.nextToken(); t != tLinebreak { if t := p.nextToken(); t != tLinebreak {
@ -364,7 +363,7 @@ func (p *PromParser) Next() (Entry, error) {
return EntrySeries, nil return EntrySeries, nil
default: default:
err = errors.Errorf("%q is not a valid start token", t) err = fmt.Errorf("%q is not a valid start token", t)
} }
return EntryInvalid, err return EntryInvalid, err
} }
@ -388,7 +387,7 @@ func (p *PromParser) parseLVals() error {
return parseError("expected label value", t) return parseError("expected label value", t)
} }
if !utf8.Valid(p.l.buf()) { if !utf8.Valid(p.l.buf()) {
return errors.Errorf("invalid UTF-8 label value") return fmt.Errorf("invalid UTF-8 label value")
} }
// The promlexer ensures the value string is quoted. Strip first // The promlexer ensures the value string is quoted. Strip first

View file

@ -16,6 +16,7 @@ package textparse
import ( import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"errors"
"io" "io"
"os" "os"
"testing" "testing"
@ -176,7 +177,7 @@ testmetric{label="\"bar\""} 1`
for { for {
et, err := p.Next() et, err := p.Next()
if err == io.EOF { if errors.Is(err, io.EOF) {
break break
} }
require.NoError(t, err) require.NoError(t, err)
@ -378,7 +379,7 @@ func BenchmarkParse(b *testing.B) {
t, err := p.Next() t, err := p.Next()
switch t { switch t {
case EntryInvalid: case EntryInvalid:
if err == io.EOF { if errors.Is(err, io.EOF) {
break Outer break Outer
} }
b.Fatal(err) b.Fatal(err)
@ -406,7 +407,7 @@ func BenchmarkParse(b *testing.B) {
t, err := p.Next() t, err := p.Next()
switch t { switch t {
case EntryInvalid: case EntryInvalid:
if err == io.EOF { if errors.Is(err, io.EOF) {
break Outer break Outer
} }
b.Fatal(err) b.Fatal(err)
@ -439,7 +440,7 @@ func BenchmarkParse(b *testing.B) {
t, err := p.Next() t, err := p.Next()
switch t { switch t {
case EntryInvalid: case EntryInvalid:
if err == io.EOF { if errors.Is(err, io.EOF) {
break Outer break Outer
} }
b.Fatal(err) b.Fatal(err)