Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ MAX_RELEASE_CATCHER_MESSAGE_SIZE=5000000
LISTEN=0.0.0.0:3000
RELEASE_EXCHANGE=release
LOG_LEVEL=trace
DEPLOYMENT_ENVIRONMENT=local
METRICS_LISTEN=localhost:2112
# OTEL_LOGS_ENDPOINT=http://host.docker.internal:4319/v1/logs
OTEL_LOGS_ENDPOINT=http://otel:4318/v1/logs

## Hawk settings
HAWK_ENABLED=true
Expand Down
2 changes: 2 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ MAX_RELEASE_CATCHER_MESSAGE_SIZE=5000000
LISTEN=localhost:3000
RELEASE_EXCHANGE=release
LOG_LEVEL=trace
DEPLOYMENT_ENVIRONMENT=local
METRICS_LISTEN=localhost:2112
OTEL_LOGS_ENDPOINT=http://otel:4318/v1/logs

## Hawk settings
HAWK_ENABLED=true
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ venv
.DS_Store
bin/hawk.collector
bin/golangci-lint
.env
.env

hawk.collector
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.16-stretch as builder
FROM golang:1.24-bookworm as builder
ARG BUILD_DIRECTORY=/build

# disable CGO
Expand Down
54 changes: 28 additions & 26 deletions cmd/collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package collector

import (
"context"
"os"
"fmt"
"log/slog"

"github.com/codex-team/hawk.collector/pkg/accounts"
log "github.com/codex-team/hawk.collector/pkg/logger"

"github.com/caarlos0/env/v6"
"github.com/codex-team/hawk.collector/cmd"
Expand All @@ -15,7 +17,6 @@ import (
"github.com/codex-team/hawk.collector/pkg/redis"
"github.com/codex-team/hawk.collector/pkg/server"
"github.com/joho/godotenv"
log "github.com/sirupsen/logrus"
)

// RunCommand - Run server in the production mode
Expand All @@ -27,48 +28,49 @@ type RunCommand struct {

// Execute Run server - Load configuration file and start server
func (x *RunCommand) Execute(args []string) error {
if err := godotenv.Load(); err != nil {
log.Println("File .env not found, reading configuration from ENV")
envLoadErr := godotenv.Load()
var err error

// setup logging as early as possible so all logs go to stdout + OTEL
otelShutdown := log.SetupFromEnv(context.Background())
defer func() {
if shutdownErr := otelShutdown(context.Background()); shutdownErr != nil {
slog.Warn("failed to shutdown OTLP logger", "error", shutdownErr)
}
}()

if envLoadErr != nil {
slog.Info("File .env not found, reading configuration from ENV")
}

// load config from .env
var cfg cmd.Config
if err := env.Parse(&cfg); err != nil {
log.Fatalf("Failed to parse ENV")
}

// setup logging and set log level from config
level, err := log.ParseLevel(cfg.LogLevel)
if err != nil {
level = log.ErrorLevel
slog.Error("Failed to parse ENV", "error", err)
return err
}
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
log.SetOutput(os.Stdout)
log.SetLevel(level)
log.Infof("✓ Log level set on %s", level)
slog.InfoContext(context.Background(), fmt.Sprintf("collector started on %s", cfg.Listen), "event", "startup")

// Initialize Hawk Catcher
if cfg.HawkEnabled {
err = hawk.Init()
if err != nil {
log.Errorf("✗ Cannot initialize Hawk Catcher: %s", err)
slog.Error("✗ Cannot initialize Hawk Catcher", "error", err)
} else {
go hawk.HawkCatcher.Run()
defer hawk.HawkCatcher.Stop()
log.Infof("✓ Hawk Catcher initialized on %s", hawk.HawkCatcher.GetURL())
slog.Info("✓ Hawk Catcher initialized", "url", hawk.HawkCatcher.GetURL())
}
}

// connect to AMQP broker with retries
log.Infof("Connecting to RabbitMQ %s", cfg.BrokerURL)
slog.Info("Connecting to RabbitMQ", "url", cfg.BrokerURL)
brokerObj := broker.New(cfg.BrokerURL, cfg.Exchange)
brokerObj.Init()
log.Infof("✓ Broker initialized on %s", cfg.BrokerURL)
slog.Info("✓ Broker initialized", "url", cfg.BrokerURL)

// connect to Redis
log.Infof("Connecting to Redis %s", cfg.RedisURL)
slog.Info("Connecting to Redis", "url", cfg.RedisURL)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
Expand All @@ -84,7 +86,7 @@ func (x *RunCommand) Execute(args []string) error {

err = redisClient.LoadBlockedIDs()
if err != nil {
log.Errorf("failed to load blocked IDs from Redis")
slog.Error("failed to load blocked IDs from Redis", "error", err)
}

// connect to accounts MongoDB
Expand All @@ -93,12 +95,12 @@ func (x *RunCommand) Execute(args []string) error {

err = accountsClient.UpdateTokenCache()
if err != nil {
log.Errorf("failed to update token cache: %s", err)
slog.Error("failed to update token cache", "error", err)
}

err = accountsClient.UpdateProjectsLimitsCache()
if err != nil {
log.Errorf("failed to update projects limits cache: %s", err)
slog.Error("failed to update projects limits cache", "error", err)
}

go periodic.RunPeriodically(accountsClient.UpdateTokenCache, cfg.TokenUpdatePeriod, doneAccountsContext)
Expand All @@ -112,7 +114,7 @@ func (x *RunCommand) Execute(args []string) error {
go periodic.RunPeriodically(redisClient.LoadBlockedIDs, cfg.BlockedIDsLoad, done)
go periodic.RunPeriodically(serverObj.UpdateBlacklist, cfg.BlacklistUpdatePeriod, done)
defer close(done)
log.Info("✓ Redis client initialized")
slog.Info("✓ Redis client initialized")

// listen and serve prometheus metrics
go metrics.RunServer(cfg.MetricsListen)
Expand Down
7 changes: 7 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ type Config struct {
// Log level
LogLevel string `env:"LOG_LEVEL"`

// Deployment environment label (local/staging/production)
DeploymentEnvironment string `env:"DEPLOYMENT_ENVIRONMENT"`

// OpenTelemetry configuration
OTelLogsEndpoint string `env:"OTEL_LOGS_ENDPOINT"`
ServiceName string `env:"SERVICE_NAME" defaultEnv:"collector"`

Copilot AI Feb 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The env tag should use envDefault not defaultEnv for the caarlos0/env package. The correct tag is env:"SERVICE_NAME" envDefault:"collector". With the current incorrect tag, the default value will not be applied if the environment variable is not set.

Copilot uses AI. Check for mistakes.

// Metrics listen host:port
MetricsListen string `env:"METRICS_LISTEN"`

Expand Down
11 changes: 7 additions & 4 deletions cmd/error.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd

import (
"github.com/codex-team/hawk.collector/pkg/hawk"
"log"
"log/slog"
"os"
"strings"

"github.com/codex-team/hawk.collector/pkg/hawk"
)

// FailOnError - throw fatal error and log it with the message provided by the msg argument if err is not nil
Expand All @@ -13,11 +15,12 @@ func FailOnError(err error, msgs ...string) {
}

if hawkError := hawk.HawkCatcher.Catch(err); hawkError != nil {
log.Printf("Error in HawkCatcher.Catch: %s", hawkError)
slog.Error("Error in HawkCatcher.Catch", "error", hawkError)
}

hawk.HawkCatcher.Stop()
log.Fatalf("%s: %s", strings.Join(msgs, ". "), err)
slog.Error(strings.Join(msgs, ". "), "error", err)
os.Exit(1)
}

// PanicOnError - throw a recoverable panic
Expand Down
68 changes: 61 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,73 @@ require (
github.com/cenkalti/backoff/v4 v4.1.0
github.com/codex-team/hawk.go v1.0.5
github.com/fasthttp/websocket v1.4.3
github.com/go-redis/redis/v8 v8.8.3
github.com/golang/protobuf v1.5.2 // indirect
github.com/go-redis/redis/v8 v8.11.5
github.com/jessevdk/go-flags v1.5.0
github.com/joho/godotenv v1.3.0
github.com/prometheus/client_golang v1.10.0
github.com/prometheus/common v0.25.0 // indirect
github.com/savsgio/gotils v0.0.0-20210520110740-c57c45b83e0a // indirect
github.com/sirupsen/logrus v1.8.1
github.com/streadway/amqp v1.0.0
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.11.1
github.com/tidwall/gjson v1.8.0
github.com/valyala/fasthttp v1.25.0
go.mongodb.org/mongo-driver v1.7.1
go.opentelemetry.io/otel v1.39.0
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.15.0
go.opentelemetry.io/otel/log v0.14.0
go.opentelemetry.io/otel/sdk v1.39.0
go.opentelemetry.io/otel/sdk/log v0.14.0
)

require (
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.0.4 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.3 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.12.2 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.25.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/savsgio/gotils v0.0.0-20210520110740-c57c45b83e0a // indirect
github.com/tidwall/match v1.0.3 // indirect
github.com/tidwall/pretty v1.1.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.0.2 // indirect
github.com/xdg-go/stringprep v1.0.2 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
github.com/yuin/gopher-lua v1.1.1 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/otel/metric v1.39.0 // indirect
go.opentelemetry.io/otel/trace v1.39.0 // indirect
go.opentelemetry.io/proto/otlp v1.9.0 // indirect
golang.org/x/crypto v0.44.0 // indirect
golang.org/x/net v0.47.0 // indirect
golang.org/x/sync v0.18.0 // indirect
golang.org/x/sys v0.39.0 // indirect
golang.org/x/text v0.31.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260120221211-b8f7ae30c516 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260120221211-b8f7ae30c516 // indirect
google.golang.org/grpc v1.77.0 // indirect
google.golang.org/protobuf v1.36.11 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

go 1.16
go 1.24.0

toolchain go1.24.12
Loading
Loading