Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tracer logging when instrumenting Gradle Daemon #6080

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
Loading