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

Bridge agent logs into application's slf4j logger #7339

Merged

Conversation

mateuszrzeszutek
Copy link
Member

@mateuszrzeszutek mateuszrzeszutek commented Nov 29, 2022

Related discussion #7257
Resolves #3413
Resolves #5059
Resolves #6258
Resolves #7179

Adds a logging implementation that'll collect agent logs in memory until slf4j is detected in the instrumented application; and when that happens will dump all the logs into the application slf4j and log directly to the application logger from that time on.

It's still in a POC state, unfortunately: while it works fine with an app that uses & initializes slf4j directly, Spring Boot applications actually reconfigure the logging implementation (e.g. logback) a while after slf4j is loaded; which causes all the startup agent logs (debug included) to be dumped with the default logback pattern.

Future work:

@mateuszrzeszutek mateuszrzeszutek requested a review from a team November 29, 2022 16:45
Copy link
Contributor

@breedx-splk breedx-splk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems cool to me. I'd love a small demo to see it in action, maybe in the sig somtime?


@Advice.OnMethodExit(suppress = Throwable.class)
public static void onExit() {
Slf4jApplicationLoggerBridge.install();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not immediately clear to me why this installation needs to happen in this instrumentation. Is there not another lifecycle hook that makes sense for this? If not (like maybe we only want to do this when slf4j is actually in the runtime mix?), maybe a comment could help to clarify why this should be wired via bytecode instead of agent lifecycle hookery.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there not another lifecycle hook that makes sense for this?

There is not - this needs to be initialized from the application code, because we need to grab a handle to the application's slf4j. This can pretty much only be done in an instrumentation.

@Override
public boolean defaultEnabled(ConfigProperties config) {
// only enable this instrumentation if the application logger is enabled
return config.getString("otel.javaagent.logging", "simple").equalsIgnoreCase("application");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this was around before this PR, but I don't see mention of it in any readme/notes/docs (and I also search the docs repo). Can we please get this added somewhere or an issue created to do that work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was planning to add some docs once this PR is accepted. Will create an issue when this is merged.

if (bridge != null) {
bridge.install(applicationLoggerFactory);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The relationship between set() and installApplicationLogger() is a little confusing. I wish there was just one method...but I'm sure there are timing considerations that make this challenging.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, there need to be 2 steps to initialize this thing. The names could be better, but I didn't manage to think up anything less confusing. Suggestions welcome 😄

@mateuszrzeszutek mateuszrzeszutek force-pushed the application-logging branch 2 times, most recently from e235600 to 0fcbedc Compare December 12, 2022 09:32
trask pushed a commit that referenced this pull request Jan 12, 2023
…#7446)

Also, log them on DEBUG when debug mode is off.
Related to
#7339
trask pushed a commit that referenced this pull request Jan 12, 2023
Fixes #7540

I think this kind of early config might be useful in general -- for
instance, I think I'd like to use it for properties introduced in
#7339
@mateuszrzeszutek
Copy link
Member Author

Rebased with main, @open-telemetry/java-instrumentation-maintainers PTAL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants