Skip to content

Commit

Permalink
feat: add attributes to PropertiesResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
levinkerschberger committed Oct 25, 2024
1 parent 489bbbe commit 94cad8a
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
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;

/**
* This resolver provides configurable properties or their default values. Currently, it is possible
Expand All @@ -28,6 +31,10 @@ 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 PropertiesResolver() {}

/**
Expand Down Expand Up @@ -90,4 +97,41 @@ 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() {

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

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

return attributes;
}

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));
}

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 @@ -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"));
});
}
}
}

0 comments on commit 94cad8a

Please sign in to comment.