Skip to content

Commit

Permalink
InstrumentationProperties avoids string concat when no instrument pro…
Browse files Browse the repository at this point in the history
…perties are set (#1717)

InstrumentationProperties avoids string concat when no instrument properties are set
  • Loading branch information
schlosna authored May 19, 2023
1 parent ba11121 commit 3049ac7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-1717.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: improvement
improvement:
description: InstrumentationProperties avoids string concat when no instrument properties
are set
links:
- https://github.com/palantir/tritium/pull/1717
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import javax.annotation.Nullable;

public final class InstrumentationProperties {
private static final SafeLogger log = SafeLoggerFactory.get(InstrumentationProperties.class);
Expand All @@ -53,26 +52,25 @@ public static boolean isSpecificEnabled(String name) {

@SuppressWarnings("WeakerAccess") // public API
public static boolean isSpecificEnabled(String name, boolean defaultValue) {
String qualifiedValue = getSpecific(name);
Map<String, String> props = instrumentationProperties.get();
// avoid string concatenation allocation in common case where no `instrument` properties are defined
if (props.isEmpty()) {
return defaultValue;
}
String qualifiedValue = props.get(INSTRUMENT_PREFIX + "." + name);
if (qualifiedValue == null) {
return defaultValue;
}
return "true".equalsIgnoreCase(qualifiedValue);
}

/** Applies the {@link #INSTRUMENT_PREFIX} and returns the current value. */
@Nullable
private static String getSpecific(String name) {
return instrumentationProperties().get(INSTRUMENT_PREFIX + "." + name);
}

@SuppressWarnings("WeakerAccess") // public API
public static boolean isGloballyEnabled() {
return !isGloballyDisabled();
}

private static boolean isGloballyDisabled() {
return "false".equalsIgnoreCase(instrumentationProperties().get(INSTRUMENT_PREFIX));
return "false".equalsIgnoreCase(instrumentationProperties.get().get(INSTRUMENT_PREFIX));
}

/**
Expand All @@ -90,10 +88,6 @@ private static Supplier<Map<String, String>> createSupplier() {
InstrumentationProperties::createInstrumentationSystemProperties, 1L, TimeUnit.MINUTES);
}

private static Map<String, String> instrumentationProperties() {
return instrumentationProperties.get();
}

private static ImmutableMap<String, String> createInstrumentationSystemProperties() {
/*
* Since system properties are backed by a java.util.Hashtable, they can be
Expand All @@ -112,7 +106,9 @@ private static ImmutableMap<String, String> createInstrumentationSystemPropertie
&& String.valueOf(entry.getKey()).startsWith(INSTRUMENT_PREFIX))
.collect(ImmutableMap.toImmutableMap(
entry -> String.valueOf(entry.getKey()), entry -> String.valueOf(entry.getValue())));
log.debug("Reloaded instrumentation properties {}", UnsafeArg.of("instrumentationProperties", map));
if (log.isDebugEnabled()) {
log.debug("Reloaded instrumentation properties", UnsafeArg.of("instrumentationProperties", map));
}
return map;
}
}

0 comments on commit 3049ac7

Please sign in to comment.