Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/content/en/docs/documentation/observability.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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());
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addInformerEventInfo previously handled a null action (falling back to an "unknown" value). With the new MDC.put(EVENT_ACTION, action.name()), passing a null action will now throw an NPE before MDC is populated. Consider restoring the null handling (or enforcing non-null via validation/annotations) to avoid a behavioral regression in this public utility method.

Suggested change
MDC.put(EVENT_ACTION, action.name());
MDC.put(EVENT_ACTION, action != null ? action.name() : "unknown");

Copilot uses AI. Check for mistakes.
MDC.put(EVENT_SOURCE_NAME, eventSourceName);
}
}
Expand Down Expand Up @@ -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());
Expand Down
Loading