(storage): move from github.com/pkg/errors to 'errors' and 'fmt' (#10946)

Signed-off-by: Matthieu MOREL <mmorel-35@users.noreply.github.com>

Co-authored-by: Matthieu MOREL <mmorel-35@users.noreply.github.com>
This commit is contained in:
Matthieu MOREL 2022-07-01 18:59:50 +02:00 committed by GitHub
parent 13bd4fd3c8
commit d56d0a9d52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 41 additions and 41 deletions

View file

@ -16,12 +16,11 @@ package storage
import ( import (
"bytes" "bytes"
"container/heap" "container/heap"
"fmt"
"math" "math"
"sort" "sort"
"sync" "sync"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/tsdb/chunkenc" "github.com/prometheus/prometheus/tsdb/chunkenc"
"github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/chunks"
@ -159,7 +158,7 @@ func (l labelGenericQueriers) SplitByHalf() (labelGenericQueriers, labelGenericQ
func (q *mergeGenericQuerier) LabelValues(name string, matchers ...*labels.Matcher) ([]string, Warnings, error) { func (q *mergeGenericQuerier) LabelValues(name string, matchers ...*labels.Matcher) ([]string, Warnings, error) {
res, ws, err := q.lvals(q.queriers, name, matchers...) res, ws, err := q.lvals(q.queriers, name, matchers...)
if err != nil { if err != nil {
return nil, nil, errors.Wrapf(err, "LabelValues() from merge generic querier for label %s", name) return nil, nil, fmt.Errorf("LabelValues() from merge generic querier for label %s: %w", name, err)
} }
return res, ws, nil return res, ws, nil
} }
@ -227,7 +226,7 @@ func (q *mergeGenericQuerier) LabelNames(matchers ...*labels.Matcher) ([]string,
warnings = append(warnings, wrn...) warnings = append(warnings, wrn...)
} }
if err != nil { if err != nil {
return nil, nil, errors.Wrap(err, "LabelNames() from merge generic querier") return nil, nil, fmt.Errorf("LabelNames() from merge generic querier: %w", err)
} }
for _, name := range names { for _, name := range names {
labelNamesMap[name] = struct{}{} labelNamesMap[name] = struct{}{}

View file

@ -14,13 +14,13 @@
package storage package storage
import ( import (
"errors"
"fmt" "fmt"
"math" "math"
"sort" "sort"
"sync" "sync"
"testing" "testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/model/labels"

View file

@ -16,13 +16,14 @@ package remote
import ( import (
"bufio" "bufio"
"encoding/binary" "encoding/binary"
"errors"
"fmt"
"hash" "hash"
"hash/crc32" "hash/crc32"
"io" "io"
"net/http" "net/http"
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
) )
// DefaultChunkedReadLimit is the default value for the maximum size of the protobuf frame client allows. // DefaultChunkedReadLimit is the default value for the maximum size of the protobuf frame client allows.
@ -119,7 +120,7 @@ func (r *ChunkedReader) Next() ([]byte, error) {
} }
if size > r.sizeLimit { if size > r.sizeLimit {
return nil, errors.Errorf("chunkedReader: message size exceeded the limit %v bytes; got: %v bytes", r.sizeLimit, size) return nil, fmt.Errorf("chunkedReader: message size exceeded the limit %v bytes; got: %v bytes", r.sizeLimit, size)
} }
if cap(r.data) < int(size) { if cap(r.data) < int(size) {

View file

@ -26,7 +26,6 @@ import (
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
"github.com/golang/snappy" "github.com/golang/snappy"
"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"
@ -222,7 +221,7 @@ func (c *Client) Store(ctx context.Context, req []byte) error {
if scanner.Scan() { if scanner.Scan() {
line = scanner.Text() line = scanner.Text()
} }
err = errors.Errorf("server returned HTTP status %s: %s", httpResp.Status, line) err = fmt.Errorf("server returned HTTP status %s: %s", httpResp.Status, line)
} }
if httpResp.StatusCode/100 == 5 { if httpResp.StatusCode/100 == 5 {
return RecoverableError{err, defaultBackoff} return RecoverableError{err, defaultBackoff}
@ -273,13 +272,13 @@ func (c *Client) Read(ctx context.Context, query *prompb.Query) (*prompb.QueryRe
} }
data, err := proto.Marshal(req) data, err := proto.Marshal(req)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "unable to marshal read request") return nil, fmt.Errorf("unable to marshal read request: %w", err)
} }
compressed := snappy.Encode(nil, data) compressed := snappy.Encode(nil, data)
httpReq, err := http.NewRequest("POST", c.url.String(), bytes.NewReader(compressed)) httpReq, err := http.NewRequest("POST", c.url.String(), bytes.NewReader(compressed))
if err != nil { if err != nil {
return nil, errors.Wrap(err, "unable to create request") return nil, fmt.Errorf("unable to create request: %w", err)
} }
httpReq.Header.Add("Content-Encoding", "snappy") httpReq.Header.Add("Content-Encoding", "snappy")
httpReq.Header.Add("Accept-Encoding", "snappy") httpReq.Header.Add("Accept-Encoding", "snappy")
@ -296,7 +295,7 @@ func (c *Client) Read(ctx context.Context, query *prompb.Query) (*prompb.QueryRe
start := time.Now() start := time.Now()
httpResp, err := c.Client.Do(httpReq.WithContext(ctx)) httpResp, err := c.Client.Do(httpReq.WithContext(ctx))
if err != nil { if err != nil {
return nil, errors.Wrap(err, "error sending request") return nil, fmt.Errorf("error sending request: %w", err)
} }
defer func() { defer func() {
io.Copy(io.Discard, httpResp.Body) io.Copy(io.Discard, httpResp.Body)
@ -307,26 +306,26 @@ func (c *Client) Read(ctx context.Context, query *prompb.Query) (*prompb.QueryRe
compressed, err = io.ReadAll(httpResp.Body) compressed, err = io.ReadAll(httpResp.Body)
if err != nil { if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("error reading response. HTTP status code: %s", httpResp.Status)) return nil, fmt.Errorf("error reading response. HTTP status code: %s: %w", httpResp.Status, err)
} }
if httpResp.StatusCode/100 != 2 { if httpResp.StatusCode/100 != 2 {
return nil, errors.Errorf("remote server %s returned HTTP status %s: %s", c.url.String(), httpResp.Status, strings.TrimSpace(string(compressed))) return nil, fmt.Errorf("remote server %s returned HTTP status %s: %s", c.url.String(), httpResp.Status, strings.TrimSpace(string(compressed)))
} }
uncompressed, err := snappy.Decode(nil, compressed) uncompressed, err := snappy.Decode(nil, compressed)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "error reading response") return nil, fmt.Errorf("error reading response: %w", err)
} }
var resp prompb.ReadResponse var resp prompb.ReadResponse
err = proto.Unmarshal(uncompressed, &resp) err = proto.Unmarshal(uncompressed, &resp)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "unable to unmarshal response body") return nil, fmt.Errorf("unable to unmarshal response body: %w", err)
} }
if len(resp.Results) != len(req.Queries) { if len(resp.Results) != len(req.Queries) {
return nil, errors.Errorf("responses: want %d, got %d", len(req.Queries), len(resp.Results)) return nil, fmt.Errorf("responses: want %d, got %d", len(req.Queries), len(resp.Results))
} }
return resp.Results[0], nil return resp.Results[0], nil

View file

@ -15,6 +15,7 @@ package remote
import ( import (
"context" "context"
"errors"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
@ -22,7 +23,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/pkg/errors"
config_util "github.com/prometheus/common/config" config_util "github.com/prometheus/common/config"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"

View file

@ -14,6 +14,7 @@
package remote package remote
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -22,7 +23,6 @@ import (
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
"github.com/golang/snappy" "github.com/golang/snappy"
"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"
@ -180,7 +180,7 @@ func NegotiateResponseType(accepted []prompb.ReadRequest_ResponseType) (prompb.R
return resType, nil return resType, nil
} }
} }
return 0, errors.Errorf("server does not support any of the requested response types: %v; supported: %v", accepted, supported) return 0, fmt.Errorf("server does not support any of the requested response types: %v; supported: %v", accepted, supported)
} }
// StreamChunkedReadResponses iterates over series, builds chunks and streams those to the caller. // StreamChunkedReadResponses iterates over series, builds chunks and streams those to the caller.
@ -214,7 +214,7 @@ func StreamChunkedReadResponses(
chk := iter.At() chk := iter.At()
if chk.Chunk == nil { if chk.Chunk == nil {
return ss.Warnings(), errors.Errorf("StreamChunkedReadResponses: found not populated chunk returned by SeriesSet at ref: %v", chk.Ref) return ss.Warnings(), fmt.Errorf("StreamChunkedReadResponses: found not populated chunk returned by SeriesSet at ref: %v", chk.Ref)
} }
// Cut the chunk. // Cut the chunk.
@ -239,11 +239,11 @@ func StreamChunkedReadResponses(
QueryIndex: queryIndex, QueryIndex: queryIndex,
}) })
if err != nil { if err != nil {
return ss.Warnings(), errors.Wrap(err, "marshal ChunkedReadResponse") return ss.Warnings(), fmt.Errorf("marshal ChunkedReadResponse: %w", err)
} }
if _, err := stream.Write(b); err != nil { if _, err := stream.Write(b); err != nil {
return ss.Warnings(), errors.Wrap(err, "write to stream") return ss.Warnings(), fmt.Errorf("write to stream: %w", err)
} }
chks = chks[:0] chks = chks[:0]
} }
@ -395,16 +395,16 @@ func (c *concreteSeriesIterator) Err() error {
func validateLabelsAndMetricName(ls labels.Labels) error { func validateLabelsAndMetricName(ls labels.Labels) error {
for i, l := range ls { for i, l := range ls {
if l.Name == labels.MetricName && !model.IsValidMetricName(model.LabelValue(l.Value)) { if l.Name == labels.MetricName && !model.IsValidMetricName(model.LabelValue(l.Value)) {
return errors.Errorf("invalid metric name: %v", l.Value) return fmt.Errorf("invalid metric name: %v", l.Value)
} }
if !model.LabelName(l.Name).IsValid() { if !model.LabelName(l.Name).IsValid() {
return errors.Errorf("invalid label name: %v", l.Name) return fmt.Errorf("invalid label name: %v", l.Name)
} }
if !model.LabelValue(l.Value).IsValid() { if !model.LabelValue(l.Value).IsValid() {
return errors.Errorf("invalid label value: %v", l.Value) return fmt.Errorf("invalid label value: %v", l.Value)
} }
if i > 0 && l.Name == ls[i-1].Name { if i > 0 && l.Name == ls[i-1].Name {
return errors.Errorf("duplicate label with name: %v", l.Name) return fmt.Errorf("duplicate label with name: %v", l.Name)
} }
} }
return nil return nil

View file

@ -15,11 +15,11 @@ package remote
import ( import (
"context" "context"
"errors"
"time" "time"
"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/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/scrape" "github.com/prometheus/prometheus/scrape"

View file

@ -15,10 +15,10 @@ package remote
import ( import (
"context" "context"
"errors"
"testing" "testing"
"time" "time"
"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

@ -15,8 +15,8 @@ package remote
import ( import (
"context" "context"
"errors"
"github.com/pkg/errors" "fmt"
"github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/storage"
@ -164,12 +164,12 @@ func (q *querier) Select(sortSeries bool, hints *storage.SelectHints, matchers .
m, added := q.addExternalLabels(matchers) m, added := q.addExternalLabels(matchers)
query, err := ToQuery(q.mint, q.maxt, m, hints) query, err := ToQuery(q.mint, q.maxt, m, hints)
if err != nil { if err != nil {
return storage.ErrSeriesSet(errors.Wrap(err, "toQuery")) return storage.ErrSeriesSet(fmt.Errorf("toQuery: %w", err))
} }
res, err := q.client.Read(q.ctx, query) res, err := q.client.Read(q.ctx, query)
if err != nil { if err != nil {
return storage.ErrSeriesSet(errors.Wrap(err, "remote_read")) return storage.ErrSeriesSet(fmt.Errorf("remote_read: %w", err))
} }
return newSeriesSetFilter(FromQueryResult(sortSeries, res), added) return newSeriesSetFilter(FromQueryResult(sortSeries, res), added)
} }

View file

@ -15,11 +15,11 @@ package remote
import ( import (
"context" "context"
"fmt"
"net/url" "net/url"
"sort" "sort"
"testing" "testing"
"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/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -209,7 +209,7 @@ type mockedRemoteClient struct {
func (c *mockedRemoteClient) Read(_ context.Context, query *prompb.Query) (*prompb.QueryResult, error) { func (c *mockedRemoteClient) Read(_ context.Context, query *prompb.Query) (*prompb.QueryResult, error) {
if c.got != nil { if c.got != nil {
return nil, errors.Errorf("expected only one call to remote client got: %v", query) return nil, fmt.Errorf("expected only one call to remote client got: %v", query)
} }
c.got = query c.got = query

View file

@ -15,12 +15,12 @@ package remote
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"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/prometheus/model/exemplar" "github.com/prometheus/prometheus/model/exemplar"
"github.com/prometheus/prometheus/prompb" "github.com/prometheus/prometheus/prompb"
@ -67,10 +67,11 @@ func (h *writeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// checkAppendExemplarError modifies the AppendExamplar's returned error based on the error cause. // checkAppendExemplarError modifies the AppendExamplar's returned error based on the error cause.
func (h *writeHandler) checkAppendExemplarError(err error, e exemplar.Exemplar, outOfOrderErrs *int) error { func (h *writeHandler) checkAppendExemplarError(err error, e exemplar.Exemplar, outOfOrderErrs *int) error {
switch errors.Cause(err) { unwrapedErr := errors.Unwrap(err)
case storage.ErrNotFound: switch {
case errors.Is(unwrapedErr, storage.ErrNotFound):
return storage.ErrNotFound return storage.ErrNotFound
case storage.ErrOutOfOrderExemplar: case errors.Is(unwrapedErr, storage.ErrOutOfOrderExemplar):
*outOfOrderErrs++ *outOfOrderErrs++
level.Debug(h.logger).Log("msg", "Out of order exemplar", "exemplar", fmt.Sprintf("%+v", e)) level.Debug(h.logger).Log("msg", "Out of order exemplar", "exemplar", fmt.Sprintf("%+v", e))
return nil return nil
@ -97,8 +98,8 @@ func (h *writeHandler) write(ctx context.Context, req *prompb.WriteRequest) (err
for _, s := range ts.Samples { for _, s := range ts.Samples {
_, err = app.Append(0, labels, s.Timestamp, s.Value) _, err = app.Append(0, labels, s.Timestamp, s.Value)
if err != nil { if err != nil {
switch errors.Cause(err) { unwrapedErr := errors.Unwrap(err)
case storage.ErrOutOfOrderSample, storage.ErrOutOfBounds, storage.ErrDuplicateSampleForTimestamp: if errors.Is(unwrapedErr, storage.ErrOutOfOrderSample) || errors.Is(unwrapedErr, storage.ErrOutOfBounds) || errors.Is(unwrapedErr, storage.ErrDuplicateSampleForTimestamp) {
level.Error(h.logger).Log("msg", "Out of order sample from remote write", "err", err.Error(), "series", labels.String(), "timestamp", s.Timestamp) level.Error(h.logger).Log("msg", "Out of order sample from remote write", "err", err.Error(), "series", labels.String(), "timestamp", s.Timestamp)
} }
return err return err