Skip to content

Commit

Permalink
refactor: Optimizing code for reading agent configuration path.
Browse files Browse the repository at this point in the history
add method ConfigFactory#getConfigPath for Bootstrap
  • Loading branch information
lanxenet committed Sep 13, 2023
1 parent 6d5f7a1 commit 2e94aad
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 41 deletions.
5 changes: 4 additions & 1 deletion config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand All @@ -49,7 +48,6 @@ public class ConfigFactory {

private static final Map<String, String> AGENT_CONFIG_KEYS_TO_PROPS =
ImmutableMap.<String, String>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)
Expand Down Expand Up @@ -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
* <p>
* 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<String, String> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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(",")
Expand All @@ -30,7 +46,6 @@ public class OtelSdkConfigs {

private static final Map<String, String> SDK_ATTRIBUTES_TO_EASE_AGENT_PROPS =
ImmutableMap.<String, String>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"
Expand Down Expand Up @@ -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
* <p>
* java properties > environment variables > OTEL_RESOURCE_ATTRIBUTES
*/
public static Map<String, String> updateEnvCfg() {
static Map<String, String> updateEnvCfg() {
Map<String, String> envCfg = new TreeMap<>();

String configEnv = SystemEnv.get(OTEL_RESOURCE_ATTRIBUTES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -76,22 +76,20 @@ public void test_loadConfigsFromUserSpec() throws URISyntaxException {

try (MockedStatic<SystemEnv> 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);
}
}


@Test
public void test_loadConfigsFromOtelUserSpec() throws URISyntaxException {
String userSpec = new File(this.getClass().getClassLoader().getResource("user-spec.properties").toURI()).getPath();

try (MockedStatic<SystemEnv> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> envCfg = OtelSdkConfigs.updateEnvCfg();
Assert.assertEquals("service1", envCfg.get("name"));
Assert.assertEquals("namespace1", envCfg.get("system"));
Expand Down
11 changes: 10 additions & 1 deletion core/src/main/java/com/megaease/easeagent/core/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 0 additions & 4 deletions plugin-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
<artifactId>plugin-api</artifactId>

<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
Expand Down

0 comments on commit 2e94aad

Please sign in to comment.