Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Stack Trace tracking for Instrumentation #701

Merged
merged 4 commits into from
Feb 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
Expand All @@ -32,6 +33,8 @@
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicReference;

import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationRuntimeException;
Expand Down Expand Up @@ -107,14 +110,25 @@ public class ConcurrentCompositeConfiguration extends ConcurrentMapConfiguration

public static final String ENABLE_STACK_TRACE = "archaius_enable_stack_trace";
public static final String ENABLE_INSTRUMENTATION = "archaius_enable_instrumentation";
public static final String STACK_TRACE_ENABLED_PROPERTIES = "archaius_stack_trace_enabled_properties";
private final boolean enableStackTrace = Boolean.parseBoolean(System.getProperty(ENABLE_STACK_TRACE));
private final boolean enableInstrumentation = Boolean.parseBoolean(System.getProperty(ENABLE_INSTRUMENTATION));
private final Set<String> stackTraceEnabledProperties = convertStringFlag(System.getProperty(STACK_TRACE_ENABLED_PROPERTIES));

private Map<String, AbstractConfiguration> namedConfigurations = new ConcurrentHashMap<>();

private final Map<String, Integer> stackTraces = new ConcurrentHashMap<>();
private final Map<String, Set<String>> stackTracesAndProperties = new ConcurrentHashMap<>();
private final AtomicReference<Set<String>> usedPropertiesRef = new AtomicReference<>(ConcurrentHashMap.newKeySet());

private Set<String> convertStringFlag(String properties) {
if (properties == null) {
rgallardo-netflix marked this conversation as resolved.
Show resolved Hide resolved
return Collections.emptySet();
}

return ImmutableSet.copyOf(Splitter.on(',').trimResults().omitEmptyStrings().split(properties));
}

public Set<String> getUsedProperties() {
return Collections.unmodifiableSet(new HashSet<>(usedPropertiesRef.get()));
}
Expand All @@ -124,6 +138,15 @@ public Set<String> getAndClearUsedProperties() {
return Collections.unmodifiableSet(ret);
}

public Map<String, Integer> getInstrumentationStackTraces() {
return Collections.unmodifiableMap(new HashMap<>(stackTraces));
}

public Map<String, Set<String>> getInstrumentationStackTracesAndProperties() {
// Shallow copy
return Collections.unmodifiableMap(new HashMap<>(stackTracesAndProperties));
}

private List<AbstractConfiguration> configList = new CopyOnWriteArrayList<AbstractConfiguration>();

private static final Logger logger = LoggerFactory.getLogger(ConcurrentCompositeConfiguration.class);
Expand Down Expand Up @@ -589,13 +612,30 @@ private Object getProperty(String key, boolean instrument)
public void recordUsage(String key) {
if (enableInstrumentation) {
usedPropertiesRef.get().add(key);
if (enableStackTrace) {
boolean isTrackedProperty = stackTraceEnabledProperties.contains(key);
if (enableStackTrace || isTrackedProperty) {
String trace = Arrays.toString(Thread.currentThread().getStackTrace());
stackTraces.merge(trace, 1, (v1, v2) -> v1 + 1);
if (enableStackTrace) {
stackTraces.merge(trace, 1, (v1, v2) -> v1 + 1);
rgallardo-netflix marked this conversation as resolved.
Show resolved Hide resolved
}
if (isTrackedProperty) {
stackTracesAndProperties.merge(trace, createSet(key), this::union);
}
}
}
}

private Set<String> union(Set<String> s1, Set<String> s2) {
s1.addAll(s2);
return s1;
}

private Set<String> createSet(String s) {
Set<String> ret = new HashSet<>();
ret.add(s);
return ret;
}

/**
* Get all the keys contained by sub configurations.
*
Expand Down
Loading