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

ArC - add debug logging for the request context manipulation #27249

Merged
merged 1 commit into from
Aug 16, 2022
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
12 changes: 12 additions & 0 deletions docs/src/main/asciidoc/cdi-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,18 @@ The request context is destroyed:

NOTE: An event with qualifier `@Initialized(RequestScoped.class)` is fired when the request context is initialized for an observer notification. Moreover, the events with qualifiers `@BeforeDestroyed(RequestScoped.class)` and `@Destroyed(RequestScoped.class)` are fired when the request context is destroyed.

==== How to Enable Trace Logging for Request Context Activation

You can set the `TRACE` level for the logger `io.quarkus.arc.requestContext` and try to analyze the log output afterwards.

.`application.properties` Example
[source,properties]
----
quarkus.log.category."io.quarkus.arc.requestContext".min-level=TRACE <1>
quarkus.log.category."io.quarkus.arc.requestContext".level=TRACE
----
<1> You also need to adjust the minimum log level for the relevant category.

=== Qualified Injected Fields

In CDI, if you declare a field injection point you need to use `@Inject` and optionally a set of qualifiers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,23 @@
*/
class RequestContext implements ManagedContext {

private static final Logger LOGGER = Logger.getLogger(RequestContext.class.getPackage().getName());
private static final Logger LOG = Logger.getLogger("io.quarkus.arc.requestContext");

private final CurrentContext<RequestContextState> currentContext;

private final LazyValue<Notifier<Object>> initializedNotifier;
private final LazyValue<Notifier<Object>> beforeDestroyedNotifier;
private final LazyValue<Notifier<Object>> destroyedNotifier;

private final boolean traceEnabled;

public RequestContext(CurrentContext<RequestContextState> currentContext) {
this.currentContext = currentContext;
this.initializedNotifier = new LazyValue<>(RequestContext::createInitializedNotifier);
this.beforeDestroyedNotifier = new LazyValue<>(RequestContext::createBeforeDestroyedNotifier);
this.destroyedNotifier = new LazyValue<>(RequestContext::createDestroyedNotifier);
// we do not need to check the effective log level
this.traceEnabled = LOG.isTraceEnabled();
}

@Override
Expand Down Expand Up @@ -122,6 +126,15 @@ public void destroy(Contextual<?> contextual) {

@Override
public void activate(ContextState initialState) {
if (traceEnabled) {
String stack = Arrays.stream(Thread.currentThread().getStackTrace())
.skip(2)
.limit(7)
.map(se -> "\n\t" + se.toString())
.collect(Collectors.joining());
LOG.tracef("Activate %s %s\n\t...",
initialState != null ? Integer.toHexString(initialState.hashCode()) : "new", stack);
}
if (initialState == null) {
currentContext.set(new RequestContextState(new ConcurrentHashMap<>()));
// Fire an event with qualifier @Initialized(RequestScoped.class) if there are any observers for it
Expand Down Expand Up @@ -151,6 +164,14 @@ public ContextState getStateIfActive() {

@Override
public void deactivate() {
if (traceEnabled) {
String stack = Arrays.stream(Thread.currentThread().getStackTrace())
.skip(2)
.limit(7)
.map(se -> "\n\t" + se.toString())
.collect(Collectors.joining());
LOG.tracef("Deactivate%s\n\t...", stack);
}
currentContext.remove();
}

Expand All @@ -161,6 +182,14 @@ public void destroy() {

@Override
public void destroy(ContextState state) {
if (traceEnabled) {
String stack = Arrays.stream(Thread.currentThread().getStackTrace())
.skip(2)
.limit(7)
.map(se -> "\n\t" + se.toString())
.collect(Collectors.joining());
LOG.tracef("Destroy %s%s\n\t...", state != null ? Integer.toHexString(state.hashCode()) : "", stack);
}
if (state == null) {
// nothing to destroy
return;
Expand All @@ -174,15 +203,15 @@ public void destroy(ContextState state) {
try {
fireIfNotEmpty(beforeDestroyedNotifier);
} catch (Exception e) {
LOGGER.warn("An error occurred during delivery of the @BeforeDestroyed(RequestScoped.class) event", e);
LOG.warn("An error occurred during delivery of the @BeforeDestroyed(RequestScoped.class) event", e);
}
//Performance: avoid an iterator on the map elements
map.forEach(this::destroyContextElement);
// Fire an event with qualifier @Destroyed(RequestScoped.class) if there are any observers for it
try {
fireIfNotEmpty(destroyedNotifier);
} catch (Exception e) {
LOGGER.warn("An error occurred during delivery of the @Destroyed(RequestScoped.class) event", e);
LOG.warn("An error occurred during delivery of the @Destroyed(RequestScoped.class) event", e);
}
map.clear();
}
Expand Down