From 90918d79a811cc9a284226c5f76d40b949cbc5da Mon Sep 17 00:00:00 2001 From: Victor Agababov Date: Thu, 17 Sep 2020 19:48:21 +0000 Subject: [PATCH] Inject the pod name into logger when provided in the envvars Deployments that are H(P)A need to provide the pod name envvar via downward api. Activator and QP are already doing this. --- injection/sharedmain/main.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/injection/sharedmain/main.go b/injection/sharedmain/main.go index 66b602e932..69dd44ccfa 100644 --- a/injection/sharedmain/main.go +++ b/injection/sharedmain/main.go @@ -47,6 +47,7 @@ import ( "knative.dev/pkg/injection" "knative.dev/pkg/leaderelection" "knative.dev/pkg/logging" + "knative.dev/pkg/logging/logkey" "knative.dev/pkg/metrics" "knative.dev/pkg/profiling" "knative.dev/pkg/reconciler" @@ -339,7 +340,16 @@ func SetupLoggerOrDie(ctx context.Context, component string) (*zap.SugaredLogger if err != nil { log.Fatalf("Error reading/parsing logging configuration: %v", err) } - return logging.NewLoggerFromConfig(loggingConfig, component) + l, level := logging.NewLoggerFromConfig(loggingConfig, component) + + // If PodName is injected into the env vars, set it on the logger. + // This is needed for HA components to distinguish logs from different + // pods. + if pn := os.Getenv("POD_NAME"); pn != "" { + l = l.With(zap.String(logkey.Pod, pn)) + } + + return l, level } // CheckK8sClientMinimumVersionOrDie checks that the hosting Kubernetes cluster