From 295d1acecec5662caa1c2f7972358379f78a25fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=87=AF?= Date: Sun, 30 Jun 2024 23:23:12 +0800 Subject: [PATCH 1/2] Use uppercase letters for environment variable naming. --- .../bithon/agent/configuration/ConfigurationManager.java | 4 ++-- .../agent/configuration/source/EnvironmentSource.java | 6 ++++-- .../agent/configuration/TestConfigurationManager.java | 8 ++++---- doc/configuration/agent/README.md | 8 ++++---- 4 files changed, 14 insertions(+), 12 deletions(-) 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..8c8062c1fd 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; /** @@ -36,8 +37,9 @@ public static PropertySource build(String envPrefix) { if (name.startsWith(envPrefix) && !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 - .replace('_', '.'); + // Here we need to convert these characters back. + // Additionally, as the env variables are in uppercase, we need to convert them to lowercase. + .replace('_', '.').toLowerCase(Locale.ENGLISH); if (!name.isEmpty()) { propertyText.append(name); propertyText.append('='); 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..e3fa7e85fb 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); @@ -238,9 +238,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 From cf3b9880a0ccfda1c0a153b148f82ce8055affd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=87=AF?= Date: Mon, 1 Jul 2024 22:23:40 +0800 Subject: [PATCH 2/2] Environment variables are case-compatible --- .../source/EnvironmentSource.java | 10 +++---- .../TestConfigurationManager.java | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) 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 8c8062c1fd..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 @@ -32,14 +32,14 @@ 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. - // Additionally, as the env variables are in uppercase, we need to convert them to lowercase. - .replace('_', '.').toLowerCase(Locale.ENGLISH); + // Here we need to convert these characters back + .replace('_', '.'); if (!name.isEmpty()) { propertyText.append(name); propertyText.append('='); 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 e3fa7e85fb..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 @@ -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;