Skip to content

Commit

Permalink
It is possible to disable NodeAndClusterIdConverter plugin.
Browse files Browse the repository at this point in the history
Signed-off-by: Lukasz Soszynski <lukasz.soszynski@eliatra.com>
  • Loading branch information
lukaszsoszynski committed Sep 6, 2022
1 parent 38372e1 commit 98de5e7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> nodeAndClusterId = new SetOnce<>();

/**
Expand All @@ -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));
}
}

/**
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 98de5e7

Please sign in to comment.