Skip to content

Commit

Permalink
Merge pull request #701 from Netflix/1.x-instrumentation-stack-traces
Browse files Browse the repository at this point in the history
Improve Stack Trace tracking for Instrumentation
  • Loading branch information
akang31 authored Feb 7, 2024
2 parents ee954c2 + 5bf66d7 commit b74bd03
Showing 1 changed file with 42 additions and 2 deletions.
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) {
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);
}
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

0 comments on commit b74bd03

Please sign in to comment.