Repository navigation
feat(gax): enter inScope() during CompositeTracer completion callbacks #15034
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
jinseopkim0
wants to merge
11
commits into
main
Choose a base branch
from
feat-composite-tracer-callback-scope
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.
+250
−19
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
050b117
feat(gax): enter inScope() during CompositeTracer completion callbacks
jinseopkim0 ae0fedf
fix(gax): suppress MustBeClosedChecker and SonarCloud java:S2589 in C…
jinseopkim0 04e2f8e
fix(gax): close child scopes on Error and make CompositeTracer.inScop…
jinseopkim0 70421d7
fix(gax): replace AtomicBoolean with local CompositeScope in Composit…
jinseopkim0 27557cd
fix(gax): add empty and single-child fast paths to CompositeTracer.in…
jinseopkim0 0ec3695
fix(gax): guard CompositeScope.closed with ReentrantLock
jinseopkim0 6002964
fix(gax): use thread-bound anonymous Scope in CompositeTracer.inScope()
jinseopkim0 7437a55
fix(gax): skip anonymous Scope wrapper when childScopes has 0 or 1 ac…
jinseopkim0 7338bf4
fix(gax): simplify CompositeTracer.inScope() to minimal null-guard im…
jinseopkim0 daacf14
chore(gax): add explanatory comments to CompositeTracer callback scoping
jinseopkim0 2d0dc1d
chore(gax): add Javadoc comments to all modified CompositeTracer methods
jinseopkim0 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,12 @@ | |
| * A composite implementation of {@link ApiTracer} that delegates all tracing events to a list of | ||
| * underlying tracers. | ||
| * | ||
| * <p>Completion and failure callbacks execute asynchronously on background worker threads where no | ||
| * trace span is active on thread-local storage. Wrapping those callbacks in {@code try (Scope | ||
| * ignored = inScope())} briefly activates the current span on the callback thread before any child | ||
| * tracer ends it, allowing sibling tracers (such as logging and metrics) to correlate error logs | ||
| * and metric exemplars with the active span before the scope closes on the same thread. | ||
| * | ||
| * <p>For internal use only. | ||
| */ | ||
| @NullMarked | ||
|
|
@@ -51,13 +57,22 @@ public CompositeTracer(List<ApiTracer> children) { | |
| this.children = ImmutableList.copyOf(children); | ||
| } | ||
|
|
||
| /** | ||
| * Opens a scope on each child tracer in forward order and returns a composite scope that closes | ||
| * them in reverse (LIFO) order. Null scopes (for example, from unstubbed {@link ApiTracer} mocks | ||
| * in tests) are ignored. | ||
| */ | ||
| @SuppressWarnings("java:S2589") | ||
| @Override | ||
| public Scope inScope() { | ||
| final List<Scope> childScopes = new ArrayList<>(children.size()); | ||
|
jinseopkim0 marked this conversation as resolved.
Resolved
|
||
|
|
||
| try { | ||
| for (ApiTracer child : children) { | ||
| childScopes.add(child.inScope()); | ||
| Scope childScope = child.inScope(); | ||
|
jinseopkim0 marked this conversation as resolved.
Resolved
|
||
| if (childScope != null) { | ||
| childScopes.add(childScope); | ||
| } | ||
| } | ||
| } catch (RuntimeException e) { | ||
| for (int i = childScopes.size() - 1; i >= 0; i--) { | ||
|
|
@@ -89,24 +104,42 @@ public Scope inScope() { | |
| }; | ||
| } | ||
|
|
||
| /** | ||
|
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. The javadocs are nearly identical for all the methods, and they are more for the behavior of |
||
| * Activates the operation span on the callback thread before any child ends it so sibling tracers | ||
| * (such as metrics exemplars) observe the active span during completion. | ||
| */ | ||
| @Override | ||
| public void operationSucceeded() { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).operationSucceeded(); | ||
| try (Scope ignored = inScope()) { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).operationSucceeded(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Activates the operation span on the callback thread before any child ends it so sibling tracers | ||
| * (such as metrics exemplars and cancellation logs) observe the active span. | ||
| */ | ||
| @Override | ||
| public void operationCancelled() { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).operationCancelled(); | ||
| try (Scope ignored = inScope()) { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).operationCancelled(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Activates the operation span on the callback thread before any child ends it so sibling tracers | ||
| * (such as metrics exemplars and ERROR-level logs) observe the active span. | ||
| */ | ||
| @Override | ||
| public void operationFailed(Throwable error) { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).operationFailed(error); | ||
| try (Scope ignored = inScope()) { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).operationFailed(error); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -132,45 +165,81 @@ public void attemptStarted(Object request, int attemptNumber) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Activates the attempt span on the callback thread before any child ends it so sibling tracers | ||
| * observe the active attempt span during completion. | ||
| */ | ||
| @Override | ||
| public void attemptSucceeded() { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).attemptSucceeded(); | ||
| try (Scope ignored = inScope()) { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).attemptSucceeded(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Activates the attempt span on the callback thread before any child ends it so sibling tracers | ||
| * observe the active attempt span on cancellation. | ||
| */ | ||
| @Override | ||
| public void attemptCancelled() { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).attemptCancelled(); | ||
| try (Scope ignored = inScope()) { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).attemptCancelled(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Activates the attempt span on the callback thread before any child ends it so sibling tracers | ||
| * (such as DEBUG-level retry logs) observe the active attempt span. | ||
| */ | ||
| @Override | ||
| public void attemptFailed(Throwable error, org.threeten.bp.Duration delay) { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).attemptFailed(error, delay); | ||
| try (Scope ignored = inScope()) { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).attemptFailed(error, delay); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Activates the attempt span on the callback thread before any child ends it so sibling tracers | ||
| * (such as DEBUG-level retry logs) observe the active attempt span. | ||
| */ | ||
| @Override | ||
| public void attemptFailedDuration(Throwable error, java.time.Duration delay) { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).attemptFailedDuration(error, delay); | ||
| try (Scope ignored = inScope()) { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).attemptFailedDuration(error, delay); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Activates the attempt span on the callback thread before any child ends it so sibling tracers | ||
| * (such as DEBUG-level exhausted-retry logs) observe the active attempt span. | ||
| */ | ||
| @Override | ||
| public void attemptFailedRetriesExhausted(Throwable error) { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).attemptFailedRetriesExhausted(error); | ||
| try (Scope ignored = inScope()) { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).attemptFailedRetriesExhausted(error); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Activates the attempt span on the callback thread before any child ends it so sibling tracers | ||
| * (such as DEBUG-level permanent-failure logs) observe the active attempt span. | ||
| */ | ||
| @Override | ||
| public void attemptPermanentFailure(Throwable error) { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).attemptPermanentFailure(error); | ||
| try (Scope ignored = inScope()) { | ||
| for (int i = children.size() - 1; i >= 0; i--) { | ||
| children.get(i).attemptPermanentFailure(error); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
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.
nit: Suppressing warning only because mocks in tests seems weak, it is more like "preventing against implementations that break the non-null contract"