Skip to content

Commit

Permalink
chore: pkg import only once (#5644)
Browse files Browse the repository at this point in the history
* chore: pkg import only once

Signed-off-by: guoguangwu <guoguangwu@magic-shield.com>

* fix: lint

Signed-off-by: guoguangwu <guoguangwu@magic-shield.com>

---------

Signed-off-by: guoguangwu <guoguangwu@magic-shield.com>
  • Loading branch information
testwill committed Nov 10, 2023
1 parent e09002d commit 6a3a571
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 26 deletions.
3 changes: 1 addition & 2 deletions pkg/configs/db/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import (
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres" // Import the postgres migrations driver
_ "github.com/golang-migrate/migrate/v4/source/file" // Import the postgres migrations driver
"github.com/lib/pq"
_ "github.com/lib/pq" // Import the postgres sql driver
"github.com/lib/pq" // Import the postgres sql driver
"github.com/pkg/errors"
)

Expand Down
3 changes: 1 addition & 2 deletions pkg/cortexpb/compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cortexpb

import (
"encoding/json"
stdlibjson "encoding/json"
"math"
"testing"
"unsafe"
Expand All @@ -21,7 +20,7 @@ func TestJsoniterMarshalForSample(t *testing.T) {
}

func TestStdlibJsonMarshalForSample(t *testing.T) {
testMarshalling(t, stdlibjson.Marshal, "json: error calling MarshalJSON for type cortexpb.Sample: test sample")
testMarshalling(t, json.Marshal, "json: error calling MarshalJSON for type cortexpb.Sample: test sample")
}

func testMarshalling(t *testing.T, marshalFn func(v interface{}) ([]byte, error), expectedError string) {
Expand Down
9 changes: 4 additions & 5 deletions pkg/querier/batch/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package batch
import (
"github.com/cortexproject/cortex/pkg/chunk"
"github.com/cortexproject/cortex/pkg/chunk/encoding"
promchunk "github.com/cortexproject/cortex/pkg/chunk/encoding"
"github.com/cortexproject/cortex/pkg/querier/iterators"

"github.com/prometheus/common/model"
Expand Down Expand Up @@ -48,7 +47,7 @@ type iterator interface {

// Batch returns the current batch. Must only be called after Seek or Next
// have returned true.
Batch() promchunk.Batch
Batch() encoding.Batch

Err() error
}
Expand All @@ -74,7 +73,7 @@ func NewGenericChunkMergeIterator(chunks []GenericChunk) chunkenc.Iterator {
// call to Next; on calls to Seek, resets batch size to 1.
type iteratorAdapter struct {
batchSize int
curr promchunk.Batch
curr encoding.Batch
underlying iterator
}

Expand Down Expand Up @@ -130,8 +129,8 @@ func (a *iteratorAdapter) Next() bool {
for a.curr.Index >= a.curr.Length && a.underlying.Next(a.batchSize) {
a.curr = a.underlying.Batch()
a.batchSize = a.batchSize * 2
if a.batchSize > promchunk.BatchSize {
a.batchSize = promchunk.BatchSize
if a.batchSize > encoding.BatchSize {
a.batchSize = encoding.BatchSize
}
}
return a.curr.Index < a.curr.Length
Expand Down
5 changes: 2 additions & 3 deletions pkg/querier/worker/frontend_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"google.golang.org/grpc"

"github.com/cortexproject/cortex/pkg/frontend/v1/frontendv1pb"
"github.com/cortexproject/cortex/pkg/querier/stats"
querier_stats "github.com/cortexproject/cortex/pkg/querier/stats"
"github.com/cortexproject/cortex/pkg/util/backoff"
util_log "github.com/cortexproject/cortex/pkg/util/log"
Expand Down Expand Up @@ -101,7 +100,7 @@ func (fp *frontendProcessor) process(c frontendv1pb.Frontend_ProcessClient) erro
// and cancel the query. We don't actually handle queries in parallel
// here, as we're running in lock step with the server - each Recv is
// paired with a Send.
go fp.runRequest(ctx, request.HttpRequest, request.StatsEnabled, func(response *httpgrpc.HTTPResponse, stats *stats.QueryStats) error {
go fp.runRequest(ctx, request.HttpRequest, request.StatsEnabled, func(response *httpgrpc.HTTPResponse, stats *querier_stats.QueryStats) error {
return c.Send(&frontendv1pb.ClientToFrontend{
HttpResponse: response,
Stats: stats,
Expand All @@ -120,7 +119,7 @@ func (fp *frontendProcessor) process(c frontendv1pb.Frontend_ProcessClient) erro
}
}

func (fp *frontendProcessor) runRequest(ctx context.Context, request *httpgrpc.HTTPRequest, statsEnabled bool, sendHTTPResponse func(response *httpgrpc.HTTPResponse, stats *stats.QueryStats) error) {
func (fp *frontendProcessor) runRequest(ctx context.Context, request *httpgrpc.HTTPRequest, statsEnabled bool, sendHTTPResponse func(response *httpgrpc.HTTPResponse, stats *querier_stats.QueryStats) error) {
var stats *querier_stats.QueryStats
if statsEnabled {
stats, ctx = querier_stats.ContextWithEmptyStats(ctx)
Expand Down
9 changes: 4 additions & 5 deletions pkg/ring/lifecycler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/pkg/errors"
perrors "github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/atomic"

Expand Down Expand Up @@ -419,7 +418,7 @@ func (i *Lifecycler) loop(ctx context.Context) error {
// First, see if we exist in the cluster, update our state to match if we do,
// and add ourselves (without tokens) if we don't.
if err := i.initRing(context.Background()); err != nil {
return perrors.Wrapf(err, "failed to join the ring %s", i.RingName)
return errors.Wrapf(err, "failed to join the ring %s", i.RingName)
}

// We do various period tasks
Expand Down Expand Up @@ -464,14 +463,14 @@ func (i *Lifecycler) loop(ctx context.Context) error {
// let's observe the ring. By using JOINING state, this ingester will be ignored by LEAVING
// ingesters, but we also signal that it is not fully functional yet.
if err := i.autoJoin(context.Background(), JOINING); err != nil {
return perrors.Wrapf(err, "failed to pick tokens in the KV store, ring: %s", i.RingName)
return errors.Wrapf(err, "failed to pick tokens in the KV store, ring: %s", i.RingName)
}

level.Info(i.logger).Log("msg", "observing tokens before going ACTIVE", "ring", i.RingName)
observeChan = time.After(i.cfg.ObservePeriod)
} else {
if err := i.autoJoin(context.Background(), ACTIVE); err != nil {
return perrors.Wrapf(err, "failed to pick tokens in the KV store, ring: %s", i.RingName)
return errors.Wrapf(err, "failed to pick tokens in the KV store, ring: %s", i.RingName)
}
}
}
Expand Down Expand Up @@ -561,7 +560,7 @@ heartbeatLoop:

if i.ShouldUnregisterOnShutdown() {
if err := i.unregister(context.Background()); err != nil {
return perrors.Wrapf(err, "failed to unregister from the KV store, ring: %s", i.RingName)
return errors.Wrapf(err, "failed to unregister from the KV store, ring: %s", i.RingName)
}
level.Info(i.logger).Log("msg", "instance removed from the KV store", "ring", i.RingName)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/util/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"

"github.com/go-kit/log"
kitlog "github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/weaveworks/common/logging"
Expand All @@ -27,7 +26,7 @@ var (
// Logger is a shared go-kit logger.
// TODO: Change all components to take a non-global logger via their constructors.
// Prefer accepting a non-global logger as an argument.
Logger = kitlog.NewNopLogger()
Logger = log.NewNopLogger()

logMessages = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "log_messages_total",
Expand Down
13 changes: 6 additions & 7 deletions pkg/util/log/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@ import (
"context"

"github.com/go-kit/log"
kitlog "github.com/go-kit/log"
"github.com/weaveworks/common/tracing"

"github.com/cortexproject/cortex/pkg/tenant"
)

// WithUserID returns a Logger that has information about the current user in
// its details.
func WithUserID(userID string, l kitlog.Logger) kitlog.Logger {
func WithUserID(userID string, l log.Logger) log.Logger {
// See note in WithContext.
return kitlog.With(l, "org_id", userID)
return log.With(l, "org_id", userID)
}

// WithTraceID returns a Logger that has information about the traceID in
// its details.
func WithTraceID(traceID string, l kitlog.Logger) kitlog.Logger {
func WithTraceID(traceID string, l log.Logger) log.Logger {
// See note in WithContext.
return kitlog.With(l, "traceID", traceID)
return log.With(l, "traceID", traceID)
}

// WithContext returns a Logger that has information about the current user in
Expand All @@ -31,7 +30,7 @@ func WithTraceID(traceID string, l kitlog.Logger) kitlog.Logger {
//
// log := util.WithContext(ctx)
// log.Errorf("Could not chunk chunks: %v", err)
func WithContext(ctx context.Context, l kitlog.Logger) kitlog.Logger {
func WithContext(ctx context.Context, l log.Logger) log.Logger {
l = headersFromContext(ctx, l)

// Weaveworks uses "orgs" and "orgID" to represent Cortex users,
Expand Down Expand Up @@ -59,7 +58,7 @@ func WithSourceIPs(sourceIPs string, l log.Logger) log.Logger {
func headersFromContext(ctx context.Context, l log.Logger) log.Logger {
headerContentsMap := HeaderMapFromContext(ctx)
for header, contents := range headerContentsMap {
l = kitlog.With(l, header, contents)
l = log.With(l, header, contents)
}
return l
}

0 comments on commit 6a3a571

Please sign in to comment.