Skip to content

Commit

Permalink
Wrap log statements using varargs to avoid object allocation.
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerbenson committed May 15, 2020
1 parent a7da1d8 commit 4b10796
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public static <T> State setupState(

final TraceScope.Continuation continuation = scope.capture();
if (state.setContinuation(continuation)) {
log.debug("created continuation {} from scope {}, state: {}", continuation, scope, state);
if (log.isDebugEnabled()) {
log.debug("created continuation {} from scope {}, state: {}", continuation, scope, state);
}
} else {
continuation.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private State() {}

public boolean setContinuation(final TraceScope.Continuation continuation) {
final boolean result = continuationRef.compareAndSet(null, continuation);
if (!result) {
if (!result && log.isDebugEnabled()) {
log.debug(
"Failed to set continuation because another continuation is already set {}: new: {}, old: {}",
this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,18 @@ private static void run(final Config config) {
final Map<String, String> globalTags = config.getMergedJmxTags();
final String reporter = getReporter(config);

log.info(
"JMXFetch config: {} {} {} {} {} {} {} {}",
jmxFetchConfigDir,
jmxFetchConfigs,
internalMetricsConfigs,
metricsConfigs,
checkPeriod,
refreshBeansPeriod,
globalTags,
reporter);
if (log.isInfoEnabled()) {
log.info(
"JMXFetch config: {} {} {} {} {} {} {} {}",
jmxFetchConfigDir,
jmxFetchConfigs,
internalMetricsConfigs,
metricsConfigs,
checkPeriod,
refreshBeansPeriod,
globalTags,
reporter);
}

final AppConfig.AppConfigBuilder configBuilder =
AppConfig.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ public ProfilingSystem(
}

public final void start() {
log.info(
"Starting profiling system: startupDelay={}ms, uploadPeriod={}ms, isStartingFirst={}",
startupDelay.toMillis(),
uploadPeriod.toMillis(),
isStartingFirst);
if (log.isInfoEnabled()) {
log.info(
"Starting profiling system: startupDelay={}ms, uploadPeriod={}ms, isStartingFirst={}",
startupDelay.toMillis(),
uploadPeriod.toMillis(),
isStartingFirst);
}

if (isStartingFirst) {
startProfilingRecording();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,14 @@ private void makeUploadRequest(final RecordingType type, final RecordingData dat
// * OkHTTP doesn't provide direct way to send uploads from streams - and workarounds would
// require stream that allows 'repeatable reads' because we may need to resend that data.
final RequestBody body = compression.compress(data.getStream(), expectedRequestSize);
log.debug(
"Uploading recording {} [{}] (Size={}/{} bytes)",
data.getName(),
type,
body.contentLength(),
expectedRequestSize);
if (log.isDebugEnabled()) {
log.debug(
"Uploading recording {} [{}] (Size={}/{} bytes)",
data.getName(),
type,
body.contentLength(),
expectedRequestSize);
}

// The body data is stored in byte array so we naturally get size limit that will fit into int
updateUploadSizesHistory((int) body.contentLength());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,13 @@ public void onError(
final JavaModule module,
final boolean loaded,
final Throwable throwable) {
log.debug(
"Failed to handle {} for transformation on classloader {}: {}",
typeName,
classLoader,
throwable.getMessage());
if (log.isDebugEnabled()) {
log.debug(
"Failed to handle {} for transformation on classloader {}: {}",
typeName,
classLoader,
throwable.getMessage());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,14 @@ public DynamicType.Builder<?> transform(
helperModules.add(new WeakReference<>(javaModule.unwrap()));
}
} catch (final Exception e) {
log.error(
"Error preparing helpers while processing {} for {}. Failed to inject helper classes into instance {}",
typeDescription,
requestingName,
classLoader,
e);
if (log.isErrorEnabled()) {
log.error(
"Error preparing helpers while processing {} for {}. Failed to inject helper classes into instance {}",
typeDescription,
requestingName,
classLoader,
e);
}
throw new RuntimeException(e);
}

Expand All @@ -151,7 +153,7 @@ private Map<String, Class<?>> injectBootstrapClassLoader(
// a reference count -- but for now, starting simple.

// Failures to create a tempDir are propagated as IOException and handled by transform
File tempDir = createTempDir();
final File tempDir = createTempDir();
try {
return ClassInjector.UsingInstrumentation.of(
tempDir,
Expand Down Expand Up @@ -191,15 +193,15 @@ private void ensureModuleCanReadHelperModules(final JavaModule target) {
}
}

private static final File createTempDir() throws IOException {
private static File createTempDir() throws IOException {
return Files.createTempDirectory("datadog-temp-jars").toFile();
}

private static final void deleteTempDir(final File file) {
private static void deleteTempDir(final File file) {
// Not using Files.delete for deleting the directory because failures
// create Exceptions which may prove expensive. Instead using the
// older File API which simply returns a boolean.
boolean deleted = file.delete();
final boolean deleted = file.delete();
if (!deleted) {
file.deleteOnExit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private AgentBuilder.Identified.Extendable injectHelperClasses(
if (helperClassNames.length > 0) {
agentBuilder =
agentBuilder.transform(
new HelperInjector(this.getClass().getSimpleName(), helperClassNames));
new HelperInjector(getClass().getSimpleName(), helperClassNames));
}
return agentBuilder;
}
Expand Down Expand Up @@ -146,21 +146,25 @@ public boolean matches(
if (log.isDebugEnabled()) {
final List<Reference.Mismatch> mismatches =
muzzle.getMismatchedReferenceSources(classLoader);
log.debug(
"Instrumentation muzzled: {} -- {} on {}",
instrumentationNames,
Instrumenter.Default.this.getClass().getName(),
classLoader);
if (log.isDebugEnabled()) {
log.debug(
"Instrumentation muzzled: {} -- {} on {}",
instrumentationNames,
Instrumenter.Default.this.getClass().getName(),
classLoader);
}
for (final Reference.Mismatch mismatch : mismatches) {
log.debug("-- {}", mismatch);
}
}
} else {
log.debug(
"Applying instrumentation: {} -- {} on {}",
instrumentationPrimaryName,
Instrumenter.Default.this.getClass().getName(),
classLoader);
if (log.isDebugEnabled()) {
log.debug(
"Applying instrumentation: {} -- {} on {}",
instrumentationPrimaryName,
Instrumenter.Default.this.getClass().getName(),
classLoader);
}
}
return isMatch;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ public static void logAllVersions() {
"dd-java-agent - version: {}",
getVersionString(
ClassLoader.getSystemClassLoader().getResourceAsStream("dd-java-agent.version")));
log.debug(
"Running on Java {}. JVM {} - {} - {}",
System.getProperty("java.version"),
System.getProperty("java.vm.name"),
System.getProperty("java.vm.vendor"),
System.getProperty("java.vm.version"));
if (log.isDebugEnabled()) {
log.debug(
"Running on Java {}. JVM {} - {} - {}",
System.getProperty("java.version"),
System.getProperty("java.vm.name"),
System.getProperty("java.vm.vendor"),
System.getProperty("java.vm.version"));
}
}

private static String getVersionString(final InputStream stream) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ static TypeDescription safeAsErasure(final TypeDefinition typeDefinition) {
try {
return typeDefinition.asErasure();
} catch (final Exception e) {
log.debug(
"{} trying to get erasure for target {}: {}",
e.getClass().getSimpleName(),
safeTypeDefinitionName(typeDefinition),
e.getMessage());
if (log.isDebugEnabled()) {
log.debug(
"{} trying to get erasure for target {}: {}",
e.getClass().getSimpleName(),
safeTypeDefinitionName(typeDefinition),
e.getMessage());
}
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,13 @@ private List<TypeDefinition> safeGetInterfaces(final TypeDefinition typeDefiniti
interfaceTypes.add(interfaceIter.next());
}
} catch (final Exception e) {
log.debug(
"{} trying to get interfaces for target {}: {}",
e.getClass().getSimpleName(),
safeTypeDefinitionName(typeDefinition),
e.getMessage());
if (log.isDebugEnabled()) {
log.debug(
"{} trying to get interfaces for target {}: {}",
e.getClass().getSimpleName(),
safeTypeDefinitionName(typeDefinition),
e.getMessage());
}
}
return interfaceTypes;
}
Expand All @@ -117,11 +119,13 @@ static TypeDefinition safeGetSuperClass(final TypeDefinition typeDefinition) {
try {
return typeDefinition.getSuperClass();
} catch (final Exception e) {
log.debug(
"{} trying to get super class for target {}: {}",
e.getClass().getSimpleName(),
safeTypeDefinitionName(typeDefinition),
e.getMessage());
if (log.isDebugEnabled()) {
log.debug(
"{} trying to get super class for target {}: {}",
e.getClass().getSimpleName(),
safeTypeDefinitionName(typeDefinition),
e.getMessage());
}
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,13 @@ public void visitMethodInsn(
final String keyClassName = ((Type) stack[1]).getClassName();
final TypeDescription contextStoreImplementationClass =
getContextStoreImplementation(keyClassName, contextClassName);
log.debug(
"Rewriting context-store map fetch for instrumenter {}: {} -> {}",
instrumenter.getClass().getName(),
keyClassName,
contextClassName);
if (log.isDebugEnabled()) {
log.debug(
"Rewriting context-store map fetch for instrumenter {}: {} -> {}",
instrumenter.getClass().getName(),
keyClassName,
contextClassName);
}
if (contextStoreImplementationClass == null) {
throw new IllegalStateException(
String.format(
Expand Down
13 changes: 10 additions & 3 deletions dd-trace-core/src/main/java/datadog/trace/core/PendingTrace.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ public void registerSpan(final DDSpan span) {
span.ref = new WeakReference<DDSpan>(span, referenceQueue);
weakReferences.add(span.ref);
final int count = pendingReferenceCount.incrementAndGet();
log.debug("traceId: {} -- registered span {}. count = {}", traceId, span, count);
if (log.isDebugEnabled()) {
log.debug("traceId: {} -- registered span {}. count = {}", traceId, span, count);
}
} else {
log.debug("span {} already registered in trace {}", span, traceId);
}
Expand Down Expand Up @@ -181,8 +183,13 @@ public void registerContinuation(final ContinuableScope.Continuation continuatio
new WeakReference<ContinuableScope.Continuation>(continuation, referenceQueue);
weakReferences.add(continuation.ref);
final int count = pendingReferenceCount.incrementAndGet();
log.debug(
"traceId: {} -- registered continuation {}. count = {}", traceId, continuation, count);
if (log.isDebugEnabled()) {
log.debug(
"traceId: {} -- registered continuation {}. count = {}",
traceId,
continuation,
count);
}
} else {
log.debug("continuation {} already registered in trace {}", continuation, traceId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ public <C> void inject(
}
log.debug("{} - B3 parent context injected", context.getTraceId());
} catch (final NumberFormatException e) {
log.debug(
"Cannot parse context id(s): {} {}", context.getTraceId(), context.getSpanId(), e);
if (log.isDebugEnabled()) {
log.debug(
"Cannot parse context id(s): {} {}", context.getTraceId(), context.getSpanId(), e);
}
}
}

Expand Down

0 comments on commit 4b10796

Please sign in to comment.