Skip to content

Commit 8144ddf

Browse files
metacosmCopilot
authored andcommitted
feat: emit MDCUtils.NO_NAMESPACE value when namespace is null (#3186)
* feat: emit MDCUtils.NO_NAMESPACE value when namespace is null Fixes #3184 Signed-off-by: Chris Laprun <metacosm@gmail.com> * fix: improve wording Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Signed-off-by: Chris Laprun <metacosm@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 2226239 commit 8144ddf

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

docs/content/en/docs/documentation/observability.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ For `InformerEventSource` and `ControllerEventSource` the following information
5050
| `eventsource.event.action` | action name (e.g. `ADDED`, `UPDATED`, `DELETED`) |
5151
| `eventsource.name` | name of the event source |
5252

53+
### Note on null values
54+
55+
If a resource doesn't provide values for one of the specified keys, the key will be omitted and not added to the MDC
56+
context. There is, however, one notable exception: the resource's namespace, where, instead of omitting the key, we emit
57+
the `MDCUtils.NO_NAMESPACE` value instead. This allows searching for resources without namespace (notably, clustered
58+
resources) in the logs more easily.
59+
5360
### Disabling MDC support
5461

5562
MDC support is enabled by default. If you want to disable it, you can set the `JAVA_OPERATOR_SDK_USE_MDC` environment

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/MDCUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.javaoperatorsdk.operator.processing.event.source.ResourceAction;
2424

2525
public class MDCUtils {
26+
public static final String NO_NAMESPACE = "no namespace";
2627

2728
private static final String NAME = "resource.name";
2829
private static final String NAMESPACE = "resource.namespace";
@@ -31,20 +32,18 @@ public class MDCUtils {
3132
private static final String RESOURCE_VERSION = "resource.resourceVersion";
3233
private static final String GENERATION = "resource.generation";
3334
private static final String UID = "resource.uid";
34-
private static final String NO_NAMESPACE = "no namespace";
3535
private static final boolean enabled =
3636
Utils.getBooleanFromSystemPropsOrDefault(Utils.USE_MDC_ENV_KEY, true);
3737

3838
private static final String EVENT_SOURCE_PREFIX = "eventsource.event.";
3939
private static final String EVENT_ACTION = EVENT_SOURCE_PREFIX + "action";
4040
private static final String EVENT_SOURCE_NAME = "eventsource.name";
41-
private static final String UNKNOWN_ACTION = "unknown action";
4241

4342
public static void addInformerEventInfo(
4443
HasMetadata resource, ResourceAction action, String eventSourceName) {
4544
if (enabled) {
4645
addResourceInfo(resource, true);
47-
MDC.put(EVENT_ACTION, action == null ? UNKNOWN_ACTION : action.name());
46+
MDC.put(EVENT_ACTION, action.name());
4847
MDC.put(EVENT_SOURCE_NAME, eventSourceName);
4948
}
5049
}
@@ -92,9 +91,10 @@ public static void addResourceInfo(HasMetadata resource, boolean forEventSource)
9291
final var metadata = resource.getMetadata();
9392
if (metadata != null) {
9493
MDC.put(key(NAME, forEventSource), metadata.getName());
95-
if (metadata.getNamespace() != null) {
96-
MDC.put(key(NAMESPACE, forEventSource), metadata.getNamespace());
97-
}
94+
95+
final var namespace = metadata.getNamespace();
96+
MDC.put(key(NAMESPACE, forEventSource), namespace != null ? namespace : NO_NAMESPACE);
97+
9898
MDC.put(key(RESOURCE_VERSION, forEventSource), metadata.getResourceVersion());
9999
if (metadata.getGeneration() != null) {
100100
MDC.put(key(GENERATION, forEventSource), metadata.getGeneration().toString());

0 commit comments

Comments
 (0)