Skip to content

Commit

Permalink
feat: implemented change requests
Browse files Browse the repository at this point in the history
  • Loading branch information
levinkerschberger committed Oct 28, 2024
1 parent 6e5e5e9 commit 1030462
Showing 1 changed file with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
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 Down Expand Up @@ -35,6 +37,8 @@ public class PropertiesResolver {
"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 @@ -107,14 +111,33 @@ public static Duration getPollingInterval() {
*/
public static Map<String, String> getAttributes() {

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

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

return attributes;
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))
Expand All @@ -125,6 +148,16 @@ private static Map<String, String> getAttributesFromEnv() {
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))
Expand Down

0 comments on commit 1030462

Please sign in to comment.