diff --git a/server/src/main/java/org/opensearch/common/logging/NodeAndClusterIdConverter.java b/server/src/main/java/org/opensearch/common/logging/NodeAndClusterIdConverter.java index a3062bca90158..4216963932b51 100644 --- a/server/src/main/java/org/opensearch/common/logging/NodeAndClusterIdConverter.java +++ b/server/src/main/java/org/opensearch/common/logging/NodeAndClusterIdConverter.java @@ -50,6 +50,7 @@ @Plugin(category = PatternConverter.CATEGORY, name = "NodeAndClusterIdConverter") @ConverterKeys({ "node_and_cluster_id" }) public final class NodeAndClusterIdConverter extends LogEventPatternConverter { + static final String ENABLED_SYSTEM_PROPERTY = "org.opensearch.common.logging.NodeAndClusterIdConverter.enabled"; private static final SetOnce nodeAndClusterId = new SetOnce<>(); /** @@ -71,7 +72,9 @@ public NodeAndClusterIdConverter() { * @param clusterUUID a clusterId received from cluster state update */ public static void setNodeIdAndClusterId(String nodeId, String clusterUUID) { - nodeAndClusterId.set(formatIds(clusterUUID, nodeId)); + if (isEnabled()) { + nodeAndClusterId.set(formatIds(clusterUUID, nodeId)); + } } /** @@ -90,4 +93,9 @@ public void format(LogEvent event, StringBuilder toAppendTo) { private static String formatIds(String clusterUUID, String nodeId) { return String.format(Locale.ROOT, "\"cluster.uuid\": \"%s\", \"node.id\": \"%s\"", clusterUUID, nodeId); } + + private static boolean isEnabled() { + String enabled = System.getProperty(ENABLED_SYSTEM_PROPERTY, "true"); + return Boolean.valueOf(enabled); + } } diff --git a/server/src/test/java/org/opensearch/common/logging/NodeAndClusterIdConverterTest.java b/server/src/test/java/org/opensearch/common/logging/NodeAndClusterIdConverterTest.java new file mode 100644 index 0000000000000..dd8ec0ea70d24 --- /dev/null +++ b/server/src/test/java/org/opensearch/common/logging/NodeAndClusterIdConverterTest.java @@ -0,0 +1,51 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.common.logging; + +import org.apache.lucene.util.SetOnce; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class NodeAndClusterIdConverterTest { + + public static final String NODE_ID = "node-id"; + public static final String CLUSTER_UUID = "cluster-uuid"; + + @After + @Before + public void cleanSystemProperties() { + System.clearProperty(NodeAndClusterIdConverter.ENABLED_SYSTEM_PROPERTY); + } + + @Test(expected = SetOnce.AlreadySetException.class) + public void testTryToSetClusterAndNodeIdByDefault() { + NodeAndClusterIdConverter.setNodeIdAndClusterId(NODE_ID, CLUSTER_UUID); + NodeAndClusterIdConverter.setNodeIdAndClusterId(NODE_ID, CLUSTER_UUID); + } + + @Test(expected = SetOnce.AlreadySetException.class) + public void testTryToSetClusterAndNodeIdWhenPluginIsEnabled() { + System.setProperty(NodeAndClusterIdConverter.ENABLED_SYSTEM_PROPERTY, "true"); + + NodeAndClusterIdConverter.setNodeIdAndClusterId(NODE_ID, CLUSTER_UUID); + NodeAndClusterIdConverter.setNodeIdAndClusterId(NODE_ID, CLUSTER_UUID); + } + + @Test + public void testNotSetClusterIdWhenPluginIsDisabled() { + System.setProperty(NodeAndClusterIdConverter.ENABLED_SYSTEM_PROPERTY, "false"); + + NodeAndClusterIdConverter.setNodeIdAndClusterId(NODE_ID, CLUSTER_UUID); + + // second invocation should not throw an exception + NodeAndClusterIdConverter.setNodeIdAndClusterId(NODE_ID, CLUSTER_UUID); + } + +}