Merge pull request #13091 from mmorel-35/errorlint/util

util: use Go standard errors package
This commit is contained in:
Julien Pivotto 2023-11-07 21:07:25 -06:00 committed by GitHub
commit cf01ec2119
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 31 additions and 24 deletions

View file

@ -37,12 +37,17 @@ issues:
- path: tsdb/ - path: tsdb/
linters: linters:
- errorlint - errorlint
- path: util/ - path: tsdb/
text: "import 'github.com/pkg/errors' is not allowed"
linters: linters:
- errorlint - depguard
- path: web/ - path: web/
linters: linters:
- errorlint - errorlint
- path: web/
text: "import 'github.com/pkg/errors' is not allowed"
linters:
- depguard
- linters: - linters:
- godot - godot
source: "^// ===" source: "^// ==="
@ -62,6 +67,8 @@ linters-settings:
desc: "Use corresponding 'os' or 'io' functions instead." desc: "Use corresponding 'os' or 'io' functions instead."
- pkg: "regexp" - pkg: "regexp"
desc: "Use github.com/grafana/regexp instead of regexp" desc: "Use github.com/grafana/regexp instead of regexp"
- pkg: "github.com/pkg/errors"
desc: "Use 'errors' or 'fmt' instead of github.com/pkg/errors"
errcheck: errcheck:
exclude-functions: exclude-functions:
# Don't flag lines such as "io.Copy(io.Discard, resp.Body)". # Don't flag lines such as "io.Copy(io.Discard, resp.Body)".

View file

@ -14,10 +14,10 @@
package ionos package ionos
import ( import (
"errors"
"time" "time"
"github.com/go-kit/log" "github.com/go-kit/log"
"github.com/pkg/errors"
"github.com/prometheus/common/config" "github.com/prometheus/common/config"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"

View file

@ -16,6 +16,7 @@ package textparse
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"errors"
"fmt" "fmt"
"io" "io"
"math" "math"
@ -24,7 +25,6 @@ import (
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types" "github.com/gogo/protobuf/types"
"github.com/pkg/errors"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/exemplar" "github.com/prometheus/prometheus/model/exemplar"
@ -396,10 +396,10 @@ func (p *ProtobufParser) Next() (Entry, error) {
// into metricBytes and validate only name, help, and type for now. // into metricBytes and validate only name, help, and type for now.
name := p.mf.GetName() name := p.mf.GetName()
if !model.IsValidMetricName(model.LabelValue(name)) { if !model.IsValidMetricName(model.LabelValue(name)) {
return EntryInvalid, errors.Errorf("invalid metric name: %s", name) return EntryInvalid, fmt.Errorf("invalid metric name: %s", name)
} }
if help := p.mf.GetHelp(); !utf8.ValidString(help) { if help := p.mf.GetHelp(); !utf8.ValidString(help) {
return EntryInvalid, errors.Errorf("invalid help for metric %q: %s", name, help) return EntryInvalid, fmt.Errorf("invalid help for metric %q: %s", name, help)
} }
switch p.mf.GetType() { switch p.mf.GetType() {
case dto.MetricType_COUNTER, case dto.MetricType_COUNTER,
@ -410,7 +410,7 @@ func (p *ProtobufParser) Next() (Entry, error) {
dto.MetricType_UNTYPED: dto.MetricType_UNTYPED:
// All good. // All good.
default: default:
return EntryInvalid, errors.Errorf("unknown metric type for metric %q: %s", name, p.mf.GetType()) return EntryInvalid, fmt.Errorf("unknown metric type for metric %q: %s", name, p.mf.GetType())
} }
p.metricBytes.Reset() p.metricBytes.Reset()
p.metricBytes.WriteString(name) p.metricBytes.WriteString(name)
@ -463,7 +463,7 @@ func (p *ProtobufParser) Next() (Entry, error) {
return EntryInvalid, err return EntryInvalid, err
} }
default: default:
return EntryInvalid, errors.Errorf("invalid protobuf parsing state: %d", p.state) return EntryInvalid, fmt.Errorf("invalid protobuf parsing state: %d", p.state)
} }
return p.state, nil return p.state, nil
} }
@ -476,13 +476,13 @@ func (p *ProtobufParser) updateMetricBytes() error {
b.WriteByte(model.SeparatorByte) b.WriteByte(model.SeparatorByte)
n := lp.GetName() n := lp.GetName()
if !model.LabelName(n).IsValid() { if !model.LabelName(n).IsValid() {
return errors.Errorf("invalid label name: %s", n) return fmt.Errorf("invalid label name: %s", n)
} }
b.WriteString(n) b.WriteString(n)
b.WriteByte(model.SeparatorByte) b.WriteByte(model.SeparatorByte)
v := lp.GetValue() v := lp.GetValue()
if !utf8.ValidString(v) { if !utf8.ValidString(v) {
return errors.Errorf("invalid label value: %s", v) return fmt.Errorf("invalid label value: %s", v)
} }
b.WriteString(v) b.WriteString(v)
} }
@ -557,7 +557,7 @@ func readDelimited(b []byte, mf *dto.MetricFamily) (n int, err error) {
} }
totalLength := varIntLength + int(messageLength) totalLength := varIntLength + int(messageLength)
if totalLength > len(b) { if totalLength > len(b) {
return 0, errors.Errorf("protobufparse: insufficient length of buffer, expected at least %d bytes, got %d bytes", totalLength, len(b)) return 0, fmt.Errorf("protobufparse: insufficient length of buffer, expected at least %d bytes, got %d bytes", totalLength, len(b))
} }
mf.Reset() mf.Reset()
return totalLength, mf.Unmarshal(b[varIntLength:totalLength]) return totalLength, mf.Unmarshal(b[varIntLength:totalLength])

View file

@ -14,6 +14,7 @@
package scrape package scrape
import ( import (
"errors"
"fmt" "fmt"
"hash/fnv" "hash/fnv"
"reflect" "reflect"
@ -22,7 +23,6 @@ import (
"github.com/go-kit/log" "github.com/go-kit/log"
"github.com/go-kit/log/level" "github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
config_util "github.com/prometheus/common/config" config_util "github.com/prometheus/common/config"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"

View file

@ -14,6 +14,7 @@
package scrape package scrape
import ( import (
"errors"
"fmt" "fmt"
"hash/fnv" "hash/fnv"
"net" "net"
@ -22,7 +23,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/pkg/errors"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/config"
@ -289,12 +289,12 @@ func (t *Target) intervalAndTimeout(defaultInterval, defaultDuration time.Durati
intervalLabel := t.labels.Get(model.ScrapeIntervalLabel) intervalLabel := t.labels.Get(model.ScrapeIntervalLabel)
interval, err := model.ParseDuration(intervalLabel) interval, err := model.ParseDuration(intervalLabel)
if err != nil { if err != nil {
return defaultInterval, defaultDuration, errors.Errorf("Error parsing interval label %q: %v", intervalLabel, err) return defaultInterval, defaultDuration, fmt.Errorf("Error parsing interval label %q: %w", intervalLabel, err)
} }
timeoutLabel := t.labels.Get(model.ScrapeTimeoutLabel) timeoutLabel := t.labels.Get(model.ScrapeTimeoutLabel)
timeout, err := model.ParseDuration(timeoutLabel) timeout, err := model.ParseDuration(timeoutLabel)
if err != nil { if err != nil {
return defaultInterval, defaultDuration, errors.Errorf("Error parsing timeout label %q: %v", timeoutLabel, err) return defaultInterval, defaultDuration, fmt.Errorf("Error parsing timeout label %q: %w", timeoutLabel, err)
} }
return time.Duration(interval), time.Duration(timeout), nil return time.Duration(interval), time.Duration(timeout), nil
@ -444,7 +444,7 @@ func PopulateLabels(lb *labels.Builder, cfg *config.ScrapeConfig, noDefaultPort
case "https": case "https":
addr += ":443" addr += ":443"
default: default:
return labels.EmptyLabels(), labels.EmptyLabels(), errors.Errorf("invalid scheme: %q", cfg.Scheme) return labels.EmptyLabels(), labels.EmptyLabels(), fmt.Errorf("invalid scheme: %q", cfg.Scheme)
} }
lb.Set(model.AddressLabel, addr) lb.Set(model.AddressLabel, addr)
} }
@ -471,7 +471,7 @@ func PopulateLabels(lb *labels.Builder, cfg *config.ScrapeConfig, noDefaultPort
interval := lb.Get(model.ScrapeIntervalLabel) interval := lb.Get(model.ScrapeIntervalLabel)
intervalDuration, err := model.ParseDuration(interval) intervalDuration, err := model.ParseDuration(interval)
if err != nil { if err != nil {
return labels.EmptyLabels(), labels.EmptyLabels(), errors.Errorf("error parsing scrape interval: %v", err) return labels.EmptyLabels(), labels.EmptyLabels(), fmt.Errorf("error parsing scrape interval: %w", err)
} }
if time.Duration(intervalDuration) == 0 { if time.Duration(intervalDuration) == 0 {
return labels.EmptyLabels(), labels.EmptyLabels(), errors.New("scrape interval cannot be 0") return labels.EmptyLabels(), labels.EmptyLabels(), errors.New("scrape interval cannot be 0")
@ -480,14 +480,14 @@ func PopulateLabels(lb *labels.Builder, cfg *config.ScrapeConfig, noDefaultPort
timeout := lb.Get(model.ScrapeTimeoutLabel) timeout := lb.Get(model.ScrapeTimeoutLabel)
timeoutDuration, err := model.ParseDuration(timeout) timeoutDuration, err := model.ParseDuration(timeout)
if err != nil { if err != nil {
return labels.EmptyLabels(), labels.EmptyLabels(), errors.Errorf("error parsing scrape timeout: %v", err) return labels.EmptyLabels(), labels.EmptyLabels(), fmt.Errorf("error parsing scrape timeout: %w", err)
} }
if time.Duration(timeoutDuration) == 0 { if time.Duration(timeoutDuration) == 0 {
return labels.EmptyLabels(), labels.EmptyLabels(), errors.New("scrape timeout cannot be 0") return labels.EmptyLabels(), labels.EmptyLabels(), errors.New("scrape timeout cannot be 0")
} }
if timeoutDuration > intervalDuration { if timeoutDuration > intervalDuration {
return labels.EmptyLabels(), labels.EmptyLabels(), errors.Errorf("scrape timeout cannot be greater than scrape interval (%q > %q)", timeout, interval) return labels.EmptyLabels(), labels.EmptyLabels(), fmt.Errorf("scrape timeout cannot be greater than scrape interval (%q > %q)", timeout, interval)
} }
// Meta labels are deleted after relabelling. Other internal labels propagate to // Meta labels are deleted after relabelling. Other internal labels propagate to
@ -507,7 +507,7 @@ func PopulateLabels(lb *labels.Builder, cfg *config.ScrapeConfig, noDefaultPort
err = res.Validate(func(l labels.Label) error { err = res.Validate(func(l labels.Label) error {
// Check label values are valid, drop the target if not. // Check label values are valid, drop the target if not.
if !model.LabelValue(l.Value).IsValid() { if !model.LabelValue(l.Value).IsValid() {
return errors.Errorf("invalid label value for %q: %q", l.Name, l.Value) return fmt.Errorf("invalid label value for %q: %q", l.Name, l.Value)
} }
return nil return nil
}) })
@ -536,7 +536,7 @@ func TargetsFromGroup(tg *targetgroup.Group, cfg *config.ScrapeConfig, noDefault
lset, origLabels, err := PopulateLabels(lb, cfg, noDefaultPort) lset, origLabels, err := PopulateLabels(lb, cfg, noDefaultPort)
if err != nil { if err != nil {
failures = append(failures, errors.Wrapf(err, "instance %d in group %s", i, tg)) failures = append(failures, fmt.Errorf("instance %d in group %s: %w", i, tg, err))
} }
if !lset.IsEmpty() || !origLabels.IsEmpty() { if !lset.IsEmpty() || !origLabels.IsEmpty() {
targets = append(targets, NewTarget(lset, origLabels, cfg.Params)) targets = append(targets, NewTarget(lset, origLabels, cfg.Params))

View file

@ -15,9 +15,9 @@ package storage_test
import ( import (
"context" "context"
"errors"
"testing" "testing"
"github.com/pkg/errors"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"

View file

@ -81,8 +81,8 @@ func (a Annotations) AsStrings(query string, maxAnnos int) []string {
if maxAnnos > 0 && len(arr) >= maxAnnos { if maxAnnos > 0 && len(arr) >= maxAnnos {
break break
} }
anErr, ok := err.(annoErr) var anErr annoErr
if ok { if errors.As(err, &anErr) {
anErr.Query = query anErr.Query = query
err = anErr err = anErr
} }