From 45c2f81671342e5193229497da8075c3b6c36cee Mon Sep 17 00:00:00 2001 From: ntap-fge <117348352+ntap-fge@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:41:53 +0200 Subject: [PATCH] Address new golangci-lint issues --- .golangci.yml | 2 ++ pkg/ingester/instance.go | 2 +- pkg/ingester/stream.go | 2 +- pkg/ingester/stream_test.go | 2 +- pkg/logentry/stages/match.go | 4 ++-- pkg/logql/engine.go | 1 + pkg/logql/log/jsonexpr/lexer.go | 4 ++-- pkg/querier/http.go | 18 +++++++++--------- pkg/querier/queryrange/codec.go | 12 ++++++------ pkg/querier/queryrange/limits.go | 4 ++-- pkg/querier/queryrange/roundtrip.go | 14 +++++++------- pkg/querier/queryrange/split_by_interval.go | 2 +- pkg/util/server/error_test.go | 2 +- pkg/util/server/middleware.go | 2 +- 14 files changed, 37 insertions(+), 34 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index c0804db5..5420bc53 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -73,3 +73,5 @@ issues: exclude: - Error return value of .*log\.Logger\)\.Log\x60 is not checked - Error return value of .*.Log.* is not checked + - '"io/ioutil" has been deprecated' + - rand.Seed has been deprecated diff --git a/pkg/ingester/instance.go b/pkg/ingester/instance.go index 62a5bc0c..39baebef 100644 --- a/pkg/ingester/instance.go +++ b/pkg/ingester/instance.go @@ -233,7 +233,7 @@ func (i *instance) getOrCreateStream(pushReqStream logproto.Stream, lock bool, r "stream", pushReqStream.Labels, ) } - return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + return nil, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) } fp := i.getHashForLabels(labels) diff --git a/pkg/ingester/stream.go b/pkg/ingester/stream.go index 20d67b60..0d71c617 100644 --- a/pkg/ingester/stream.go +++ b/pkg/ingester/stream.go @@ -269,7 +269,7 @@ func (s *stream) Push( fmt.Fprintf(&buf, "total ignored: %d out of %d", len(failedEntriesWithError), len(entries)) - return bytesAdded, httpgrpc.Errorf(http.StatusBadRequest, buf.String()) + return bytesAdded, httpgrpc.Errorf(http.StatusBadRequest, "%s", buf.String()) } return bytesAdded, lastEntryWithErr.e } diff --git a/pkg/ingester/stream_test.go b/pkg/ingester/stream_test.go index 9b8b1a6c..17a9c5ff 100644 --- a/pkg/ingester/stream_test.go +++ b/pkg/ingester/stream_test.go @@ -63,7 +63,7 @@ func TestMaxReturnedStreamsErrors(t *testing.T) { } fmt.Fprintf(&expected, "total ignored: %d out of %d", numLogs, numLogs) - expectErr := httpgrpc.Errorf(http.StatusBadRequest, expected.String()) + expectErr := httpgrpc.Errorf(http.StatusBadRequest, "%s", expected.String()) _, err = s.Push(context.Background(), newLines, recordPool.GetRecord()) require.Error(t, err) diff --git a/pkg/logentry/stages/match.go b/pkg/logentry/stages/match.go index cb31e87b..d59aec5e 100644 --- a/pkg/logentry/stages/match.go +++ b/pkg/logentry/stages/match.go @@ -55,10 +55,10 @@ func validateMatcherConfig(cfg *MatcherConfig) (logql.LogSelectorExpr, error) { return nil, errors.New(ErrUnknownMatchAction) } - if cfg.Action == MatchActionKeep && (cfg.Stages == nil || len(cfg.Stages) == 0) { + if cfg.Action == MatchActionKeep && len(cfg.Stages) == 0 { return nil, errors.New(ErrMatchRequiresStages) } - if cfg.Action == MatchActionDrop && (cfg.Stages != nil && len(cfg.Stages) != 0) { + if cfg.Action == MatchActionDrop && len(cfg.Stages) != 0 { return nil, errors.New(ErrStagesWithDropLine) } diff --git a/pkg/logql/engine.go b/pkg/logql/engine.go index 994d0868..e33b8950 100644 --- a/pkg/logql/engine.go +++ b/pkg/logql/engine.go @@ -290,6 +290,7 @@ func (q *query) evalSample(ctx context.Context, expr SampleExpr) (promql_parser. return result, stepEvaluator.Error() } +// nolint: unparam func (q *query) evalLiteral(_ context.Context, expr *literalExpr) (promql_parser.Value, error) { s := promql.Scalar{ T: q.params.Start().UnixNano() / int64(time.Millisecond), diff --git a/pkg/logql/log/jsonexpr/lexer.go b/pkg/logql/log/jsonexpr/lexer.go index f92a72ef..5cf387d8 100644 --- a/pkg/logql/log/jsonexpr/lexer.go +++ b/pkg/logql/log/jsonexpr/lexer.go @@ -23,7 +23,7 @@ func NewScanner(r io.Reader, debug bool) *Scanner { } func (sc *Scanner) Error(s string) { - sc.err = fmt.Errorf(s) + sc.err = fmt.Errorf("%s", s) fmt.Printf("syntax error: %s\n", s) } @@ -53,7 +53,7 @@ func (sc *Scanner) lex(lval *JSONExprSymType) int { sc.unread() val, err := sc.scanInt() if err != nil { - sc.err = fmt.Errorf(err.Error()) + sc.err = fmt.Errorf("%s", err.Error()) return 0 } diff --git a/pkg/querier/http.go b/pkg/querier/http.go index 4b183ab1..89285dba 100644 --- a/pkg/querier/http.go +++ b/pkg/querier/http.go @@ -38,7 +38,7 @@ func (q *Querier) RangeQueryHandler(w http.ResponseWriter, r *http.Request) { request, err := loghttp.ParseRangeQuery(r) if err != nil { - serverutil.WriteError(httpgrpc.Errorf(http.StatusBadRequest, err.Error()), w) + serverutil.WriteError(httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()), w) return } @@ -78,7 +78,7 @@ func (q *Querier) InstantQueryHandler(w http.ResponseWriter, r *http.Request) { request, err := loghttp.ParseInstantQuery(r) if err != nil { - serverutil.WriteError(httpgrpc.Errorf(http.StatusBadRequest, err.Error()), w) + serverutil.WriteError(httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()), w) return } @@ -118,12 +118,12 @@ func (q *Querier) LogQueryHandler(w http.ResponseWriter, r *http.Request) { request, err := loghttp.ParseRangeQuery(r) if err != nil { - serverutil.WriteError(httpgrpc.Errorf(http.StatusBadRequest, err.Error()), w) + serverutil.WriteError(httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()), w) return } request.Query, err = parseRegexQuery(r) if err != nil { - serverutil.WriteError(httpgrpc.Errorf(http.StatusBadRequest, err.Error()), w) + serverutil.WriteError(httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()), w) return } @@ -172,7 +172,7 @@ func (q *Querier) LogQueryHandler(w http.ResponseWriter, r *http.Request) { func (q *Querier) LabelHandler(w http.ResponseWriter, r *http.Request) { req, err := loghttp.ParseLabelQuery(r) if err != nil { - serverutil.WriteError(httpgrpc.Errorf(http.StatusBadRequest, err.Error()), w) + serverutil.WriteError(httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()), w) return } @@ -202,13 +202,13 @@ func (q *Querier) TailHandler(w http.ResponseWriter, r *http.Request) { req, err := loghttp.ParseTailQuery(r) if err != nil { - serverutil.WriteError(httpgrpc.Errorf(http.StatusBadRequest, err.Error()), w) + serverutil.WriteError(httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()), w) return } req.Query, err = parseRegexQuery(r) if err != nil { - serverutil.WriteError(httpgrpc.Errorf(http.StatusBadRequest, err.Error()), w) + serverutil.WriteError(httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()), w) return } @@ -309,7 +309,7 @@ func (q *Querier) TailHandler(w http.ResponseWriter, r *http.Request) { func (q *Querier) SeriesHandler(w http.ResponseWriter, r *http.Request) { req, err := loghttp.ParseSeriesQuery(r) if err != nil { - serverutil.WriteError(httpgrpc.Errorf(http.StatusBadRequest, err.Error()), w) + serverutil.WriteError(httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()), w) return } @@ -348,7 +348,7 @@ func parseRegexQuery(httpRequest *http.Request) (string, error) { func (q *Querier) validateEntriesLimits(ctx context.Context, query string, limit uint32) error { userID, err := user.ExtractOrgID(ctx) if err != nil { - return httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + return httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) } expr, err := logql.ParseExpr(query) diff --git a/pkg/querier/queryrange/codec.go b/pkg/querier/queryrange/codec.go index 073f7410..b82858cb 100644 --- a/pkg/querier/queryrange/codec.go +++ b/pkg/querier/queryrange/codec.go @@ -151,14 +151,14 @@ func (*ValiLabelNamesRequest) GetCachingOptions() (res queryrange.CachingOptions func (codec) DecodeRequest(_ context.Context, r *http.Request) (queryrange.Request, error) { if err := r.ParseForm(); err != nil { - return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + return nil, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) } switch op := getOperation(r.URL.Path); op { case QueryRangeOp: req, err := loghttp.ParseRangeQuery(r) if err != nil { - return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + return nil, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) } return &ValiRequest{ Query: req.Query, @@ -174,7 +174,7 @@ func (codec) DecodeRequest(_ context.Context, r *http.Request) (queryrange.Reque case SeriesOp: req, err := loghttp.ParseSeriesQuery(r) if err != nil { - return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + return nil, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) } return &ValiSeriesRequest{ Match: req.Groups, @@ -185,7 +185,7 @@ func (codec) DecodeRequest(_ context.Context, r *http.Request) (queryrange.Reque case LabelNamesOp: req, err := loghttp.ParseLabelQuery(r) if err != nil { - return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + return nil, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) } return &ValiLabelNamesRequest{ StartTs: *req.Start, @@ -193,7 +193,7 @@ func (codec) DecodeRequest(_ context.Context, r *http.Request) (queryrange.Reque Path: r.URL.Path, }, nil default: - return nil, httpgrpc.Errorf(http.StatusBadRequest, fmt.Sprintf("unknown request path: %s", r.URL.Path)) + return nil, httpgrpc.Errorf(http.StatusBadRequest, "unknown request path: %s", r.URL.Path) } } @@ -273,7 +273,7 @@ func (codec) EncodeRequest(ctx context.Context, r queryrange.Request) (*http.Req func (codec) DecodeResponse(ctx context.Context, r *http.Response, req queryrange.Request) (queryrange.Response, error) { if r.StatusCode/100 != 2 { body, _ := ioutil.ReadAll(r.Body) - return nil, httpgrpc.Errorf(r.StatusCode, string(body)) + return nil, httpgrpc.Errorf(r.StatusCode, "%s", string(body)) } sp, _ := opentracing.StartSpanFromContext(ctx, "codec.DecodeResponse") diff --git a/pkg/querier/queryrange/limits.go b/pkg/querier/queryrange/limits.go index 49a58e96..c1b05ac5 100644 --- a/pkg/querier/queryrange/limits.go +++ b/pkg/querier/queryrange/limits.go @@ -206,7 +206,7 @@ func (rt limitedRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) } userid, err := user.ExtractOrgID(ctx) if err != nil { - return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + return nil, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) } parallelism := rt.limits.MaxQueryParallelism(userid) @@ -255,7 +255,7 @@ func (rt limitedRoundTripper) do(ctx context.Context, r queryrange.Request) (que } if err := user.InjectOrgIDIntoHTTPRequest(ctx, request); err != nil { - return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + return nil, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) } response, err := rt.next.RoundTrip(request) diff --git a/pkg/querier/queryrange/roundtrip.go b/pkg/querier/queryrange/roundtrip.go index d944c8ec..e3175579 100644 --- a/pkg/querier/queryrange/roundtrip.go +++ b/pkg/querier/queryrange/roundtrip.go @@ -107,18 +107,18 @@ func newRoundTripper(next, log, metric, series, labels http.RoundTripper, limits func (r roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { err := req.ParseForm() if err != nil { - return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + return nil, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) } switch op := getOperation(req.URL.Path); op { case QueryRangeOp: rangeQuery, err := loghttp.ParseRangeQuery(req) if err != nil { - return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + return nil, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) } expr, err := logql.ParseExpr(rangeQuery.Query) if err != nil { - return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + return nil, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) } switch e := expr.(type) { case logql.SampleExpr: @@ -126,7 +126,7 @@ func (r roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { case logql.LogSelectorExpr: expr, err := transformRegexQuery(req, e) if err != nil { - return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + return nil, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) } if err := validateLimits(req, rangeQuery.Limit, r.limits); err != nil { return nil, err @@ -142,13 +142,13 @@ func (r roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { case SeriesOp: _, err := loghttp.ParseSeriesQuery(req) if err != nil { - return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + return nil, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) } return r.series.RoundTrip(req) case LabelNamesOp: _, err := loghttp.ParseLabelQuery(req) if err != nil { - return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + return nil, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) } return r.labels.RoundTrip(req) default: @@ -179,7 +179,7 @@ func transformRegexQuery(req *http.Request, expr logql.LogSelectorExpr) (logql.L func validateLimits(req *http.Request, reqLimit uint32, limits Limits) error { userID, err := user.ExtractOrgID(req.Context()) if err != nil { - return httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + return httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) } maxEntriesLimit := limits.MaxEntriesLimitPerQuery(userID) diff --git a/pkg/querier/queryrange/split_by_interval.go b/pkg/querier/queryrange/split_by_interval.go index 3feeb213..af2e5cac 100644 --- a/pkg/querier/queryrange/split_by_interval.go +++ b/pkg/querier/queryrange/split_by_interval.go @@ -161,7 +161,7 @@ func (h *splitByInterval) loop(ctx context.Context, ch <-chan *valiResult, next func (h *splitByInterval) Do(ctx context.Context, r queryrange.Request) (queryrange.Response, error) { userid, err := user.ExtractOrgID(ctx) if err != nil { - return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + return nil, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()) } interval := h.limits.QuerySplitDuration(userid) diff --git a/pkg/util/server/error_test.go b/pkg/util/server/error_test.go index cd9ffc4c..61d6e351 100644 --- a/pkg/util/server/error_test.go +++ b/pkg/util/server/error_test.go @@ -33,7 +33,7 @@ func Test_writeError(t *testing.T) { {"orgid", user.ErrNoOrgID, user.ErrNoOrgID.Error(), http.StatusBadRequest}, {"deadline", context.DeadlineExceeded, ErrDeadlineExceeded, http.StatusGatewayTimeout}, {"parse error", logql.ParseError{}, "parse error : ", http.StatusBadRequest}, - {"httpgrpc", httpgrpc.Errorf(http.StatusBadRequest, errors.New("foo").Error()), "foo", http.StatusBadRequest}, + {"httpgrpc", httpgrpc.Errorf(http.StatusBadRequest, "%s", errors.New("foo").Error()), "foo", http.StatusBadRequest}, {"internal", errors.New("foo"), "foo", http.StatusInternalServerError}, {"query error", chunk.ErrQueryMustContainMetricName, chunk.ErrQueryMustContainMetricName.Error(), http.StatusBadRequest}, {"wrapped query error", fmt.Errorf("wrapped: %w", chunk.ErrQueryMustContainMetricName), "wrapped: " + chunk.ErrQueryMustContainMetricName.Error(), http.StatusBadRequest}, diff --git a/pkg/util/server/middleware.go b/pkg/util/server/middleware.go index 6131e11a..a1072d51 100644 --- a/pkg/util/server/middleware.go +++ b/pkg/util/server/middleware.go @@ -14,7 +14,7 @@ func NewPrepopulateMiddleware() middleware.Interface { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { err := req.ParseForm() if err != nil { - WriteError(httpgrpc.Errorf(http.StatusBadRequest, err.Error()), w) + WriteError(httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()), w) return }