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

Support Tracing of Dependency Injection #11449

Open
wants to merge 13 commits into
base: 4.8.x
Choose a base branch
from

Conversation

graemerocher
Copy link
Contributor

@graemerocher graemerocher commented Dec 16, 2024

Adds the ability to trace the behaviour of dependency and configuration injection. Can be activated a number of different ways, the simplest being an environment variable:

MICRONAUT_INJECT_TRACE=.+ ./gradlew run

Which enables useful output such as what is the configuration profile of the application, where configuration is read from, what beans are created, how long the bean took to be created, the graph of the bean creation and where it was created from.

To support this, the PR also adds property origin tracking so that one can inspect where a property originated from.

In addition, some of the changes in DefaultBeanContext are in order to reduce stack trace sizes which were starting to get out of hand.

@graemerocher graemerocher added the type: enhancement New feature or request label Dec 16, 2024
@graemerocher graemerocher marked this pull request as draft December 16, 2024 19:43
@graemerocher graemerocher force-pushed the dependency-injection-debugger branch from 3735137 to e01e206 Compare January 2, 2025 19:01
Copy link

sonarqubecloud bot commented Jan 3, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
9 New Critical Issues (required ≤ 0)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@graemerocher graemerocher marked this pull request as ready for review January 3, 2025 17:14
type,
generics
);
Optional<String> q = annotationMetadata.getAnnotationNameByStereotype(AnnotationUtil.QUALIFIER)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe you can use similar code to Qualifiers.forArgument

Copy link
Contributor Author

Choose a reason for hiding this comment

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

code here is in core which can't reference inject

return Set.of();
}

void startTrace(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you move all the tracing code into some other class, not to have it in a public enum?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the trace methods are not currently public but package private. In the future yes it would be good to add a public API but for the moment it is package private / internal

try (BeanResolutionContext resolutionContext = newResolutionContext(beanDefinition, null)) {
T bean;
if (beanDefinition instanceof InstantiatableBeanDefinition<T> instantiatableBeanDefinition) {
bean = resolveByBeanFactory(resolutionContext, instantiatableBeanDefinition, qualifier, argumentValues);
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't create a new out-scope variable, just call return postBeanCreated right after. Next else is not needed. Same pattern repeats a few times

@dstepanov
Copy link
Contributor

Can you show an example of the output?

@dstepanov
Copy link
Contributor

Maybe one of the outputs can be a flame graph like the async profiler output Async profiler output

@graemerocher graemerocher force-pushed the dependency-injection-debugger branch from 079f97d to 25e2da1 Compare January 13, 2025 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Projects
Status: Ready for Review
Development

Successfully merging this pull request may close these issues.

2 participants