-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(gax): implement active scope management and attribute validation for T3 attempt spans #13977
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -35,6 +35,7 @@ | |||||||||||||||||||||||||||||
| import io.opentelemetry.api.trace.Span; | ||||||||||||||||||||||||||||||
| import io.opentelemetry.api.trace.SpanBuilder; | ||||||||||||||||||||||||||||||
| import io.opentelemetry.api.trace.SpanKind; | ||||||||||||||||||||||||||||||
| import io.opentelemetry.api.trace.StatusCode; | ||||||||||||||||||||||||||||||
| import io.opentelemetry.api.trace.Tracer; | ||||||||||||||||||||||||||||||
| import java.util.HashMap; | ||||||||||||||||||||||||||||||
| import java.util.Map; | ||||||||||||||||||||||||||||||
|
|
@@ -53,6 +54,7 @@ | |||||||||||||||||||||||||||||
| private final String attemptSpanName; | ||||||||||||||||||||||||||||||
| private final ApiTracerContext apiTracerContext; | ||||||||||||||||||||||||||||||
| private @Nullable Span attemptSpan; | ||||||||||||||||||||||||||||||
| private io.opentelemetry.context.@Nullable Scope attemptScope; | ||||||||||||||||||||||||||||||
|
Check warning on line 57 in sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/OpenTelemetryTracingTracer.java
|
||||||||||||||||||||||||||||||
|
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 field |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||
| public void injectTraceContext(java.util.Map<String, String> carrier) { | ||||||||||||||||||||||||||||||
|
|
@@ -225,21 +227,22 @@ | |||||||||||||||||||||||||||||
| attemptSpan.setAllAttributes(ObservabilityUtils.toOtelAttributes(responseAttributes)); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (error != null && !Strings.isNullOrEmpty(error.getMessage())) { | ||||||||||||||||||||||||||||||
| attemptSpan.setAttribute( | ||||||||||||||||||||||||||||||
| ObservabilityAttributes.STATUS_MESSAGE_ATTRIBUTE, error.getMessage()); | ||||||||||||||||||||||||||||||
| if (error != null) { | ||||||||||||||||||||||||||||||
| attemptSpan.setStatus(StatusCode.ERROR); | ||||||||||||||||||||||||||||||
| if (!Strings.isNullOrEmpty(error.getMessage())) { | ||||||||||||||||||||||||||||||
| attemptSpan.setAttribute( | ||||||||||||||||||||||||||||||
| ObservabilityAttributes.STATUS_MESSAGE_ATTRIBUTE, error.getMessage()); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+230
to
236
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. Since
Suggested change
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| endAttempt(); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private void endAttempt() { | ||||||||||||||||||||||||||||||
| if (attemptSpan == null) { | ||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||
| if (attemptSpan != null) { | ||||||||||||||||||||||||||||||
| attemptSpan.end(); | ||||||||||||||||||||||||||||||
| attemptSpan = null; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| attemptSpan.end(); | ||||||||||||||||||||||||||||||
| attemptSpan = null; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
241
to
246
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. To prevent context leaks, the private void endAttempt() {
try {
if (attemptScope != null) {
attemptScope.close();
}
} finally {
attemptScope = null;
try {
if (attemptSpan != null) {
attemptSpan.end();
}
} finally {
attemptSpan = null;
}
}
}References
Comment on lines
241
to
246
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 private void endAttempt() {
try {
if (attemptScope != null) {
attemptScope.close();
}
} finally {
attemptScope = null;
try {
if (attemptSpan != null) {
attemptSpan.end();
}
} finally {
attemptSpan = null;
}
}
}References
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,16 @@ void testAttemptLifecycle_startsAndEndsAttemptSpan() { | |
| verify(span).end(); | ||
| } | ||
|
|
||
| @Test | ||
| void testAttemptFailed_setsErrorStatus() { | ||
| openTelemetryTracingTracer.attemptStarted(new Object(), 1); | ||
| openTelemetryTracingTracer.attemptFailedDuration( | ||
| new RuntimeException("Test error"), java.time.Duration.ofSeconds(1)); | ||
|
|
||
| verify(span).setStatus(io.opentelemetry.api.trace.StatusCode.ERROR); | ||
| verify(span).end(); | ||
| } | ||
|
Comment on lines
+91
to
+98
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. This test only verifies that the span status is set to |
||
|
|
||
| @Test | ||
| void testAttemptSucceeded_grpc() { | ||
| ApiTracerContext context = | ||
|
|
||
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.
The field
attemptScopeis declared here but is never initialized, used, or closed anywhere in the class. The PR description states that the OpenTelemetry contextScopeis activated duringattemptStarted(...), but that implementation is missing. Please implement the scope activation inattemptStarted(...)usingotelContext.makeCurrent()as described.