Skip to content

Commit 022eb63

Browse files
committed
Caching the primaryInstrumentationName
Caching the primaryInstrumentationName to avoid repeatedly calling instrumentationNames() and constantly allocating a String[]
1 parent 02cc483 commit 022eb63

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/decorator/HttpServerDecorator.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,30 @@ public abstract class HttpServerDecorator<REQUEST, CONNECTION, RESPONSE, REQUEST
8181
private final boolean traceClientIpResolverEnabled =
8282
Config.get().isTraceClientIpResolverEnabled();
8383

84+
// Used to cache the primary instrumentation name
85+
// Deliberately not synchronized or volatile, reading a state null and
86+
// repeating the name determination logic is fine
87+
private String cachedPrimaryInstrumentationName = null;
88+
89+
/**
90+
* Wrapper around instrumentationNames() that caches the result to avoid repeatedly allocating a
91+
* String[]
92+
*/
93+
protected final String primaryInstrumentationName() {
94+
String primaryName = cachedPrimaryInstrumentationName;
95+
if (primaryName != null) return primaryName;
96+
97+
String[] instrumentationNames = instrumentationNames();
98+
primaryName =
99+
instrumentationNames != null && instrumentationNames.length > 0
100+
? instrumentationNames[0]
101+
: DEFAULT_INSTRUMENTATION_NAME;
102+
103+
cachedPrimaryInstrumentationName = primaryName;
104+
105+
return primaryName;
106+
}
107+
84108
protected abstract AgentPropagation.ContextVisitor<REQUEST_CARRIER> getter();
85109

86110
protected abstract AgentPropagation.ContextVisitor<RESPONSE> responseGetter();
@@ -153,11 +177,7 @@ public Context extract(REQUEST_CARRIER carrier) {
153177
* @return A new context bundling the span, child of the given parent context.
154178
*/
155179
public Context startSpan(REQUEST_CARRIER carrier, Context parentContext) {
156-
String[] instrumentationNames = instrumentationNames();
157-
String instrumentationName =
158-
instrumentationNames != null && instrumentationNames.length > 0
159-
? instrumentationNames[0]
160-
: DEFAULT_INSTRUMENTATION_NAME;
180+
String instrumentationName = primaryInstrumentationName();
161181
AgentSpanContext extracted = getExtractedSpanContext(parentContext);
162182
// Call IG callbacks
163183
extracted = callIGCallbackStart(extracted);

0 commit comments

Comments
 (0)