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:
@@ -1,6 +1,7 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
)
|
||||
@@ -26,13 +27,13 @@ type SmtpSettings struct {
|
||||
FromName string
|
||||
}
|
||||
|
||||
func NewSettingsFromEnv(env func(string) string) (*Settings, error) {
|
||||
func NewSettingsFromEnv(ctx context.Context, env func(string) string) (*Settings, error) {
|
||||
var (
|
||||
smtp *SmtpSettings
|
||||
err error
|
||||
)
|
||||
if env("SMTP_ENABLED") == "true" {
|
||||
smtp, err = getSmtpSettings(env)
|
||||
smtp, err = getSmtpSettings(ctx, env)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -46,26 +47,26 @@ func NewSettingsFromEnv(env func(string) string) (*Settings, error) {
|
||||
}
|
||||
|
||||
if settings.BaseUrl == "" {
|
||||
slog.Error("BASE_URL must be set")
|
||||
slog.ErrorContext(ctx, "BASE_URL must be set")
|
||||
return nil, ErrMissingConfig
|
||||
}
|
||||
if settings.Port == "" {
|
||||
slog.Error("PORT must be set")
|
||||
slog.ErrorContext(ctx, "PORT must be set")
|
||||
return nil, ErrMissingConfig
|
||||
}
|
||||
if settings.Environment == "" {
|
||||
slog.Error("ENVIRONMENT must be set")
|
||||
slog.ErrorContext(ctx, "ENVIRONMENT must be set")
|
||||
return nil, ErrMissingConfig
|
||||
}
|
||||
|
||||
slog.Info("settings read", "BASE_URL", settings.BaseUrl)
|
||||
slog.Info("settings read", "ENVIRONMENT", settings.Environment)
|
||||
slog.Info("settings read", "ENVIRONMENT", settings.Environment)
|
||||
slog.InfoContext(ctx, "settings read", "BASE_URL", settings.BaseUrl)
|
||||
slog.InfoContext(ctx, "settings read", "ENVIRONMENT", settings.Environment)
|
||||
slog.InfoContext(ctx, "settings read", "ENVIRONMENT", settings.Environment)
|
||||
|
||||
return settings, nil
|
||||
}
|
||||
|
||||
func getSmtpSettings(env func(string) string) (*SmtpSettings, error) {
|
||||
func getSmtpSettings(ctx context.Context, env func(string) string) (*SmtpSettings, error) {
|
||||
smtp := SmtpSettings{
|
||||
Host: env("SMTP_HOST"),
|
||||
Port: env("SMTP_PORT"),
|
||||
@@ -76,27 +77,27 @@ func getSmtpSettings(env func(string) string) (*SmtpSettings, error) {
|
||||
}
|
||||
|
||||
if smtp.Host == "" {
|
||||
slog.Error("SMTP_HOST must be set")
|
||||
slog.ErrorContext(ctx, "SMTP_HOST must be set")
|
||||
return nil, ErrMissingConfig
|
||||
}
|
||||
if smtp.Port == "" {
|
||||
slog.Error("SMTP_PORT must be set")
|
||||
slog.ErrorContext(ctx, "SMTP_PORT must be set")
|
||||
return nil, ErrMissingConfig
|
||||
}
|
||||
if smtp.User == "" {
|
||||
slog.Error("SMTP_USER must be set")
|
||||
slog.ErrorContext(ctx, "SMTP_USER must be set")
|
||||
return nil, ErrMissingConfig
|
||||
}
|
||||
if smtp.Pass == "" {
|
||||
slog.Error("SMTP_PASS must be set")
|
||||
slog.ErrorContext(ctx, "SMTP_PASS must be set")
|
||||
return nil, ErrMissingConfig
|
||||
}
|
||||
if smtp.FromMail == "" {
|
||||
slog.Error("SMTP_FROM_MAIL must be set")
|
||||
slog.ErrorContext(ctx, "SMTP_FROM_MAIL must be set")
|
||||
return nil, ErrMissingConfig
|
||||
}
|
||||
if smtp.FromName == "" {
|
||||
slog.Error("SMTP_FROM_NAME must be set")
|
||||
slog.ErrorContext(ctx, "SMTP_FROM_NAME must be set")
|
||||
return nil, ErrMissingConfig
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user