Skip to content

Commit 3049ac7

Browse files
authored
InstrumentationProperties avoids string concat when no instrument properties are set (#1717)
InstrumentationProperties avoids string concat when no instrument properties are set
1 parent ba11121 commit 3049ac7

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

changelog/@unreleased/pr-1717.v2.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type: improvement
2+
improvement:
3+
description: InstrumentationProperties avoids string concat when no instrument properties
4+
are set
5+
links:
6+
- https://github.com/palantir/tritium/pull/1717

tritium-core/src/main/java/com/palantir/tritium/event/InstrumentationProperties.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.Map;
3030
import java.util.concurrent.TimeUnit;
3131
import java.util.function.Supplier;
32-
import javax.annotation.Nullable;
3332

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

5453
@SuppressWarnings("WeakerAccess") // public API
5554
public static boolean isSpecificEnabled(String name, boolean defaultValue) {
56-
String qualifiedValue = getSpecific(name);
55+
Map<String, String> props = instrumentationProperties.get();
56+
// avoid string concatenation allocation in common case where no `instrument` properties are defined
57+
if (props.isEmpty()) {
58+
return defaultValue;
59+
}
60+
String qualifiedValue = props.get(INSTRUMENT_PREFIX + "." + name);
5761
if (qualifiedValue == null) {
5862
return defaultValue;
5963
}
6064
return "true".equalsIgnoreCase(qualifiedValue);
6165
}
6266

63-
/** Applies the {@link #INSTRUMENT_PREFIX} and returns the current value. */
64-
@Nullable
65-
private static String getSpecific(String name) {
66-
return instrumentationProperties().get(INSTRUMENT_PREFIX + "." + name);
67-
}
68-
6967
@SuppressWarnings("WeakerAccess") // public API
7068
public static boolean isGloballyEnabled() {
7169
return !isGloballyDisabled();
7270
}
7371

7472
private static boolean isGloballyDisabled() {
75-
return "false".equalsIgnoreCase(instrumentationProperties().get(INSTRUMENT_PREFIX));
73+
return "false".equalsIgnoreCase(instrumentationProperties.get().get(INSTRUMENT_PREFIX));
7674
}
7775

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

93-
private static Map<String, String> instrumentationProperties() {
94-
return instrumentationProperties.get();
95-
}
96-
9791
private static ImmutableMap<String, String> createInstrumentationSystemProperties() {
9892
/*
9993
* Since system properties are backed by a java.util.Hashtable, they can be
@@ -112,7 +106,9 @@ private static ImmutableMap<String, String> createInstrumentationSystemPropertie
112106
&& String.valueOf(entry.getKey()).startsWith(INSTRUMENT_PREFIX))
113107
.collect(ImmutableMap.toImmutableMap(
114108
entry -> String.valueOf(entry.getKey()), entry -> String.valueOf(entry.getValue())));
115-
log.debug("Reloaded instrumentation properties {}", UnsafeArg.of("instrumentationProperties", map));
109+
if (log.isDebugEnabled()) {
110+
log.debug("Reloaded instrumentation properties", UnsafeArg.of("instrumentationProperties", map));
111+
}
116112
return map;
117113
}
118114
}

0 commit comments

Comments
 (0)