-
Notifications
You must be signed in to change notification settings - Fork 969
Introduce RequestLogListener
#6543
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
Open
ikhoon
wants to merge
3
commits into
line:main
Choose a base branch
from
ikhoon:requestlog-listener
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
459 changes: 270 additions & 189 deletions
459
core/src/main/java/com/linecorp/armeria/common/logging/DefaultRequestLog.java
Large diffs are not rendered by default.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
core/src/main/java/com/linecorp/armeria/common/logging/IdempotentRequestLogListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * Copyright 2025 LY Corporation | ||
| * | ||
| * LY Corporation licenses this file to you under the Apache License, | ||
| * version 2.0 (the "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at: | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package com.linecorp.armeria.common.logging; | ||
|
|
||
| import static com.linecorp.armeria.common.logging.DefaultRequestLog.hasInterestedFlags; | ||
|
|
||
| import com.linecorp.armeria.internal.common.util.ReentrantShortLock; | ||
|
|
||
| /** | ||
| * A {@link RequestLogListener} that delivers each event only once to the delegate listener. | ||
| */ | ||
| final class IdempotentRequestLogListener extends ReentrantShortLock implements RequestLogListener { | ||
|
|
||
| private static final long serialVersionUID = -573237359665852226L; | ||
|
|
||
| private final RequestLogListener delegate; | ||
| private int notifiedFlags; | ||
|
|
||
| IdempotentRequestLogListener(RequestLogListener delegate) { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public void onEvent(RequestLogProperty property, RequestLog log) { | ||
| lock(); | ||
| try { | ||
| if (hasInterestedFlags(notifiedFlags, property)) { | ||
| // Already notified. | ||
| return; | ||
| } | ||
| notifiedFlags |= property.flag(); | ||
| } finally { | ||
| unlock(); | ||
| } | ||
| delegate.onEvent(property, log); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
core/src/main/java/com/linecorp/armeria/common/logging/RequestLogListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * Copyright 2025 LY Corporation | ||
| * | ||
| * LY Corporation licenses this file to you under the Apache License, | ||
| * version 2.0 (the "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at: | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package com.linecorp.armeria.common.logging; | ||
|
|
||
| import com.linecorp.armeria.common.annotation.UnstableApi; | ||
|
|
||
| /** | ||
| * A listener that listens to all events of a {@link RequestLog}. | ||
| * | ||
| * <p>If a {@link RequestLogProperty} was completed before adding this listener to the {@link RequestLog}, | ||
| * the {@link #onEvent(RequestLogProperty, RequestLog)} method will be invoked immediately with the already | ||
| * completed property upon adding the listener. | ||
| * | ||
| * <p>Note that this listener may be invoked in the I/O worker thread so make sure to offload any blocking | ||
| * operations to a separate thread pool. | ||
| */ | ||
| @UnstableApi | ||
| @FunctionalInterface | ||
| public interface RequestLogListener { | ||
|
|
||
| /** | ||
| * Invoked when the specified {@link RequestLogProperty} is completed. | ||
| */ | ||
| void onEvent(RequestLogProperty property, RequestLog log); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,16 +37,6 @@ public enum RequestLogProperty { | |
| */ | ||
| REQUEST_START_TIME(true), | ||
|
|
||
| /** | ||
| * {@link RequestLog#requestEndTimeNanos()}, {@link RequestLog#requestDurationNanos()}. | ||
| */ | ||
| REQUEST_END_TIME(true), | ||
|
|
||
| /** | ||
| * {@link RequestLog#requestFirstBytesTransferredTimeNanos()}. | ||
| */ | ||
| REQUEST_FIRST_BYTES_TRANSFERRED_TIME(true), | ||
|
|
||
| /** | ||
| * {@link RequestLog#channel()}, {@link RequestLog#sessionProtocol()}, {@link RequestLog#sslSession()}, | ||
| * {@link RequestLog#connectionTimings()}. | ||
|
|
@@ -73,6 +63,11 @@ public enum RequestLogProperty { | |
| */ | ||
| REQUEST_HEADERS(true), | ||
|
|
||
| /** | ||
| * {@link RequestLog#requestFirstBytesTransferredTimeNanos()}. | ||
| */ | ||
| REQUEST_FIRST_BYTES_TRANSFERRED_TIME(true), | ||
|
|
||
| /** | ||
| * {@link RequestLog#requestContent()}, {@link RequestLog#rawRequestContent()}. | ||
| */ | ||
|
|
@@ -98,30 +93,40 @@ public enum RequestLogProperty { | |
| */ | ||
| REQUEST_CAUSE(true), | ||
|
|
||
| /** | ||
| * {@link RequestLog#requestEndTimeNanos()}, {@link RequestLog#requestDurationNanos()}. | ||
| */ | ||
| REQUEST_END_TIME(true), | ||
|
|
||
| /** | ||
| * Indicates that the request is complete and all request properties are available. | ||
| */ | ||
| REQUEST_COMPLETE(true), | ||
|
|
||
| // Response properties | ||
|
|
||
| /** | ||
| * {@link RequestLog#responseCause()}. | ||
| */ | ||
| // Notify the response cause before other response properties to propagate the cause as early as possible. | ||
| RESPONSE_CAUSE(false), | ||
|
|
||
| /** | ||
| * {@link RequestLog#responseStartTimeMicros()}, {@link RequestLog#responseStartTimeMillis()}, | ||
| * {@link RequestLog#responseStartTimeNanos()}. | ||
| */ | ||
| RESPONSE_START_TIME(false), | ||
|
|
||
| /** | ||
| * {@link RequestLog#responseEndTimeNanos()}, {@link RequestLog#responseDurationNanos()}, | ||
| * {@link RequestLog#totalDurationNanos()}. | ||
| * {@link RequestLog#responseHeaders()}. | ||
| */ | ||
| RESPONSE_END_TIME(false), | ||
| RESPONSE_HEADERS(false), | ||
|
|
||
| /** | ||
| * {@link RequestLog#responseFirstBytesTransferredTimeNanos()}. | ||
| */ | ||
| RESPONSE_FIRST_BYTES_TRANSFERRED_TIME(false), | ||
|
|
||
| /** | ||
| * {@link RequestLog#responseHeaders()}. | ||
| */ | ||
| RESPONSE_HEADERS(false), | ||
|
|
||
| /** | ||
| * {@link RequestLog#responseContent()}. | ||
| */ | ||
|
|
@@ -132,20 +137,32 @@ public enum RequestLogProperty { | |
| */ | ||
| RESPONSE_CONTENT_PREVIEW(false), | ||
|
|
||
| /** | ||
| * {@link RequestLog#responseLength()}. | ||
| */ | ||
| // TODO(ikhoon): Check if this property is actually used anywhere. | ||
| RESPONSE_LENGTH(false), | ||
|
|
||
| /** | ||
| * {@link RequestLog#responseTrailers()}. | ||
| */ | ||
| RESPONSE_TRAILERS(false), | ||
|
|
||
| /** | ||
| * {@link RequestLog#responseLength()}. | ||
| * {@link RequestLog#responseEndTimeNanos()}, {@link RequestLog#responseDurationNanos()}, | ||
| * {@link RequestLog#totalDurationNanos()}. | ||
| */ | ||
| RESPONSE_LENGTH(false), | ||
| RESPONSE_END_TIME(false), | ||
|
|
||
| /** | ||
| * {@link RequestLog#responseCause()}. | ||
| * Indicates that the response is complete and all response properties are available. | ||
| */ | ||
| RESPONSE_COMPLETE(false), | ||
|
|
||
| /** | ||
| * Indicates that both the request and response are complete and all properties are available. | ||
| */ | ||
| RESPONSE_CAUSE(false); | ||
| ALL_COMPLETE(false); | ||
|
|
||
| private static final Set<RequestLogProperty> REQUEST_PROPERTIES = | ||
| Arrays.stream(values()) | ||
|
|
@@ -154,7 +171,7 @@ public enum RequestLogProperty { | |
|
|
||
| private static final Set<RequestLogProperty> RESPONSE_PROPERTIES = | ||
| Arrays.stream(values()) | ||
| .filter(p -> !p.isRequestProperty) | ||
| .filter(p -> !p.isRequestProperty && p != ALL_COMPLETE) | ||
| .collect(Sets.toImmutableEnumSet()); | ||
|
|
||
| private static final Set<RequestLogProperty> ALL_PROPERTIES = | ||
|
|
@@ -165,8 +182,8 @@ public enum RequestLogProperty { | |
| static final int FLAGS_ALL_COMPLETE; | ||
|
|
||
| static { | ||
| FLAGS_REQUEST_COMPLETE = flags(REQUEST_PROPERTIES); | ||
| FLAGS_RESPONSE_COMPLETE = flags(RESPONSE_PROPERTIES); | ||
| FLAGS_REQUEST_COMPLETE = flags(REQUEST_PROPERTIES) & ~REQUEST_COMPLETE.flag(); | ||
| FLAGS_RESPONSE_COMPLETE = flags(RESPONSE_PROPERTIES) & ~RESPONSE_COMPLETE.flag(); | ||
|
Comment on lines
+185
to
+186
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Understood |
||
| FLAGS_ALL_COMPLETE = FLAGS_REQUEST_COMPLETE | FLAGS_RESPONSE_COMPLETE; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note) Understood the reason for the reorder is to ensure properties are notified in the correct order