Skip to content

Commit

Permalink
feature: Add agent attributes to identify agents in configuration ser…
Browse files Browse the repository at this point in the history
…ver (#25)

* feat: add attributes to PropertiesResolver

* feat: add attributes to AgentInfo

* feat: implemented change requests
  • Loading branch information
levinkerschberger authored Oct 29, 2024
1 parent c58d6ea commit 6fc5c59
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import io.opentelemetry.javaagent.bootstrap.JavaagentFileHolder;
import java.io.File;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This resolver provides configurable properties or their default values. Currently, it is possible
Expand All @@ -28,6 +33,12 @@ public class PropertiesResolver {
public static final String POLLING_INTERVAL_ENV_PROPERTY =
"INSPECTIT_CONFIG_HTTP_POLLING_INTERVAL";

public static final String ATTRIBUTES_SYSTEM_PROPERTY_PREFIX =
"inspectit.config.http.attributes.";
public static final String ATTRIBUTES_ENV_PROPERTY_PREFIX = "INSPECTIT_CONFIG_HTTP_ATTRIBUTES_";

private static final Logger log = LoggerFactory.getLogger(PropertiesResolver.class);

private PropertiesResolver() {}

/**
Expand Down Expand Up @@ -90,4 +101,70 @@ public static Duration getPollingInterval() {
? Duration.parse(pollingIntervalEnvProperty)
: Duration.ofSeconds(10);
}

/**
* Get the attributes which should be sent to the configuration server. When an attribute is
* configured via both system properties and environmental properties, the system property will be
* used.
*
* @return the attributes to be sent to the configuration server
*/
public static Map<String, String> getAttributes() {

try {
Map<String, String> attributes = new HashMap<>(getAttributesFromEnv());

// Override with system properties where available
attributes.putAll(getAttributesFromSystemProperties());

return attributes;
} catch (Exception e) {
log.error(
"Failed to load agent attributes. Continuing with empty attributes for failsafe initialization. Error: {}",
e.getMessage(),
e);
return new HashMap<>();
}
}

/**
* Retrieves environment variables that start with {@code ATTRIBUTES_ENV_PROPERTY_PREFIX} and maps
* them to a new {@link Map} with transformed keys.
*
* <p>The method filters all environment variables to include only those with keys that start with
* {@code ATTRIBUTES_ENV_PROPERTY_PREFIX}. The prefix is removed, and the keys are converted to
* lowercase in the resulting map.
*
* @return a {@link Map} containing environment variables with the prefix removed and keys
* transformed to lowercase.
*/
private static Map<String, String> getAttributesFromEnv() {
return System.getenv().entrySet().stream()
.filter(entry -> entry.getKey().startsWith(ATTRIBUTES_ENV_PROPERTY_PREFIX))
.collect(
Collectors.toMap(
entry ->
entry.getKey().substring(ATTRIBUTES_ENV_PROPERTY_PREFIX.length()).toLowerCase(),
Map.Entry::getValue));
}

/**
* Retrieves system properties that start with {@code ATTRIBUTES_ENV_PROPERTY_PREFIX} and maps
* them to a new {@link Map} with transformed keys.
*
* <p>This method filters the system properties to include only those with keys that start with
* {@code ATTRIBUTES_SYSTEM_PROPERTY_PREFIX}. The prefix is removed in the resulting map, with
* keys unchanged in case.
*
* @return a {@link Map} containing system properties with the prefix removed.
*/
private static Map<String, String> getAttributesFromSystemProperties() {
return System.getProperties().entrySet().stream()
.filter(entry -> entry.getKey().toString().startsWith(ATTRIBUTES_SYSTEM_PROPERTY_PREFIX))
.collect(
Collectors.toMap(
entry ->
entry.getKey().toString().substring(ATTRIBUTES_SYSTEM_PROPERTY_PREFIX.length()),
entry -> entry.getValue().toString()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import io.opentelemetry.javaagent.tooling.AgentVersion;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Map;
import java.util.Objects;
import rocks.inspectit.gepard.agent.internal.properties.PropertiesResolver;

/** Meta-information about the current agent */
public class AgentInfo {
Expand All @@ -31,6 +33,8 @@ public class AgentInfo {

private final long pid;

private final Map<String, String> attributes;

private AgentInfo() {
this.serviceName = getServiceNameFromSdk();
this.gepardVersion = "0.0.1";
Expand All @@ -39,6 +43,7 @@ private AgentInfo() {
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
this.startTime = runtime.getStartTime();
this.pid = runtime.getPid();
this.attributes = PropertiesResolver.getAttributes();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static rocks.inspectit.gepard.agent.internal.properties.PropertiesResolver.*;

import java.time.Duration;
import java.util.Map;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

Expand All @@ -19,6 +20,9 @@ class PropertiesResolverTest {

private static final String PERSISTENCE_FILE = "/path/to/file.json";

private static final String TEST_ATTRIBUTE_HOST = "localhost";
private static final String TEST_ATTRIBUTE_PORT = "8080";

@Nested
class ServerUrl {

Expand Down Expand Up @@ -165,4 +169,44 @@ void resolverReturnsDefaultFileIfNoPropertyExists() {
assertEquals(expected, file);
}
}

@Nested
class Attributes {
@Test
void resolverReturnsAttributesIfSystemPropertyExists() throws Exception {
restoreSystemProperties(
() -> {
System.setProperty(ATTRIBUTES_SYSTEM_PROPERTY_PREFIX + "host", TEST_ATTRIBUTE_HOST);

Map<String, String> attributes = PropertiesResolver.getAttributes();
assertTrue(attributes.containsKey("host"));
assertEquals(TEST_ATTRIBUTE_HOST, attributes.get("host"));
});
}

@Test
void resolverReturnsAttributesIfEnvironmentPropertyExists() throws Exception {
Map<String, String> attributes =
withEnvironmentVariable(ATTRIBUTES_ENV_PROPERTY_PREFIX + "PORT", TEST_ATTRIBUTE_PORT)
.execute(PropertiesResolver::getAttributes);
assertTrue(attributes.containsKey("port"));
assertEquals(TEST_ATTRIBUTE_PORT, attributes.get("port"));
}

@Test
void resolverReturnsSystemPropertyIfSystemAndEnvPropertyExist() throws Exception {
String envTestHost = TEST_ATTRIBUTE_HOST + "1";
restoreSystemProperties(
() -> {
System.setProperty(ATTRIBUTES_SYSTEM_PROPERTY_PREFIX + "host", TEST_ATTRIBUTE_HOST);

Map<String, String> attributes =
withEnvironmentVariable(ATTRIBUTES_ENV_PROPERTY_PREFIX + "HOST", envTestHost)
.execute(PropertiesResolver::getAttributes);

assertTrue(attributes.containsKey("host"));
assertEquals(TEST_ATTRIBUTE_HOST, attributes.get("host"));
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ void agentInformationContainsProperties() throws JsonProcessingException {
assertTrue(info.contains("javaVersion"));
assertTrue(info.contains("startTime"));
assertTrue(info.contains("pid"));
assertTrue(info.contains("attributes"));
}
}

0 comments on commit 6fc5c59

Please sign in to comment.