diff --git a/changelog/@unreleased/pr-1717.v2.yml b/changelog/@unreleased/pr-1717.v2.yml new file mode 100644 index 000000000..9bd38f0e7 --- /dev/null +++ b/changelog/@unreleased/pr-1717.v2.yml @@ -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 diff --git a/tritium-core/src/main/java/com/palantir/tritium/event/InstrumentationProperties.java b/tritium-core/src/main/java/com/palantir/tritium/event/InstrumentationProperties.java index 2cc530cb7..038bb69de 100644 --- a/tritium-core/src/main/java/com/palantir/tritium/event/InstrumentationProperties.java +++ b/tritium-core/src/main/java/com/palantir/tritium/event/InstrumentationProperties.java @@ -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); @@ -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 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)); } /** @@ -90,10 +88,6 @@ private static Supplier> createSupplier() { InstrumentationProperties::createInstrumentationSystemProperties, 1L, TimeUnit.MINUTES); } - private static Map instrumentationProperties() { - return instrumentationProperties.get(); - } - private static ImmutableMap createInstrumentationSystemProperties() { /* * Since system properties are backed by a java.util.Hashtable, they can be @@ -112,7 +106,9 @@ private static ImmutableMap 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; } }