diff --git a/agent/agent-core/src/main/java/org/bithon/agent/configuration/ConfigurationManager.java b/agent/agent-core/src/main/java/org/bithon/agent/configuration/ConfigurationManager.java index 7154c9568b..b56bd59ba2 100644 --- a/agent/agent-core/src/main/java/org/bithon/agent/configuration/ConfigurationManager.java +++ b/agent/agent-core/src/main/java/org/bithon/agent/configuration/ConfigurationManager.java @@ -51,7 +51,7 @@ * 1. The default agent configuration located under the agent-distribution/conf * 2. The external configuration specified via -Dbithon.configuration.location command line argument * 3. All parameters defined by -Dbithon.xxx - * 4. All environment variables starting with bithon_ + * 4. All environment variables starting with BITHON_ * 5. Dynamic configuration from the remote controller * * Different configuration sources may contain the same property keys, @@ -124,7 +124,7 @@ static ConfigurationManager create(File defaultConfigLocation) { return new ConfigurationManager(PropertySource.from(PropertySourceType.INTERNAL, defaultConfigLocation, true), ExternalSource.build(), CommandLineArgsSource.build("bithon."), - EnvironmentSource.build("bithon_")); + EnvironmentSource.build("BITHON_")); } // Sorted in priority diff --git a/agent/agent-core/src/main/java/org/bithon/agent/configuration/source/EnvironmentSource.java b/agent/agent-core/src/main/java/org/bithon/agent/configuration/source/EnvironmentSource.java index 63de8bb137..5d679ebcef 100644 --- a/agent/agent-core/src/main/java/org/bithon/agent/configuration/source/EnvironmentSource.java +++ b/agent/agent-core/src/main/java/org/bithon/agent/configuration/source/EnvironmentSource.java @@ -19,6 +19,7 @@ import org.bithon.agent.instrumentation.expt.AgentException; import java.io.IOException; +import java.util.Locale; import java.util.Map; /** @@ -31,9 +32,10 @@ public static PropertySource build(String envPrefix) { StringBuilder propertyText = new StringBuilder(); for (Map.Entry entry : Helper.getEnvironmentVariables().entrySet()) { - String name = entry.getKey(); + String name = entry.getKey().toLowerCase(Locale.ENGLISH); String value = entry.getValue(); - if (name.startsWith(envPrefix) && !value.isEmpty()) { + String prefix = envPrefix.toLowerCase(Locale.ENGLISH); + if (name.startsWith(prefix) && !value.isEmpty()) { name = name.substring(envPrefix.length()) // For env, the underscore is used as a replacement of '.' character, // Here we need to convert these characters back diff --git a/agent/agent-core/src/test/java/org/bithon/agent/configuration/TestConfigurationManager.java b/agent/agent-core/src/test/java/org/bithon/agent/configuration/TestConfigurationManager.java index baec5614f8..7fd7ddf8bf 100644 --- a/agent/agent-core/src/test/java/org/bithon/agent/configuration/TestConfigurationManager.java +++ b/agent/agent-core/src/test/java/org/bithon/agent/configuration/TestConfigurationManager.java @@ -191,8 +191,8 @@ public void test_Environment() { )); configurationMock.when(Helper::getEnvironmentVariables) - .thenReturn(ImmutableMap.of("bithon_t", "t1", - "bithon_test_prop", "from_env")); + .thenReturn(ImmutableMap.of("BITHON_T", "t1", + "BITHON_TEST_PROP", "from_env")); ConfigurationManager manager = ConfigurationManager.create(defaultConfigLocation); @@ -201,6 +201,33 @@ public void test_Environment() { } } + @Test + public void test_Environment_CaseCompatibility() { + try (MockedStatic configurationMock = Mockito.mockStatic(Helper.class)) { + configurationMock.when(Helper::getCommandLineInputArgs) + .thenReturn(Arrays.asList("-Xms512M", + // A property without assignment + // to verify the processing is correct with such configuration + "-Dbithon.test", + // Override the in file configuration + "-Dbithon.test.prop=from_command_line", + + // Also set the external configuration + "-Dbithon.configuration.location=" + externalConfigLocation + )); + + configurationMock.when(Helper::getEnvironmentVariables) + .thenReturn(ImmutableMap.of("bithon_t", "t1", + "bithon_test_prop", "from_env")); + + ConfigurationManager manager = ConfigurationManager.create(defaultConfigLocation); + + TestProp config = manager.getConfig(TestProp.class); + Assert.assertEquals("from_env", config.getProp()); + } + } + + static class TwoProps { private String prop1; private String prop2; @@ -238,9 +265,9 @@ public void test_PropFromDifferentSource() { )); configurationMock.when(Helper::getEnvironmentVariables) - .thenReturn(ImmutableMap.of("bithon_t", "t1", + .thenReturn(ImmutableMap.of("BITHON_T", "t1", //Overwrite the prop2 - "bithon_test_prop2", "from_env")); + "BITHON_TEST_PROP2", "from_env")); ConfigurationManager manager = ConfigurationManager.create(defaultConfigLocation); diff --git a/doc/configuration/agent/README.md b/doc/configuration/agent/README.md index c76fdaca5e..08bf61ea4d 100644 --- a/doc/configuration/agent/README.md +++ b/doc/configuration/agent/README.md @@ -52,15 +52,15 @@ java -Dbithon.application.name=bithon-server -Dbithon.application.env=local -jar ## Environment variables -Configurations can also be set by environment variables. This is helpful when the agent is loaded in docker environment. +Configurations can also be set by environment variables. This is helpful when the agent is loaded in docker/container environment. ```text -export bithon_application_name=bithon-server -export bithon_application_env=local +export BITHON_APPLICATION_NAME=bithon-server +export BITHON_APPLICATION_ENV=local java -jar bithon-server.jar ``` -Note that all environment variables are in underscore mode. +Note that all environment variables are in underscore mode and uppercase. ## Dynamic Configuration