diff --git a/storage/merge.go b/storage/merge.go index f7246c7c8..7726f9bdc 100644 --- a/storage/merge.go +++ b/storage/merge.go @@ -16,12 +16,11 @@ package storage import ( "bytes" "container/heap" + "fmt" "math" "sort" "sync" - "github.com/pkg/errors" - "github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/tsdb/chunkenc" "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) { res, ws, err := q.lvals(q.queriers, name, matchers...) 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 } @@ -227,7 +226,7 @@ func (q *mergeGenericQuerier) LabelNames(matchers ...*labels.Matcher) ([]string, warnings = append(warnings, wrn...) } 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 { labelNamesMap[name] = struct{}{} diff --git a/storage/merge_test.go b/storage/merge_test.go index c0b49c836..933e5b1bd 100644 --- a/storage/merge_test.go +++ b/storage/merge_test.go @@ -14,13 +14,13 @@ package storage import ( + "errors" "fmt" "math" "sort" "sync" "testing" - "github.com/pkg/errors" "github.com/stretchr/testify/require" "github.com/prometheus/prometheus/model/labels" diff --git a/storage/remote/chunked.go b/storage/remote/chunked.go index 670a16834..96ce483e0 100644 --- a/storage/remote/chunked.go +++ b/storage/remote/chunked.go @@ -16,13 +16,14 @@ package remote import ( "bufio" "encoding/binary" + "errors" + "fmt" "hash" "hash/crc32" "io" "net/http" "github.com/gogo/protobuf/proto" - "github.com/pkg/errors" ) // 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 { - 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) { diff --git a/storage/remote/client.go b/storage/remote/client.go index 7120822d9..92666cd1d 100644 --- a/storage/remote/client.go +++ b/storage/remote/client.go @@ -26,7 +26,6 @@ import ( "github.com/gogo/protobuf/proto" "github.com/golang/snappy" - "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" @@ -222,7 +221,7 @@ func (c *Client) Store(ctx context.Context, req []byte) error { if scanner.Scan() { 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 { 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) 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) httpReq, err := http.NewRequest("POST", c.url.String(), bytes.NewReader(compressed)) 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("Accept-Encoding", "snappy") @@ -296,7 +295,7 @@ func (c *Client) Read(ctx context.Context, query *prompb.Query) (*prompb.QueryRe start := time.Now() httpResp, err := c.Client.Do(httpReq.WithContext(ctx)) if err != nil { - return nil, errors.Wrap(err, "error sending request") + return nil, fmt.Errorf("error sending request: %w", err) } defer func() { 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) 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 { - 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) if err != nil { - return nil, errors.Wrap(err, "error reading response") + return nil, fmt.Errorf("error reading response: %w", err) } var resp prompb.ReadResponse err = proto.Unmarshal(uncompressed, &resp) 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) { - 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 diff --git a/storage/remote/client_test.go b/storage/remote/client_test.go index f9162a279..c040667a5 100644 --- a/storage/remote/client_test.go +++ b/storage/remote/client_test.go @@ -15,6 +15,7 @@ package remote import ( "context" + "errors" "net/http" "net/http/httptest" "net/url" @@ -22,7 +23,6 @@ import ( "testing" "time" - "github.com/pkg/errors" config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" "github.com/stretchr/testify/require" diff --git a/storage/remote/codec.go b/storage/remote/codec.go index f56fc6760..5d78107bb 100644 --- a/storage/remote/codec.go +++ b/storage/remote/codec.go @@ -14,6 +14,7 @@ package remote import ( + "errors" "fmt" "io" "net/http" @@ -22,7 +23,6 @@ import ( "github.com/gogo/protobuf/proto" "github.com/golang/snappy" - "github.com/pkg/errors" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/model/exemplar" @@ -180,7 +180,7 @@ func NegotiateResponseType(accepted []prompb.ReadRequest_ResponseType) (prompb.R 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. @@ -214,7 +214,7 @@ func StreamChunkedReadResponses( chk := iter.At() 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. @@ -239,11 +239,11 @@ func StreamChunkedReadResponses( QueryIndex: queryIndex, }) 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 { - return ss.Warnings(), errors.Wrap(err, "write to stream") + return ss.Warnings(), fmt.Errorf("write to stream: %w", err) } chks = chks[:0] } @@ -395,16 +395,16 @@ func (c *concreteSeriesIterator) Err() error { func validateLabelsAndMetricName(ls labels.Labels) error { for i, l := range ls { 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() { - return errors.Errorf("invalid label name: %v", l.Name) + return fmt.Errorf("invalid label name: %v", l.Name) } 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 { - return errors.Errorf("duplicate label with name: %v", l.Name) + return fmt.Errorf("duplicate label with name: %v", l.Name) } } return nil diff --git a/storage/remote/metadata_watcher.go b/storage/remote/metadata_watcher.go index eee36463b..21de565ed 100644 --- a/storage/remote/metadata_watcher.go +++ b/storage/remote/metadata_watcher.go @@ -15,11 +15,11 @@ package remote import ( "context" + "errors" "time" "github.com/go-kit/log" "github.com/go-kit/log/level" - "github.com/pkg/errors" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/scrape" diff --git a/storage/remote/metadata_watcher_test.go b/storage/remote/metadata_watcher_test.go index 3660bb9a7..bc841a014 100644 --- a/storage/remote/metadata_watcher_test.go +++ b/storage/remote/metadata_watcher_test.go @@ -15,10 +15,10 @@ package remote import ( "context" + "errors" "testing" "time" - "github.com/pkg/errors" "github.com/prometheus/common/model" "github.com/stretchr/testify/require" diff --git a/storage/remote/read.go b/storage/remote/read.go index 071763593..154eb73f9 100644 --- a/storage/remote/read.go +++ b/storage/remote/read.go @@ -15,8 +15,8 @@ package remote import ( "context" - - "github.com/pkg/errors" + "errors" + "fmt" "github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/storage" @@ -164,12 +164,12 @@ func (q *querier) Select(sortSeries bool, hints *storage.SelectHints, matchers . m, added := q.addExternalLabels(matchers) query, err := ToQuery(q.mint, q.maxt, m, hints) 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) 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) } diff --git a/storage/remote/read_test.go b/storage/remote/read_test.go index 6681b9e97..98c1abcf9 100644 --- a/storage/remote/read_test.go +++ b/storage/remote/read_test.go @@ -15,11 +15,11 @@ package remote import ( "context" + "fmt" "net/url" "sort" "testing" - "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" config_util "github.com/prometheus/common/config" "github.com/stretchr/testify/require" @@ -209,7 +209,7 @@ type mockedRemoteClient struct { func (c *mockedRemoteClient) Read(_ context.Context, query *prompb.Query) (*prompb.QueryResult, error) { 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 diff --git a/storage/remote/write_handler.go b/storage/remote/write_handler.go index 1807543ff..649362223 100644 --- a/storage/remote/write_handler.go +++ b/storage/remote/write_handler.go @@ -15,12 +15,12 @@ package remote import ( "context" + "errors" "fmt" "net/http" "github.com/go-kit/log" "github.com/go-kit/log/level" - "github.com/pkg/errors" "github.com/prometheus/prometheus/model/exemplar" "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. func (h *writeHandler) checkAppendExemplarError(err error, e exemplar.Exemplar, outOfOrderErrs *int) error { - switch errors.Cause(err) { - case storage.ErrNotFound: + unwrapedErr := errors.Unwrap(err) + switch { + case errors.Is(unwrapedErr, storage.ErrNotFound): return storage.ErrNotFound - case storage.ErrOutOfOrderExemplar: + case errors.Is(unwrapedErr, storage.ErrOutOfOrderExemplar): *outOfOrderErrs++ level.Debug(h.logger).Log("msg", "Out of order exemplar", "exemplar", fmt.Sprintf("%+v", e)) return nil @@ -97,8 +98,8 @@ func (h *writeHandler) write(ctx context.Context, req *prompb.WriteRequest) (err for _, s := range ts.Samples { _, err = app.Append(0, labels, s.Timestamp, s.Value) if err != nil { - switch errors.Cause(err) { - case storage.ErrOutOfOrderSample, storage.ErrOutOfBounds, storage.ErrDuplicateSampleForTimestamp: + unwrapedErr := errors.Unwrap(err) + 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) } return err