Skip to content

Commit

Permalink
Augment instrumentation stack trace functionality and surface it
Browse files Browse the repository at this point in the history
  • Loading branch information
akang31 committed Feb 6, 2024
1 parent ee954c2 commit 3bd8dc9
Showing 1 changed file with 18 additions and 1 deletion.
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 Down Expand Up @@ -106,15 +107,26 @@ public class ConcurrentCompositeConfiguration extends ConcurrentMapConfiguration
implements AggregatedConfiguration, ConfigurationListener, Cloneable {

public static final String ENABLE_STACK_TRACE = "archaius_enable_stack_trace";
public static final String STACK_TRACE_ENABLED_PROPERTIES = "archaius_stack_trace_enabled_properties";
public static final String ENABLE_INSTRUMENTATION = "archaius_enable_instrumentation";
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 AtomicReference<Set<String>> usedPropertiesRef = new AtomicReference<>(ConcurrentHashMap.newKeySet());

private Set<String> convertStringFlag(String properties) {
if (properties == null) {
return Collections.emptySet();
}
Set<String> ret = new HashSet<>();
Collections.addAll(ret, properties.split(","));
return ret;
}

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

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

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

private static final Logger logger = LoggerFactory.getLogger(ConcurrentCompositeConfiguration.class);
Expand Down Expand Up @@ -589,7 +605,8 @@ private Object getProperty(String key, boolean instrument)
public void recordUsage(String key) {
if (enableInstrumentation) {
usedPropertiesRef.get().add(key);
if (enableStackTrace) {
if (enableStackTrace
|| (!stackTraceEnabledProperties.isEmpty() && stackTraceEnabledProperties.contains(key))) {
String trace = Arrays.toString(Thread.currentThread().getStackTrace());
stackTraces.merge(trace, 1, (v1, v2) -> v1 + 1);
}
Expand Down

0 comments on commit 3bd8dc9

Please sign in to comment.