Skip to content

Commit fafd37d

Browse files
committed
stage new logging
2 parents 38ae0c5 + 27962db commit fafd37d

35 files changed

Lines changed: 898 additions & 154 deletions

.env.docker

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ MAX_RELEASE_CATCHER_MESSAGE_SIZE=5000000
99
LISTEN=0.0.0.0:3000
1010
RELEASE_EXCHANGE=release
1111
LOG_LEVEL=trace
12+
DEPLOYMENT_ENVIRONMENT=local
1213
METRICS_LISTEN=localhost:2112
14+
# OTEL_LOGS_ENDPOINT=http://host.docker.internal:4319/v1/logs
15+
OTEL_LOGS_ENDPOINT=http://otel:4318/v1/logs
1316

1417
## Hawk settings
1518
HAWK_ENABLED=true

.env.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ MAX_RELEASE_CATCHER_MESSAGE_SIZE=5000000
99
LISTEN=localhost:3000
1010
RELEASE_EXCHANGE=release
1111
LOG_LEVEL=trace
12+
DEPLOYMENT_ENVIRONMENT=local
1213
METRICS_LISTEN=localhost:2112
14+
OTEL_LOGS_ENDPOINT=http://otel:4318/v1/logs
1315

1416
## Hawk settings
1517
HAWK_ENABLED=true

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ venv
1616
.DS_Store
1717
bin/hawk.collector
1818
bin/golangci-lint
19-
.env
19+
.env
20+
21+
hawk.collector

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.16-stretch as builder
1+
FROM golang:1.24-bookworm as builder
22
ARG BUILD_DIRECTORY=/build
33

44
# disable CGO

cmd/collector/main.go

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package collector
22

33
import (
44
"context"
5-
"os"
5+
"fmt"
6+
"log/slog"
67

78
"github.com/codex-team/hawk.collector/pkg/accounts"
9+
log "github.com/codex-team/hawk.collector/pkg/logger"
810

911
"github.com/caarlos0/env/v6"
1012
"github.com/codex-team/hawk.collector/cmd"
@@ -15,7 +17,6 @@ import (
1517
"github.com/codex-team/hawk.collector/pkg/redis"
1618
"github.com/codex-team/hawk.collector/pkg/server"
1719
"github.com/joho/godotenv"
18-
log "github.com/sirupsen/logrus"
1920
)
2021

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

2829
// Execute Run server - Load configuration file and start server
2930
func (x *RunCommand) Execute(args []string) error {
30-
if err := godotenv.Load(); err != nil {
31-
log.Println("File .env not found, reading configuration from ENV")
31+
envLoadErr := godotenv.Load()
32+
var err error
33+
34+
// setup logging as early as possible so all logs go to stdout + OTEL
35+
otelShutdown := log.SetupFromEnv(context.Background())
36+
defer func() {
37+
if shutdownErr := otelShutdown(context.Background()); shutdownErr != nil {
38+
slog.Warn("failed to shutdown OTLP logger", "error", shutdownErr)
39+
}
40+
}()
41+
42+
if envLoadErr != nil {
43+
slog.Info("File .env not found, reading configuration from ENV")
3244
}
3345

3446
// load config from .env
3547
var cfg cmd.Config
3648
if err := env.Parse(&cfg); err != nil {
37-
log.Fatalf("Failed to parse ENV")
38-
}
39-
40-
// setup logging and set log level from config
41-
level, err := log.ParseLevel(cfg.LogLevel)
42-
if err != nil {
43-
level = log.ErrorLevel
49+
slog.Error("Failed to parse ENV", "error", err)
50+
return err
4451
}
45-
log.SetFormatter(&log.TextFormatter{
46-
FullTimestamp: true,
47-
})
48-
log.SetOutput(os.Stdout)
49-
log.SetLevel(level)
50-
log.Infof("✓ Log level set on %s", level)
52+
slog.InfoContext(context.Background(), fmt.Sprintf("collector started on %s", cfg.Listen), "event", "startup")
5153

5254
// Initialize Hawk Catcher
5355
if cfg.HawkEnabled {
5456
err = hawk.Init()
5557
if err != nil {
56-
log.Errorf("✗ Cannot initialize Hawk Catcher: %s", err)
58+
slog.Error("✗ Cannot initialize Hawk Catcher", "error", err)
5759
} else {
5860
go hawk.HawkCatcher.Run()
5961
defer hawk.HawkCatcher.Stop()
60-
log.Infof("✓ Hawk Catcher initialized on %s", hawk.HawkCatcher.GetURL())
62+
slog.Info("✓ Hawk Catcher initialized", "url", hawk.HawkCatcher.GetURL())
6163
}
6264
}
6365

6466
// connect to AMQP broker with retries
65-
log.Infof("Connecting to RabbitMQ %s", cfg.BrokerURL)
67+
slog.Info("Connecting to RabbitMQ", "url", cfg.BrokerURL)
6668
brokerObj := broker.New(cfg.BrokerURL, cfg.Exchange)
6769
brokerObj.Init()
68-
log.Infof("✓ Broker initialized on %s", cfg.BrokerURL)
70+
slog.Info("✓ Broker initialized", "url", cfg.BrokerURL)
6971

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

8587
err = redisClient.LoadBlockedIDs()
8688
if err != nil {
87-
log.Errorf("failed to load blocked IDs from Redis")
89+
slog.Error("failed to load blocked IDs from Redis", "error", err)
8890
}
8991

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

9496
err = accountsClient.UpdateTokenCache()
9597
if err != nil {
96-
log.Errorf("failed to update token cache: %s", err)
98+
slog.Error("failed to update token cache", "error", err)
9799
}
98100

99101
err = accountsClient.UpdateProjectsLimitsCache()
100102
if err != nil {
101-
log.Errorf("failed to update projects limits cache: %s", err)
103+
slog.Error("failed to update projects limits cache", "error", err)
102104
}
103105

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

117119
// listen and serve prometheus metrics
118120
go metrics.RunServer(cfg.MetricsListen)

cmd/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ type Config struct {
3434
// Log level
3535
LogLevel string `env:"LOG_LEVEL"`
3636

37+
// Deployment environment label (local/staging/production)
38+
DeploymentEnvironment string `env:"DEPLOYMENT_ENVIRONMENT"`
39+
40+
// OpenTelemetry configuration
41+
OTelLogsEndpoint string `env:"OTEL_LOGS_ENDPOINT"`
42+
ServiceName string `env:"SERVICE_NAME" defaultEnv:"collector"`
43+
3744
// Metrics listen host:port
3845
MetricsListen string `env:"METRICS_LISTEN"`
3946

cmd/error.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package cmd
22

33
import (
4-
"github.com/codex-team/hawk.collector/pkg/hawk"
5-
"log"
4+
"log/slog"
5+
"os"
66
"strings"
7+
8+
"github.com/codex-team/hawk.collector/pkg/hawk"
79
)
810

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

1517
if hawkError := hawk.HawkCatcher.Catch(err); hawkError != nil {
16-
log.Printf("Error in HawkCatcher.Catch: %s", hawkError)
18+
slog.Error("Error in HawkCatcher.Catch", "error", hawkError)
1719
}
1820

1921
hawk.HawkCatcher.Stop()
20-
log.Fatalf("%s: %s", strings.Join(msgs, ". "), err)
22+
slog.Error(strings.Join(msgs, ". "), "error", err)
23+
os.Exit(1)
2124
}
2225

2326
// PanicOnError - throw a recoverable panic

go.mod

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,73 @@ require (
77
github.com/cenkalti/backoff/v4 v4.1.0
88
github.com/codex-team/hawk.go v1.0.5
99
github.com/fasthttp/websocket v1.4.3
10-
github.com/go-redis/redis/v8 v8.8.3
11-
github.com/golang/protobuf v1.5.2 // indirect
10+
github.com/go-redis/redis/v8 v8.11.5
1211
github.com/jessevdk/go-flags v1.5.0
1312
github.com/joho/godotenv v1.3.0
1413
github.com/prometheus/client_golang v1.10.0
15-
github.com/prometheus/common v0.25.0 // indirect
16-
github.com/savsgio/gotils v0.0.0-20210520110740-c57c45b83e0a // indirect
17-
github.com/sirupsen/logrus v1.8.1
1814
github.com/streadway/amqp v1.0.0
19-
github.com/stretchr/testify v1.7.0
15+
github.com/stretchr/testify v1.11.1
2016
github.com/tidwall/gjson v1.8.0
2117
github.com/valyala/fasthttp v1.25.0
2218
go.mongodb.org/mongo-driver v1.7.1
19+
go.opentelemetry.io/otel v1.39.0
20+
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.15.0
21+
go.opentelemetry.io/otel/log v0.14.0
22+
go.opentelemetry.io/otel/sdk v1.39.0
23+
go.opentelemetry.io/otel/sdk/log v0.14.0
24+
)
25+
26+
require (
27+
github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302 // indirect
28+
github.com/beorn7/perks v1.0.1 // indirect
29+
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
30+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
31+
github.com/davecgh/go-spew v1.1.1 // indirect
32+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
33+
github.com/go-logr/logr v1.4.3 // indirect
34+
github.com/go-logr/stdr v1.2.2 // indirect
35+
github.com/go-stack/stack v1.8.0 // indirect
36+
github.com/gobwas/httphead v0.1.0 // indirect
37+
github.com/gobwas/pool v0.2.1 // indirect
38+
github.com/gobwas/ws v1.0.4 // indirect
39+
github.com/golang/protobuf v1.5.4 // indirect
40+
github.com/golang/snappy v0.0.3 // indirect
41+
github.com/google/uuid v1.6.0 // indirect
42+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.3 // indirect
43+
github.com/josharian/intern v1.0.0 // indirect
44+
github.com/klauspost/compress v1.12.2 // indirect
45+
github.com/mailru/easyjson v0.7.7 // indirect
46+
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
47+
github.com/pkg/errors v0.9.1 // indirect
48+
github.com/pmezard/go-difflib v1.0.0 // indirect
49+
github.com/prometheus/client_model v0.2.0 // indirect
50+
github.com/prometheus/common v0.25.0 // indirect
51+
github.com/prometheus/procfs v0.6.0 // indirect
52+
github.com/savsgio/gotils v0.0.0-20210520110740-c57c45b83e0a // indirect
53+
github.com/tidwall/match v1.0.3 // indirect
54+
github.com/tidwall/pretty v1.1.0 // indirect
55+
github.com/valyala/bytebufferpool v1.0.0 // indirect
56+
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
57+
github.com/xdg-go/scram v1.0.2 // indirect
58+
github.com/xdg-go/stringprep v1.0.2 // indirect
59+
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
60+
github.com/yuin/gopher-lua v1.1.1 // indirect
61+
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
62+
go.opentelemetry.io/otel/metric v1.39.0 // indirect
63+
go.opentelemetry.io/otel/trace v1.39.0 // indirect
64+
go.opentelemetry.io/proto/otlp v1.9.0 // indirect
65+
golang.org/x/crypto v0.44.0 // indirect
66+
golang.org/x/net v0.47.0 // indirect
67+
golang.org/x/sync v0.18.0 // indirect
68+
golang.org/x/sys v0.39.0 // indirect
69+
golang.org/x/text v0.31.0 // indirect
70+
google.golang.org/genproto/googleapis/api v0.0.0-20260120221211-b8f7ae30c516 // indirect
71+
google.golang.org/genproto/googleapis/rpc v0.0.0-20260120221211-b8f7ae30c516 // indirect
72+
google.golang.org/grpc v1.77.0 // indirect
73+
google.golang.org/protobuf v1.36.11 // indirect
74+
gopkg.in/yaml.v3 v3.0.1 // indirect
2375
)
2476

25-
go 1.16
77+
go 1.24.0
78+
79+
toolchain go1.24.12

0 commit comments

Comments
 (0)