fix(observabillity): propagate ctx to every log call and add resource to logging
This commit was merged in pull request #187.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user