-
Notifications
You must be signed in to change notification settings - Fork 236
fix: unify how resource information is added, prevent NPEs #3185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,44 +35,28 @@ public class MDCUtils { | |
| private static final boolean enabled = | ||
| Utils.getBooleanFromSystemPropsOrDefault(Utils.USE_MDC_ENV_KEY, true); | ||
|
|
||
| private static final String EVENT_RESOURCE_NAME = "eventsource.event.resource.name"; | ||
| private static final String EVENT_RESOURCE_UID = "eventsource.event.resource.uid"; | ||
| private static final String EVENT_RESOURCE_NAMESPACE = "eventsource.event.resource.namespace"; | ||
| private static final String EVENT_RESOURCE_KIND = "eventsource.event.resource.kind"; | ||
| private static final String EVENT_RESOURCE_VERSION = "eventsource.event.resource.resourceVersion"; | ||
| private static final String EVENT_ACTION = "eventsource.event.action"; | ||
| 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) { | ||
| MDC.put(EVENT_RESOURCE_NAME, resource.getMetadata().getName()); | ||
| MDC.put(EVENT_RESOURCE_NAMESPACE, resource.getMetadata().getNamespace()); | ||
| MDC.put(EVENT_RESOURCE_KIND, HasMetadata.getKind(resource.getClass())); | ||
| MDC.put(EVENT_RESOURCE_VERSION, resource.getMetadata().getResourceVersion()); | ||
| MDC.put(EVENT_RESOURCE_UID, resource.getMetadata().getUid()); | ||
| MDC.put(EVENT_ACTION, action == null ? null : action.name()); | ||
| addResourceInfo(resource, true); | ||
| MDC.put(EVENT_ACTION, action == null ? UNKNOWN_ACTION : action.name()); | ||
| MDC.put(EVENT_SOURCE_NAME, eventSourceName); | ||
| } | ||
| } | ||
|
|
||
| public static void removeInformerEventInfo() { | ||
| if (enabled) { | ||
| MDC.remove(EVENT_RESOURCE_NAME); | ||
| MDC.remove(EVENT_RESOURCE_NAMESPACE); | ||
| MDC.remove(EVENT_RESOURCE_KIND); | ||
| MDC.remove(EVENT_RESOURCE_VERSION); | ||
| MDC.remove(EVENT_RESOURCE_UID); | ||
| removeResourceInfo(true); | ||
| MDC.remove(EVENT_ACTION); | ||
| MDC.remove(EVENT_SOURCE_NAME); | ||
| } | ||
| } | ||
|
|
||
| public static void withMDCForEvent( | ||
| HasMetadata resource, Runnable runnable, String eventSourceName) { | ||
| withMDCForEvent(resource, null, runnable, eventSourceName); | ||
| } | ||
|
|
||
| public static void withMDCForEvent( | ||
| HasMetadata resource, ResourceAction action, Runnable runnable, String eventSourceName) { | ||
| try { | ||
|
|
@@ -83,24 +67,6 @@ public static void withMDCForEvent( | |
| } | ||
| } | ||
|
|
||
| public static void withMDCForResourceID(ResourceID resourceID, Runnable runnable) { | ||
| try { | ||
| MDCUtils.addResourceIDInfo(resourceID); | ||
| runnable.run(); | ||
| } finally { | ||
| MDCUtils.removeResourceIDInfo(); | ||
| } | ||
| } | ||
|
|
||
| public static void withMDCForPrimary(HasMetadata primary, Runnable runnable) { | ||
| try { | ||
| MDCUtils.addResourceInfo(primary); | ||
| runnable.run(); | ||
| } finally { | ||
| MDCUtils.removeResourceInfo(); | ||
| } | ||
| } | ||
|
|
||
| public static void addResourceIDInfo(ResourceID resourceID) { | ||
| if (enabled) { | ||
| MDC.put(NAME, resourceID.getName()); | ||
|
|
@@ -116,33 +82,45 @@ public static void removeResourceIDInfo() { | |
| } | ||
|
|
||
| public static void addResourceInfo(HasMetadata resource) { | ||
| addResourceInfo(resource, false); | ||
| } | ||
|
|
||
| public static void addResourceInfo(HasMetadata resource, boolean forEventSource) { | ||
| if (enabled) { | ||
| MDC.put(API_VERSION, resource.getApiVersion()); | ||
| MDC.put(KIND, resource.getKind()); | ||
| MDC.put(key(API_VERSION, forEventSource), resource.getApiVersion()); | ||
| MDC.put(key(KIND, forEventSource), resource.getKind()); | ||
| final var metadata = resource.getMetadata(); | ||
| if (metadata != null) { | ||
| MDC.put(NAME, metadata.getName()); | ||
| MDC.put(key(NAME, forEventSource), metadata.getName()); | ||
| if (metadata.getNamespace() != null) { | ||
| MDC.put(NAMESPACE, metadata.getNamespace()); | ||
| MDC.put(key(NAMESPACE, forEventSource), metadata.getNamespace()); | ||
| } | ||
| MDC.put(RESOURCE_VERSION, metadata.getResourceVersion()); | ||
| MDC.put(key(RESOURCE_VERSION, forEventSource), metadata.getResourceVersion()); | ||
| if (metadata.getGeneration() != null) { | ||
| MDC.put(GENERATION, metadata.getGeneration().toString()); | ||
| MDC.put(key(GENERATION, forEventSource), metadata.getGeneration().toString()); | ||
| } | ||
| MDC.put(UID, metadata.getUid()); | ||
| MDC.put(key(UID, forEventSource), metadata.getUid()); | ||
|
Comment on lines
89
to
+102
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| private static String key(String baseKey, boolean forEventSource) { | ||
| return forEventSource ? EVENT_SOURCE_PREFIX + baseKey : baseKey; | ||
| } | ||
|
|
||
| public static void removeResourceInfo() { | ||
| removeResourceInfo(false); | ||
| } | ||
|
|
||
| public static void removeResourceInfo(boolean forEventSource) { | ||
| if (enabled) { | ||
| MDC.remove(API_VERSION); | ||
| MDC.remove(KIND); | ||
| MDC.remove(NAME); | ||
| MDC.remove(NAMESPACE); | ||
| MDC.remove(RESOURCE_VERSION); | ||
| MDC.remove(GENERATION); | ||
| MDC.remove(UID); | ||
| MDC.remove(key(API_VERSION, forEventSource)); | ||
| MDC.remove(key(KIND, forEventSource)); | ||
| MDC.remove(key(NAME, forEventSource)); | ||
| MDC.remove(key(NAMESPACE, forEventSource)); | ||
| MDC.remove(key(RESOURCE_VERSION, forEventSource)); | ||
| MDC.remove(key(GENERATION, forEventSource)); | ||
| MDC.remove(key(UID, forEventSource)); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MDC.put(EVENT_SOURCE_NAME, eventSourceName)will still fail on MDC implementations that don't accept null values ifeventSourceNameis null. Consider applying the same strategy as for the action (default value) or omitting/removing the key when the name is null.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eventSourceNameis never supposed to benull.