feat: add X-Instance-Id header and instance_id log field

This commit is contained in:
kk10
2026-10-17 10:09:31 +03:00
parent cd6a1c7aee
commit dc79ef5e1f
4 changed files with 34 additions and 1 deletions
+16
View File
@@ -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
}