fix(observabillity): propagate ctx to every log call and add resource to logging
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 5m5s
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 5m36s

This commit was merged in pull request #187.
This commit is contained in:
2025-06-17 09:42:19 +02:00
parent ff3c7bdf52
commit 6c92206b3c
27 changed files with 288 additions and 266 deletions

View File

@@ -39,7 +39,7 @@ func Run(ctx context.Context, database *sqlx.DB, migrationsPrefix string, env fu
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
err = otelShutdown(ctx)
if err != nil {
slog.Error("error shutting down OpenTelemetry SDK", "err", err)
slog.ErrorContext(ctx, "error shutting down OpenTelemetry SDK", "err", err)
}
cancel()
}()
@@ -47,10 +47,10 @@ func Run(ctx context.Context, database *sqlx.DB, migrationsPrefix string, env fu
slog.SetDefault(log.NewLogPropagator())
}
slog.Info("Starting server...")
slog.InfoContext(ctx, "Starting server...")
// init server settings
serverSettings, err := types.NewSettingsFromEnv(env)
serverSettings, err := types.NewSettingsFromEnv(ctx, env)
if err != nil {
return err
}
@@ -67,26 +67,26 @@ func Run(ctx context.Context, database *sqlx.DB, migrationsPrefix string, env fu
Handler: createHandlerWithServices(ctx, database, serverSettings),
ReadHeaderTimeout: 2 * time.Second,
}
go startServer(httpServer)
go startServer(ctx, httpServer)
// graceful shutdown
var wg sync.WaitGroup
wg.Add(1)
go shutdownServer(httpServer, ctx, &wg)
go shutdownServer(ctx, httpServer, &wg)
wg.Wait()
return nil
}
func startServer(s *http.Server) {
slog.Info("Starting server", "addr", s.Addr)
func startServer(ctx context.Context, s *http.Server) {
slog.InfoContext(ctx, "Starting server", "addr", s.Addr)
if err := s.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("error listening and serving", "err", err)
slog.ErrorContext(ctx, "error listening and serving", "err", err)
}
}
func shutdownServer(s *http.Server, ctx context.Context, wg *sync.WaitGroup) {
func shutdownServer(ctx context.Context, s *http.Server, wg *sync.WaitGroup) {
defer wg.Done()
if s == nil {
return
@@ -97,9 +97,9 @@ func shutdownServer(s *http.Server, ctx context.Context, wg *sync.WaitGroup) {
shutdownCtx, cancel := context.WithTimeout(shutdownCtx, 10*time.Second)
defer cancel()
if err := s.Shutdown(shutdownCtx); err != nil {
slog.Error("error shutting down http server", "err", err)
slog.ErrorContext(ctx, "error shutting down http server", "err", err)
} else {
slog.Info("Gracefully stopped http server", "addr", s.Addr)
slog.InfoContext(ctx, "Gracefully stopped http server", "addr", s.Addr)
}
}
@@ -170,7 +170,7 @@ func dailyTaskTimer(ctx context.Context, transactionRecurring service.Transactio
}
func runDailyTasks(ctx context.Context, transactionRecurring service.TransactionRecurring, auth service.Auth) {
slog.Info("Running daily tasks")
slog.InfoContext(ctx, "Running daily tasks")
_ = transactionRecurring.GenerateTransactions(ctx)
_ = auth.CleanupSessionsAndTokens(ctx)
}