diff --git a/config/pom.xml b/config/pom.xml index 57d9b4152..3b9945e9d 100644 --- a/config/pom.xml +++ b/config/pom.xml @@ -53,10 +53,13 @@ ${project.version} test - org.yaml snakeyaml + + com.google.guava + guava + diff --git a/config/src/main/java/com/megaease/easeagent/config/ConfigFactory.java b/config/src/main/java/com/megaease/easeagent/config/ConfigFactory.java index a0bfa020d..b55f2a8c7 100644 --- a/config/src/main/java/com/megaease/easeagent/config/ConfigFactory.java +++ b/config/src/main/java/com/megaease/easeagent/config/ConfigFactory.java @@ -18,14 +18,12 @@ package com.megaease.easeagent.config; import com.google.common.base.CaseFormat; -import com.google.common.base.Strings; import com.megaease.easeagent.log4j2.Logger; import com.megaease.easeagent.log4j2.LoggerFactory; import com.megaease.easeagent.plugin.utils.ImmutableMap; import com.megaease.easeagent.plugin.utils.SystemEnv; import com.megaease.easeagent.plugin.utils.common.JsonUtil; import com.megaease.easeagent.plugin.utils.common.StringUtils; -import io.opentelemetry.sdk.resources.OtelSdkConfigs; import java.io.File; import java.util.HashMap; @@ -37,7 +35,8 @@ public class ConfigFactory { private static final String CONFIG_PROP_FILE = "agent.properties"; private static final String CONFIG_YAML_FILE = "agent.yaml"; - public static final String AGENT_CONFIG_PATH = "config.path"; + public static final String AGENT_CONFIG_PATH_PROP_KEY = "easeagent.config.path"; + public static final String AGENT_CONFIG_PATH_ENV_KEY = "EASEAGENT_CONFIG_PATH"; public static final String AGENT_SERVICE = "name"; public static final String AGENT_SYSTEM = "system"; @@ -49,7 +48,6 @@ public class ConfigFactory { private static final Map AGENT_CONFIG_KEYS_TO_PROPS = ImmutableMap.builder() - .put("easeagent.config.path", AGENT_CONFIG_PATH) .put("easeagent.name", AGENT_SERVICE) .put("easeagent.system", AGENT_SYSTEM) .put("easeagent.server.port", AGENT_SERVER_PORT) @@ -113,21 +111,27 @@ private ConfigFactory() { } /** - * load config from environment variables and java properties and default config file. + * Get config file path from system properties or environment variables *

* user special config: * -Deaseagent.config.path=/easeagent/agent.properties || export EASEAGENT_CONFIG_PATH=/easeagent/agent.properties * or OTEL config format * -Dotel.javaagent.configuration-file=/easeagent/agent.properties || export OTEL_JAVAAGENT_CONFIGURATION_FILE=/easeagent/agent.properties */ - public static GlobalConfigs loadConfigs(ClassLoader loader) { - Map envCfg = updateEnvCfg(); - String configFile = envCfg.get(AGENT_CONFIG_PATH); - if (StringUtils.isEmpty(configFile)) { - envCfg = OtelSdkConfigs.updateEnvCfg(); - configFile = envCfg.get(AGENT_CONFIG_PATH); + public static String getConfigPath() { + // get config path from -Deaseagent.config.path=xxx + String path = System.getProperty(AGENT_CONFIG_PATH_PROP_KEY); + if (StringUtils.isEmpty(path)) { + // get config path from export EASEAGENT_CONFIG_PATH=xxx + path = SystemEnv.get(AGENT_CONFIG_PATH_ENV_KEY); } - return loadConfigs(configFile, loader); + + if (StringUtils.isEmpty(path)) { + // get config path from OTEL configuration + // eg: -Dotel.javaagent.configuration-file=/easeagent/agent.properties || export OTEL_JAVAAGENT_CONFIGURATION_FILE=/easeagent/agent.properties + path = OtelSdkConfigs.getConfigPath(); + } + return path; } public static GlobalConfigs loadConfigs(String pathname, ClassLoader loader) { diff --git a/plugin-api/src/main/java/io/opentelemetry/sdk/resources/OtelSdkConfigs.java b/config/src/main/java/com/megaease/easeagent/config/OtelSdkConfigs.java similarity index 73% rename from plugin-api/src/main/java/io/opentelemetry/sdk/resources/OtelSdkConfigs.java rename to config/src/main/java/com/megaease/easeagent/config/OtelSdkConfigs.java index 075b74845..5d5e36ec7 100644 --- a/plugin-api/src/main/java/io/opentelemetry/sdk/resources/OtelSdkConfigs.java +++ b/config/src/main/java/com/megaease/easeagent/config/OtelSdkConfigs.java @@ -1,8 +1,21 @@ /* - * Copyright (c) 2023, Inspireso and/or its affiliates. All rights reserved. + * Copyright (c) 2021, MegaEase + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -package io.opentelemetry.sdk.resources; +package com.megaease.easeagent.config; import com.google.common.base.CaseFormat; import com.google.common.base.Splitter; @@ -21,7 +34,10 @@ * {@see https://github.com/open-telemetry/opentelemetry-java/blob/main/sdk-extensions/autoconfigure/README.md#disabling-opentelemetrysdk} */ public class OtelSdkConfigs { - static final String OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES"; + private static final String OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES"; + + private static final String CONFIG_PATH_PROP_KEY = "otel.javaagent.configuration-file"; + private static final String CONFIG_PATH_ENV_KEY = "OTEL_JAVAAGENT_CONFIGURATION_FILE"; private static final Splitter.MapSplitter OTEL_RESOURCE_ATTRIBUTES_SPLITTER = Splitter.on(",") @@ -30,7 +46,6 @@ public class OtelSdkConfigs { private static final Map SDK_ATTRIBUTES_TO_EASE_AGENT_PROPS = ImmutableMap.builder() - .put("javaagent.configuration-file", "config.path") .put("sdk.disabled", "easeagent.server.enabled") .put("service.name", "name") //"easeagent.name" .put("service.namespace", "system") //"easeagent.system" @@ -60,12 +75,25 @@ public class OtelSdkConfigs { } } + /** + * Get config path from java properties or environment variables + */ + static String getConfigPath() { + String path = System.getProperty(CONFIG_PATH_PROP_KEY); + if (StringUtils.isEmpty(path)) { + path = SystemEnv.get(CONFIG_PATH_ENV_KEY); + } + + return path; + } + + /** * update config value from environment variables and java properties *

* java properties > environment variables > OTEL_RESOURCE_ATTRIBUTES */ - public static Map updateEnvCfg() { + static Map updateEnvCfg() { Map envCfg = new TreeMap<>(); String configEnv = SystemEnv.get(OTEL_RESOURCE_ATTRIBUTES); diff --git a/config/src/test/java/com/megaease/easeagent/config/ConfigFactoryTest.java b/config/src/test/java/com/megaease/easeagent/config/ConfigFactoryTest.java index a4ef20c50..14796e3ef 100644 --- a/config/src/test/java/com/megaease/easeagent/config/ConfigFactoryTest.java +++ b/config/src/test/java/com/megaease/easeagent/config/ConfigFactoryTest.java @@ -57,13 +57,13 @@ public void test_loadConfigs() { mockSystemEnv.when(() -> SystemEnv.get("EASEAGENT_NAME")).thenReturn("service1"); mockSystemEnv.when(() -> SystemEnv.get("EASEAGENT_SYSTEM")).thenReturn("system1"); - Configs config = ConfigFactory.loadConfigs(this.getClass().getClassLoader()); + Configs config = ConfigFactory.loadConfigs(null, this.getClass().getClassLoader()); assertEquals("service1", config.getString(AGENT_SERVICE)); assertEquals("system1", config.getString(AGENT_SYSTEM)); System.setProperty("easeagent.name", "service2"); System.setProperty("easeagent.system", "system2"); - config = ConfigFactory.loadConfigs(this.getClass().getClassLoader()); + config = ConfigFactory.loadConfigs(null, this.getClass().getClassLoader()); assertEquals("service2", config.getString(AGENT_SERVICE)); assertEquals("system2", config.getString(AGENT_SYSTEM)); } @@ -76,9 +76,8 @@ public void test_loadConfigsFromUserSpec() throws URISyntaxException { try (MockedStatic mockSystemEnv = Mockito.mockStatic(SystemEnv.class)) { mockSystemEnv.when(() -> SystemEnv.get("EASEAGENT_CONFIG_PATH")).thenReturn(userSpec); - Configs config = ConfigFactory.loadConfigs(this.getClass().getClassLoader()); - assertEquals("user-spec", config.getString(AGENT_SERVICE)); - assertEquals("system-spec", config.getString(AGENT_SYSTEM)); + String path = ConfigFactory.getConfigPath(); + assertEquals(userSpec, path); } } @@ -86,12 +85,11 @@ public void test_loadConfigsFromUserSpec() throws URISyntaxException { @Test public void test_loadConfigsFromOtelUserSpec() throws URISyntaxException { String userSpec = new File(this.getClass().getClassLoader().getResource("user-spec.properties").toURI()).getPath(); + try (MockedStatic mockSystemEnv = Mockito.mockStatic(SystemEnv.class)) { mockSystemEnv.when(() -> SystemEnv.get("OTEL_JAVAAGENT_CONFIGURATION_FILE")).thenReturn(userSpec); - Configs config = ConfigFactory.loadConfigs(this.getClass().getClassLoader()); - assertEquals("user-spec", config.getString(AGENT_SERVICE)); - assertEquals("system-spec", config.getString(AGENT_SYSTEM)); - + String path = ConfigFactory.getConfigPath(); + assertEquals(userSpec, path); } } } diff --git a/plugin-api/src/test/java/io/opentelemetry/sdk/resources/OtelSdkConfigsTest.java b/config/src/test/java/com/megaease/easeagent/config/OtelSdkConfigsTest.java similarity index 59% rename from plugin-api/src/test/java/io/opentelemetry/sdk/resources/OtelSdkConfigsTest.java rename to config/src/test/java/com/megaease/easeagent/config/OtelSdkConfigsTest.java index ed9d3c5fa..4b0eb78da 100644 --- a/plugin-api/src/test/java/io/opentelemetry/sdk/resources/OtelSdkConfigsTest.java +++ b/config/src/test/java/com/megaease/easeagent/config/OtelSdkConfigsTest.java @@ -1,28 +1,36 @@ /* - * Copyright (c) 2023, Inspireso and/or its affiliates. All rights reserved. + * Copyright (c) 2021, MegaEase + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -package io.opentelemetry.sdk.resources; +package com.megaease.easeagent.config; -import com.megaease.easeagent.plugin.api.config.AutoRefreshConfigSupplier; -import com.megaease.easeagent.plugin.tools.config.AutoRefreshConfigSupplierTest; import com.megaease.easeagent.plugin.utils.SystemEnv; import org.junit.Assert; import org.junit.Test; -import java.lang.reflect.Type; import java.util.Map; -import static io.opentelemetry.sdk.resources.OtelSdkConfigs.OTEL_RESOURCE_ATTRIBUTES; -import static org.junit.Assert.*; public class OtelSdkConfigsTest { @Test public void updateEnvCfg() { //value from system env "OTEL_RESOURCE_ATTRIBUTES - String attributes="service.name=service1,service.namespace=namespace1"; - SystemEnv.set(OTEL_RESOURCE_ATTRIBUTES, attributes); + String attributes = "service.name=service1,service.namespace=namespace1"; + SystemEnv.set("OTEL_RESOURCE_ATTRIBUTES", attributes); Map envCfg = OtelSdkConfigs.updateEnvCfg(); Assert.assertEquals("service1", envCfg.get("name")); Assert.assertEquals("namespace1", envCfg.get("system")); diff --git a/core/src/main/java/com/megaease/easeagent/core/Bootstrap.java b/core/src/main/java/com/megaease/easeagent/core/Bootstrap.java index 21b34fd92..74dcbb9b1 100644 --- a/core/src/main/java/com/megaease/easeagent/core/Bootstrap.java +++ b/core/src/main/java/com/megaease/easeagent/core/Bootstrap.java @@ -37,6 +37,7 @@ import com.megaease.easeagent.plugin.bridge.AgentInfo; import com.megaease.easeagent.plugin.bridge.EaseAgent; import com.megaease.easeagent.plugin.report.AgentReport; +import com.megaease.easeagent.plugin.utils.common.StringUtils; import com.megaease.easeagent.report.AgentReportAware; import com.megaease.easeagent.report.DefaultAgentReport; import lombok.SneakyThrows; @@ -88,10 +89,16 @@ public static void start(String args, Instrumentation inst, String javaAgentJarP LOGGER.debug("Injected class: {}", bootstrapClassSet); } + // initiate configuration + String configPath = ConfigFactory.getConfigPath(); + if (StringUtils.isEmpty(configPath)) { + configPath = args; + } + ClassLoader classLoader = Bootstrap.class.getClassLoader(); final AgentInfo agentInfo = AgentInfoFactory.loadAgentInfo(classLoader); EaseAgent.agentInfo = agentInfo; - final GlobalConfigs conf = ConfigFactory.loadConfigs(classLoader); + final GlobalConfigs conf = ConfigFactory.loadConfigs(configPath, classLoader); wrapConfig(conf); // loader check @@ -133,6 +140,8 @@ private static void initHttpServer(Configs conf) { if (port == null) { port = DEF_AGENT_SERVER_PORT; } + String portStr = System.getProperty(AGENT_SERVER_PORT_KEY, String.valueOf(port)); + port = Integer.parseInt(portStr); AgentHttpServer agentHttpServer = new AgentHttpServer(port); diff --git a/plugin-api/pom.xml b/plugin-api/pom.xml index daafa50e4..bef7d611f 100644 --- a/plugin-api/pom.xml +++ b/plugin-api/pom.xml @@ -29,10 +29,6 @@ plugin-api - - com.google.guava - guava - com.squareup javapoet