diff --git a/docs/content/en/docs/documentation/observability.md b/docs/content/en/docs/documentation/observability.md index acadaea182..0a629ab0e7 100644 --- a/docs/content/en/docs/documentation/observability.md +++ b/docs/content/en/docs/documentation/observability.md @@ -50,6 +50,13 @@ For `InformerEventSource` and `ControllerEventSource` the following information | `eventsource.event.action` | action name (e.g. `ADDED`, `UPDATED`, `DELETED`) | | `eventsource.name` | name of the event source | +### Note on null values + +If a resource doesn't provide values for one of the specified keys, the key will be omitted and not added to the MDC +context. There is, however, one notable exception: the resource's namespace, where, instead of omitting the key, we emit +the `MDCUtils.NO_NAMESPACE` value instead. This allows searching for resources without namespace (notably, clustered +resources) in the logs more easily. + ### Disabling MDC support MDC support is enabled by default. If you want to disable it, you can set the `JAVA_OPERATOR_SDK_USE_MDC` environment diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/MDCUtils.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/MDCUtils.java index b8a7ba1f40..716490388d 100644 --- a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/MDCUtils.java +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/MDCUtils.java @@ -23,6 +23,7 @@ import io.javaoperatorsdk.operator.processing.event.source.ResourceAction; public class MDCUtils { + public static final String NO_NAMESPACE = "no namespace"; private static final String NAME = "resource.name"; private static final String NAMESPACE = "resource.namespace"; @@ -31,20 +32,18 @@ public class MDCUtils { private static final String RESOURCE_VERSION = "resource.resourceVersion"; private static final String GENERATION = "resource.generation"; private static final String UID = "resource.uid"; - private static final String NO_NAMESPACE = "no namespace"; private static final boolean enabled = Utils.getBooleanFromSystemPropsOrDefault(Utils.USE_MDC_ENV_KEY, true); private static final String EVENT_SOURCE_PREFIX = "eventsource.event."; private static final String EVENT_ACTION = EVENT_SOURCE_PREFIX + "action"; private static final String EVENT_SOURCE_NAME = "eventsource.name"; - private static final String UNKNOWN_ACTION = "unknown action"; public static void addInformerEventInfo( HasMetadata resource, ResourceAction action, String eventSourceName) { if (enabled) { addResourceInfo(resource, true); - MDC.put(EVENT_ACTION, action == null ? UNKNOWN_ACTION : action.name()); + MDC.put(EVENT_ACTION, action.name()); MDC.put(EVENT_SOURCE_NAME, eventSourceName); } } @@ -92,9 +91,10 @@ public static void addResourceInfo(HasMetadata resource, boolean forEventSource) final var metadata = resource.getMetadata(); if (metadata != null) { MDC.put(key(NAME, forEventSource), metadata.getName()); - if (metadata.getNamespace() != null) { - MDC.put(key(NAMESPACE, forEventSource), metadata.getNamespace()); - } + + final var namespace = metadata.getNamespace(); + MDC.put(key(NAMESPACE, forEventSource), namespace != null ? namespace : NO_NAMESPACE); + MDC.put(key(RESOURCE_VERSION, forEventSource), metadata.getResourceVersion()); if (metadata.getGeneration() != null) { MDC.put(key(GENERATION, forEventSource), metadata.getGeneration().toString());