fix(observabillity): include otel logs
Some checks failed
Build Docker Image / Build-Docker-Image (push) Failing after 4m29s

This commit is contained in:
2025-06-07 15:12:18 +02:00
parent e65146c71c
commit 661a3ba79f
26 changed files with 230 additions and 184 deletions

View File

@@ -3,6 +3,7 @@ package internal
import (
"errors"
"fmt"
"log/slog"
"spend-sparrow/internal/db"
"spend-sparrow/internal/handler"
"spend-sparrow/internal/handler/middleware"
@@ -26,7 +27,27 @@ func Run(ctx context.Context, database *sqlx.DB, migrationsPrefix string, env fu
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer cancel()
log.L.Info("Starting server...")
otelEnabled := types.IsOtelEnabled(env)
if otelEnabled {
// use context.Background(), otherwise the shutdown can't be called, as the context is already cancelled
otelShutdown, err := setupOTelSDK(context.Background())
if err != nil {
return fmt.Errorf("could not setup OpenTelemetry SDK: %w", err)
}
defer func() {
// User context.Background(), as the main context is already cancelled
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
err = otelShutdown(ctx)
if err != nil {
slog.Error("error shutting down OpenTelemetry SDK", "err", err)
}
cancel()
}()
slog.SetDefault(log.NewLogPropagator())
}
slog.Info("Starting server...")
// init server settings
serverSettings, err := types.NewSettingsFromEnv(env)
@@ -40,25 +61,6 @@ func Run(ctx context.Context, database *sqlx.DB, migrationsPrefix string, env fu
return fmt.Errorf("could not run migrations: %w", err)
}
if serverSettings.OtelEnabled {
// use context.Background(), otherwise the shutdown can't be called, as the context is already cancelled
otelShutdown, err := setupOTelSDK(context.Background())
if err != nil {
return fmt.Errorf("could not setup OpenTelemetry SDK: %w", err)
}
defer func() {
// User context.Background(), as the main context is already cancelled
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
err = otelShutdown(ctx)
if err != nil {
log.L.Error("error shutting down OpenTelemetry SDK", "err", err)
}
cancel()
}()
log.InitOtelLogger()
}
// init server
httpServer := &http.Server{
Addr: ":" + serverSettings.Port,
@@ -77,9 +79,9 @@ func Run(ctx context.Context, database *sqlx.DB, migrationsPrefix string, env fu
}
func startServer(s *http.Server) {
log.L.Info("Starting server", "addr", s.Addr)
slog.Info("Starting server", "addr", s.Addr)
if err := s.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.L.Error("error listening and serving", "err", err)
slog.Error("error listening and serving", "err", err)
}
}
@@ -94,9 +96,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 {
log.L.Error("error shutting down http server", "err", err)
slog.Error("error shutting down http server", "err", err)
} else {
log.L.Info("Gracefully stopped http server", "addr", s.Addr)
slog.Info("Gracefully stopped http server", "addr", s.Addr)
}
}