Skip to content

Commit

Permalink
refactor(logging): use zerolog (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
chronark authored Sep 16, 2023
1 parent bc64410 commit 2dacc58
Show file tree
Hide file tree
Showing 21 changed files with 197 additions and 178 deletions.
58 changes: 28 additions & 30 deletions apps/agent/cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ import (
"github.com/unkeyed/unkey/apps/agent/pkg/server"
"github.com/unkeyed/unkey/apps/agent/pkg/tracing"
"github.com/unkeyed/unkey/apps/agent/pkg/version"
"go.uber.org/zap"
)

type features struct {
enableAxiom bool
analytics string
eventBus string
prewarm bool
verbose bool
}

var runtimeConfig features
Expand All @@ -46,6 +46,7 @@ func init() {
AgentCmd.Flags().StringVar(&runtimeConfig.analytics, "analytics", "", "Send analytics to a backend, available: ['tinybird']")
AgentCmd.Flags().StringVar(&runtimeConfig.eventBus, "event-bus", "", "Use a message bus for communication between nodes, available: ['kafka']")
AgentCmd.Flags().BoolVar(&runtimeConfig.prewarm, "prewarm", false, "Load all keys from the db to memory on boot")
AgentCmd.Flags().BoolVarP(&runtimeConfig.verbose, "verbose", "v", false, "Print debug logs")
}

// AgentCmd represents the agent command
Expand All @@ -55,36 +56,32 @@ var AgentCmd = &cobra.Command{
Long: ``,
Run: func(cmd *cobra.Command, args []string) {

logger := logging.New().With(
zap.String("version", version.Version),
)
logger := logging.New(runtimeConfig.verbose).With().Str("version", version.Version).Logger()

defer func() {
// this is best effort and can error quite frequently
_ = logger.Sync()
}()
logger.Info("Starting Unkey Agent", zap.Any("runtimeConfig", runtimeConfig))
logger.Info().Any("runtimeConfig", runtimeConfig).Msg("Starting Unkey Agent")

e := env.Env{
ErrorHandler: func(err error) { logger.Fatal("unable to load environment variable", zap.Error(err)) },
ErrorHandler: func(err error) {
logger.Fatal().Err(err).Msg("unable to load environment variable")
},
}
region := e.String("FLY_REGION", "local")
allocId := e.String("FLY_ALLOC_ID", "")
logger = logger.With(zap.String("region", region))
logger = logger.With().Str("region", region).Logger()
if allocId != "" {
logger = logger.With(zap.String("allocId", allocId))
logger = logger.With().Str("allocId", allocId).Logger()
}

metrics := metricsPkg.NewNoop()
if runtimeConfig.enableAxiom {
realMetrics, err := metricsPkg.New(metricsPkg.Config{
AxiomOrgId: e.String("AXIOM_ORG_ID"),
AxiomToken: e.String("AXIOM_TOKEN"),
Logger: logger.With(zap.String("pkg", "metrics")),
Logger: logger.With().Str("pkg", "metrics").Logger(),
Region: region,
})
if err != nil {
logger.Fatal("unable to start metrics", zap.Error(err))
logger.Fatal().Err(err).Msg("unable to start metrics")
}
metrics = realMetrics
}
Expand All @@ -103,16 +100,16 @@ var AgentCmd = &cobra.Command{
AxiomToken: e.String("AXIOM_TOKEN"),
})
if err != nil {
logger.Fatal("unable to start tracer", zap.Error(err))
logger.Fatal().Err(err).Msg("unable to start tracer")
}
defer func() {
err := closeTracer()
if err != nil {
logger.Fatal("unable to close tracer", zap.Error(err))
logger.Fatal().Err(err).Msg("unable to close tracer")
}
}()
tracer = t
logger.Info("Axiom tracing enabled")
logger.Info().Msg("Axiom tracing enabled")
}
}

Expand All @@ -128,7 +125,7 @@ var AgentCmd = &cobra.Command{
database.WithTracing(tracer),
)
if err != nil {
logger.Fatal("Failed to connect to database", zap.Error(err))
logger.Fatal().Err(err).Msg("Failed to connect to database")
}
defer db.Close()
// Setup Analytics
Expand Down Expand Up @@ -160,7 +157,7 @@ var AgentCmd = &cobra.Command{
Password: e.String("KAFKA_PASSWORD"),
})
if err != nil {
logger.Fatal("unable to start kafka", zap.Error(err))
logger.Fatal().Err(err).Msg("unable to start kafka")
}

k.Start()
Expand All @@ -177,7 +174,7 @@ var AgentCmd = &cobra.Command{
RedisUrl: redisUrl,
})
if err != nil {
logger.Fatal("unable to start redis ratelimiting", zap.Error(err))
logger.Fatal().Err(err).Msg("unable to start redis ratelimiting")
}
}

Expand All @@ -188,12 +185,12 @@ var AgentCmd = &cobra.Command{
RefreshFromOrigin: func(ctx context.Context, keyHash string) (entities.Key, bool) {
key, found, err := db.FindKeyByHash(ctx, keyHash)
if err != nil {
logger.Error("unable to refresh key by hash", zap.Error(err))
logger.Err(err).Msg("unable to refresh key by hash")
return entities.Key{}, false
}
return key, found
},
Logger: logger.With(zap.String("cacheType", "key")),
Logger: logger.With().Str("cacheType", "key").Logger(),
Metrics: metrics,
Resource: "key",
})
Expand All @@ -207,12 +204,12 @@ var AgentCmd = &cobra.Command{
RefreshFromOrigin: func(ctx context.Context, apiId string) (entities.Api, bool) {
key, found, err := db.FindApi(ctx, apiId)
if err != nil {
logger.Error("unable to refresh api by id", zap.Error(err))
logger.Err(err).Msg("unable to refresh api by id")
return entities.Api{}, false
}
return key, found
},
Logger: logger.With(zap.String("cacheType", "api")),
Logger: logger.With().Str("cacheType", "api").Logger(),
Metrics: metrics,
Resource: "api",
})
Expand All @@ -222,12 +219,12 @@ var AgentCmd = &cobra.Command{
eventBus.OnKeyEvent(func(ctx context.Context, e events.KeyEvent) error {

if e.Type == events.KeyDeleted {
logger.Info("evicting from cache", zap.String("keyId", e.Key.Id), zap.String("keyHash", e.Key.Hash))
logger.Debug().Str("keyId", e.Key.Id).Str("keyHash", e.Key.Hash).Msg("evicting from cache")
keyCache.Remove(context.Background(), e.Key.Hash)
return nil
}

logger.Info("precaching key", zap.String("keyId", e.Key.Id), zap.String("keyHash", e.Key.Hash))
logger.Debug().Str("keyId", e.Key.Id).Str("keyHash", e.Key.Hash).Msg("precaching key")
key, found, err := db.FindKeyById(ctx, e.Key.Id)
if err != nil {
return fmt.Errorf("unable to get key by id: %s: %w", e.Key.Id, err)
Expand All @@ -236,7 +233,7 @@ var AgentCmd = &cobra.Command{
return nil
}
keyCache.Set(ctx, key.Hash, key)
logger.Info("precaching api", zap.String("keyAuthId", key.KeyAuthId))
logger.Debug().Str("keyAuthId", key.KeyAuthId).Msg("precaching api")
api, found, err := db.FindApiByKeyAuthId(ctx, key.KeyAuthId)
if err != nil {
return fmt.Errorf("unable to find api by keyAuthId: %s: %w", key.KeyAuthId, err)
Expand All @@ -262,7 +259,7 @@ var AgentCmd = &cobra.Command{
go func() {
err := cacheWarmer.Run(context.Background())
if err != nil {
logger.Error("unable to warm cache", zap.Error(err))
logger.Err(err).Msg("unable to warm cache")
}
}()
}
Expand Down Expand Up @@ -292,12 +289,13 @@ var AgentCmd = &cobra.Command{
EventBus: eventBus,
Version: version.Version,
WorkspaceService: workspaceService,
Metrics: metrics,
})

go func() {
err = srv.Start(fmt.Sprintf("0.0.0.0:%s", e.String("PORT", "8080")))
if err != nil {
logger.Fatal("Failed to run service", zap.Error(err))
logger.Fatal().Err(err).Msg("Failed to run service")
}
}()
defer srv.Close()
Expand All @@ -307,6 +305,6 @@ var AgentCmd = &cobra.Command{

// wait for signal
sig := <-cShutdown
logger.Info("Caught signal, shutting down", zap.Any("sig", sig))
logger.Info().Any("sig", sig).Msg("Caught signal, shutting down")
},
}
5 changes: 3 additions & 2 deletions apps/agent/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ require (
github.com/gofiber/fiber/v2 v2.47.0
github.com/google/uuid v1.3.0
github.com/joho/godotenv v1.5.1
github.com/rs/zerolog v1.30.0
github.com/segmentio/kafka-go v0.4.40
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.4
github.com/testcontainers/testcontainers-go v0.22.0
github.com/testcontainers/testcontainers-go/modules/mysql v0.22.0
go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/trace v1.14.0
go.uber.org/zap v1.24.0
Expand Down Expand Up @@ -70,7 +71,7 @@ require (
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/testcontainers/testcontainers-go/modules/mysql v0.22.0 // indirect
github.com/testcontainers/testcontainers-go v0.22.0 // indirect
github.com/tinylib/msgp v1.1.8 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.47.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions apps/agent/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkX
github.com/containerd/containerd v1.7.3 h1:cKwYKkP1eTj54bP3wCdXXBymmKRQMrWjkLSWZZJDa8o=
github.com/containerd/containerd v1.7.3/go.mod h1:32FOM4/O0RkNg7AjQj3hDzN9cUGtu+HMvaKUNiqCZB8=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/dockercfg v0.3.1 h1:/FpZ+JaygUR/lZP2NlFI2DVfrOEMAIKP5wWEJdoYe9E=
github.com/cpuguy83/dockercfg v0.3.1/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
Expand Down Expand Up @@ -257,8 +258,10 @@ github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
Expand Down Expand Up @@ -311,6 +314,9 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c=
github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 h1:rmMl4fXJhKMNWl+K+r/fq4FbbKI+Ia2m9hYBLm2h4G4=
Expand Down Expand Up @@ -550,7 +556,9 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
12 changes: 6 additions & 6 deletions apps/agent/pkg/analytics/tinybird/tinybird.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"time"

"github.com/unkeyed/unkey/apps/agent/pkg/analytics"
"go.uber.org/zap"
"github.com/unkeyed/unkey/apps/agent/pkg/logging"
)

type Tinybird struct {
Expand All @@ -21,7 +21,7 @@ type Tinybird struct {
keyVerificationsC chan analytics.KeyVerificationEvent
closeC chan struct{}

logger *zap.Logger
logger logging.Logger
}

var _ analytics.Analytics = &Tinybird{}
Expand All @@ -30,7 +30,7 @@ type Config struct {
// Token is the Tinybird token
Token string

Logger *zap.Logger
Logger logging.Logger
}

func New(config Config) *Tinybird {
Expand Down Expand Up @@ -62,12 +62,12 @@ func (t *Tinybird) consume() {
Time: e.Time,
})
if err != nil {
t.logger.Error("unable to publish v1 event to tinybird", zap.Error(err))
t.logger.Err(err).Msg("unable to publish v1 event to tinybird")
}

err = t.publishEvent("key_verifications__v2", e)
if err != nil {
t.logger.Error("unable to publish event to tinybird", zap.Error(err))
t.logger.Err(err).Msg("unable to publish event to tinybird")
}
}
}
Expand All @@ -79,7 +79,7 @@ func (t *Tinybird) Close() {
}

func (t *Tinybird) PublishKeyVerificationEvent(ctx context.Context, event analytics.KeyVerificationEvent) {
t.logger.Info("publishing event", zap.Any("event", event))
t.logger.Debug().Any("event", event).Msg("publishing event")
t.keyVerificationsC <- event
}

Expand Down
7 changes: 3 additions & 4 deletions apps/agent/pkg/boot/cache_warmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/unkeyed/unkey/apps/agent/pkg/database"
"github.com/unkeyed/unkey/apps/agent/pkg/entities"
"github.com/unkeyed/unkey/apps/agent/pkg/logging"
"go.uber.org/zap"
)

type CacheWarmer struct {
Expand All @@ -31,7 +30,7 @@ func NewCacheWarmer(config Config) *CacheWarmer {
apiCache: config.ApiCache,
keyCache: config.KeyCache,
db: config.DB,
logger: config.Logger.With(zap.String("pkg", "cacheWarmer")),
logger: config.Logger.With().Str("pkg", "cacheWarmer").Logger(),
stopped: false,
}
}
Expand All @@ -56,8 +55,8 @@ func (c *CacheWarmer) Run(ctx context.Context) error {
return fmt.Errorf("unable to list apis: %w", err)
}
for _, api := range apis {
logger := c.logger.With(zap.String("apiId", api.Id))
logger.Info("seeding api")
logger := c.logger.With().Str("apiId", api.Id).Logger()
logger.Info().Msg("seeding api")
c.apiCache.Set(ctx, api.KeyAuthId, api)

keyOffset := 0
Expand Down
Loading

1 comment on commit 2dacc58

@vercel
Copy link

@vercel vercel bot commented on 2dacc58 Sep 16, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

unkey – ./

unkey-unkey.vercel.app
unkey.vercel.app
unkey-git-main-unkey.vercel.app
www.unkey.dev
unkey.dev

Please sign in to comment.