From dc79ef5e1f63c0cf8f8be76c5c7e040e5eb51497 Mon Sep 17 00:00:00 2001 From: kk10 Date: Sat, 17 Oct 2026 10:09:31 +0300 Subject: [PATCH] feat: add X-Instance-Id header and instance_id log field --- internal/app/app.go | 3 ++- internal/app/router.go | 2 ++ internal/config/config.go | 16 ++++++++++++++++ internal/middleware/instance_id.go | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 internal/middleware/instance_id.go diff --git a/internal/app/app.go b/internal/app/app.go index ad0e81bf..9c807e19 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -27,7 +27,7 @@ func Run() error { cfg := config.Load() - log := logger.New(cfg.Logging.Level) + log := logger.New(cfg.Logging.Level).With("instance_id", cfg.Server.InstanceID) dbURL, err := cfg.Database.URL() if err != nil { @@ -80,6 +80,7 @@ func Run() error { router := NewRouter( log, + cfg.Server.InstanceID, cfg.Server.MaxInflightRequests, pool, authService, diff --git a/internal/app/router.go b/internal/app/router.go index f30293a6..85a87f2a 100644 --- a/internal/app/router.go +++ b/internal/app/router.go @@ -14,6 +14,7 @@ import ( func NewRouter( log *slog.Logger, + instanceID string, maxInflightRequests int, db handler.Pinger, authService *service.AuthService, @@ -29,6 +30,7 @@ func NewRouter( chatHandler := handler.NewChatHandler(chatService) messageHandler := handler.NewMessageHandler(messageService) + router.Use(middleware.InstanceID(instanceID)) router.Use(middleware.RequestTimeout(5 * time.Second)) router.Use(middleware.RequestID) router.Use(middleware.Logging(log)) diff --git a/internal/config/config.go b/internal/config/config.go index 96fe012e..f934f6ac 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -32,6 +32,7 @@ type ServerConfig struct { Port string MaxInflightRequests int SessionTTL time.Duration + InstanceID string } type LoggingConfig struct { @@ -61,6 +62,7 @@ func Load() Config { Port: getEnv("APP_PORT", "8080"), MaxInflightRequests: getEnvNonNegativeInt("MAX_INFLIGHT_REQUESTS", 0), SessionTTL: time.Duration(getEnvPositiveInt("SESSION_TTL_SECONDS", 3600)) * time.Second, + InstanceID: getInstanceID("INSTANCE_ID"), }, Logging: LoggingConfig{ @@ -165,3 +167,17 @@ func getEnvSessionStore(key, def string) string { return def } } + +func getInstanceID(key string) string { + if v := os.Getenv(key); v != "" { + return v + } + + host, err := os.Hostname() + if err != nil || host == "" { + log.Printf("cannot determine hostname, using \"unknown\" for %s: %v", key, err) + return "unknown" + } + + return host +} diff --git a/internal/middleware/instance_id.go b/internal/middleware/instance_id.go new file mode 100644 index 00000000..5fa86d18 --- /dev/null +++ b/internal/middleware/instance_id.go @@ -0,0 +1,14 @@ +package middleware + +import ( + "net/http" +) + +func InstanceID(id string) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("X-Instance-Id", id) + next.ServeHTTP(w, r) + }) + } +}