Skip to content

feat: enable deactivation of MDC logging #2699

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

Merged
merged 1 commit into from
Feb 25, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class Utils {
private static final Logger log = LoggerFactory.getLogger(Utils.class);
public static final String CHECK_CRD_ENV_KEY = "JAVA_OPERATOR_SDK_CHECK_CRD";
public static final String DEBUG_THREAD_POOL_ENV_KEY = "JAVA_OPERATOR_SDK_DEBUG_THREAD_POOL";
public static final String USE_MDC_ENV_KEY = "JAVA_OPERATOR_SDK_USE_MDC";
public static final String GENERIC_PARAMETER_TYPE_ERROR_PREFIX =
"Couldn't retrieve generic parameter type from ";

Expand Down Expand Up @@ -97,7 +98,7 @@ public static boolean debugThreadPool() {
return getBooleanFromSystemPropsOrDefault(DEBUG_THREAD_POOL_ENV_KEY, false);
}

static boolean getBooleanFromSystemPropsOrDefault(String propertyName, boolean defaultValue) {
public static boolean getBooleanFromSystemPropsOrDefault(String propertyName, boolean defaultValue) {
var property = System.getProperty(propertyName);
if (property == null) {
return defaultValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.javaoperatorsdk.operator.processing;

import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.javaoperatorsdk.operator.api.config.Utils;
import org.slf4j.MDC;

import io.fabric8.kubernetes.api.model.HasMetadata;
Expand All @@ -15,38 +17,50 @@ public class MDCUtils {
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);

public static void addResourceIDInfo(ResourceID resourceID) {
MDC.put(NAME, resourceID.getName());
MDC.put(NAMESPACE, resourceID.getNamespace().orElse(NO_NAMESPACE));
if (enabled) {
MDC.put(NAME, resourceID.getName());
MDC.put(NAMESPACE, resourceID.getNamespace().orElse(NO_NAMESPACE));
}
}

public static void removeResourceIDInfo() {
MDC.remove(NAME);
MDC.remove(NAMESPACE);
if (enabled) {
MDC.remove(NAME);
MDC.remove(NAMESPACE);
}
}

public static void addResourceInfo(HasMetadata resource) {
MDC.put(API_VERSION, resource.getApiVersion());
MDC.put(KIND, resource.getKind());
MDC.put(NAME, resource.getMetadata().getName());
if (resource.getMetadata().getNamespace() != null) {
MDC.put(NAMESPACE, resource.getMetadata().getNamespace());
}
MDC.put(RESOURCE_VERSION, resource.getMetadata().getResourceVersion());
if (resource.getMetadata().getGeneration() != null) {
MDC.put(GENERATION, resource.getMetadata().getGeneration().toString());
}
MDC.put(UID, resource.getMetadata().getUid());
if (enabled) {
MDC.put(API_VERSION, resource.getApiVersion());
MDC.put(KIND, resource.getKind());
final var metadata = resource.getMetadata();
if (metadata != null) {
MDC.put(NAME, metadata.getName());
if (metadata.getNamespace() != null) {
MDC.put(NAMESPACE, metadata.getNamespace());
}
MDC.put(RESOURCE_VERSION, metadata.getResourceVersion());
if (metadata.getGeneration() != null) {
MDC.put(GENERATION, metadata.getGeneration().toString());
}
MDC.put(UID, metadata.getUid());
}
}
}

public static void removeResourceInfo() {
MDC.remove(API_VERSION);
MDC.remove(KIND);
MDC.remove(NAME);
MDC.remove(NAMESPACE);
MDC.remove(RESOURCE_VERSION);
MDC.remove(GENERATION);
MDC.remove(UID);
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);
}
}
}