Skip to content

Commit

Permalink
Fix tracer logging when instrumenting Gradle Daemon (#6080)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-tkachenko-datadog authored Oct 25, 2023
1 parent 8f42a2c commit 9824b2e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ private LoggerHelperFactory getHelperFactory() {
}
return factory;
}

@Override
public void reinitialize() {
helperFactory = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package datadog.trace.instrumentation.gradle;

import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;

import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.logging.GlobalLogLevelSwitcher;
import net.bytebuddy.asm.Advice;

@AutoService(Instrumenter.class)
public class GradleDaemonLoggingInstrumentation extends Instrumenter.CiVisibility
implements Instrumenter.ForSingleType {

public GradleDaemonLoggingInstrumentation() {
super("gradle", "gradle-daemon-logging");
}

@Override
public String instrumentedType() {
return "org.gradle.launcher.daemon.bootstrap.DaemonMain";
}

@Override
public void adviceTransformations(AdviceTransformation transformation) {
transformation.applyAdvice(
named("initialiseLogging"),
GradleDaemonLoggingInstrumentation.class.getName() + "$ReinitialiseLogging");
}

/**
* Gradle Daemon closes original {@link System.out} and {@link System.err} streams (see {@link
* org.gradle.launcher.daemon.bootstrap.DaemonMain#daemonStarted}), and replaces them with Gradle
* Daemon log streams (see {@link
* org.gradle.launcher.daemon.bootstrap.DaemonMain#initialiseLogging}).
*
* <p>{@link datadog.trace.logging.simplelogger.SLCompatFactory} that contains logging settings
* gets initialized before Gradle Daemon, so DD logging settings refer to the original streams
* that the daemon closes. They need to refer to the new streams created by the daemon, so here we
* reset the settings letting them re-initialize to capture the new streams.
*/
public static class ReinitialiseLogging {
@Advice.OnMethodExit(suppress = Throwable.class)
public static void reinitialiseTracerLogging() {
GlobalLogLevelSwitcher.get().reinitialize();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ public void restore() {
delegate.restore();
}
}

@Override
public void reinitialize() {
if (delegate != null) {
delegate.reinitialize();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ public interface LogLevelSwitcher {

/** Restore the LogLevel to the original setting. */
void restore();

/** Re-execute logging settings initialization */
void reinitialize();
}

0 comments on commit 9824b2e

Please sign in to comment.