diff --git a/.github/workflows/gha-go-test.yaml b/.github/workflows/gha-go-test.yaml index 38aeecb1..9800de64 100644 --- a/.github/workflows/gha-go-test.yaml +++ b/.github/workflows/gha-go-test.yaml @@ -10,7 +10,11 @@ jobs: uses: actions/setup-go@v3 with: go-version: 1.19 - - name: test + - name: Run staticcheck + run: | + go install honnef.co/go/tools/cmd/staticcheck@latest + make static + - name: Run test run: make test - - name: test-race + - name: Run test-race run: make race diff --git a/Makefile b/Makefile index 0c396fb0..b20e77ef 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,6 @@ +.PHONY: static +static: + staticcheck ./... .PHONY: test test: go test ./... diff --git a/config/config.go b/config/config.go index a5dbc4db..6fa1dfb2 100644 --- a/config/config.go +++ b/config/config.go @@ -6,10 +6,10 @@ import ( "gopkg.in/yaml.v2" "log" "os" + + "github.com/artie-labs/reader/constants" ) -const ctxKey = "_cfg" - type Kafka struct { BootstrapServers string `yaml:"bootstrapServers"` TopicPrefix string `yaml:"topicPrefix"` @@ -104,11 +104,11 @@ func ReadConfig(fp string) (*Settings, error) { } func InjectIntoContext(ctx context.Context, settings *Settings) context.Context { - return context.WithValue(ctx, ctxKey, settings) + return context.WithValue(ctx, constants.ConfigKey, settings) } func FromContext(ctx context.Context) *Settings { - val := ctx.Value(ctxKey) + val := ctx.Value(constants.ConfigKey) if val == nil { return nil } diff --git a/constants/constants.go b/constants/constants.go new file mode 100644 index 00000000..1ecabeb4 --- /dev/null +++ b/constants/constants.go @@ -0,0 +1,9 @@ +package constants + +type contextKey string + +const ( + ConfigKey contextKey = "__cfg" + KafkaKey contextKey = "__kafka" + LoggerKey contextKey = "__logger" +) diff --git a/lib/kafkalib/batch.go b/lib/kafkalib/batch.go index 7d542fa2..dc372318 100644 --- a/lib/kafkalib/batch.go +++ b/lib/kafkalib/batch.go @@ -15,7 +15,7 @@ const ( RetryDelayMs = 250 ) -var BatchEmptyErr = fmt.Errorf("batch is empty") +var ErrEmptyBatch = fmt.Errorf("batch is empty") type Batch struct { msgs []kafka.Message @@ -25,7 +25,7 @@ type Batch struct { func (b *Batch) IsValid() error { if len(b.msgs) == 0 { - return BatchEmptyErr + return ErrEmptyBatch } if b.chunkSize < 1 { diff --git a/lib/kafkalib/kafka.go b/lib/kafkalib/kafka.go index 68a8a3ee..8222d2f7 100644 --- a/lib/kafkalib/kafka.go +++ b/lib/kafkalib/kafka.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "github.com/artie-labs/reader/config" + "github.com/artie-labs/reader/constants" "github.com/artie-labs/reader/lib/logger" awsCfg "github.com/aws/aws-sdk-go-v2/config" "github.com/segmentio/kafka-go" @@ -12,14 +13,11 @@ import ( "time" ) -const ( - ctxKey = "_kafka" - maxKafkaItemBytes = 4000000 -) +const maxKafkaItemBytes = 4000000 func FromContext(ctx context.Context) *kafka.Writer { log := logger.FromContext(ctx) - kafkaVal := ctx.Value(ctxKey) + kafkaVal := ctx.Value(constants.KafkaKey) if kafkaVal == nil { log.Fatal("kafka is not set in context.Context") } @@ -64,5 +62,5 @@ func InjectIntoContext(ctx context.Context) context.Context { } } - return context.WithValue(ctx, ctxKey, writer) + return context.WithValue(ctx, constants.KafkaKey, writer) } diff --git a/lib/logger/logger.go b/lib/logger/logger.go index 81fba593..49a58e72 100644 --- a/lib/logger/logger.go +++ b/lib/logger/logger.go @@ -2,20 +2,20 @@ package logger import ( "context" - "github.com/artie-labs/reader/config" "github.com/evalphobia/logrus_sentry" "github.com/sirupsen/logrus" "os" -) -const loggerKey = "_log" + "github.com/artie-labs/reader/config" + "github.com/artie-labs/reader/constants" +) func InjectLoggerIntoCtx(ctx context.Context) context.Context { - return context.WithValue(ctx, loggerKey, initLogger(config.FromContext(ctx))) + return context.WithValue(ctx, constants.LoggerKey, initLogger(config.FromContext(ctx))) } func FromContext(ctx context.Context) *logrus.Logger { - logVal := ctx.Value(loggerKey) + logVal := ctx.Value(constants.LoggerKey) if logVal == nil { return FromContext(InjectLoggerIntoCtx(ctx)) } diff --git a/sources/dynamodb/shard.go b/sources/dynamodb/shard.go index c473e3b2..62028d2d 100644 --- a/sources/dynamodb/shard.go +++ b/sources/dynamodb/shard.go @@ -124,6 +124,4 @@ func (s *Store) processShard(ctx context.Context, shard *dynamodbstreams.Shard) s.storage.SetShardProcessed(*shard.ShardId) } } - - return }