Skip to content

Commit 4a8d3f4

Browse files
committed
feat(gax): record error status on attempt span in OpenTelemetryTracingTracer
1 parent fa6641b commit 4a8d3f4

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/OpenTelemetryTracingTracer.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import io.opentelemetry.api.trace.Span;
3636
import io.opentelemetry.api.trace.SpanBuilder;
3737
import io.opentelemetry.api.trace.SpanKind;
38+
import io.opentelemetry.api.trace.StatusCode;
3839
import io.opentelemetry.api.trace.Tracer;
3940
import java.util.HashMap;
4041
import java.util.Map;
@@ -225,21 +226,22 @@ private void recordErrorAndEndAttempt(@Nullable Throwable error) {
225226
attemptSpan.setAllAttributes(ObservabilityUtils.toOtelAttributes(responseAttributes));
226227
}
227228

228-
if (error != null && !Strings.isNullOrEmpty(error.getMessage())) {
229-
attemptSpan.setAttribute(
230-
ObservabilityAttributes.STATUS_MESSAGE_ATTRIBUTE, error.getMessage());
229+
if (error != null) {
230+
attemptSpan.setStatus(StatusCode.ERROR);
231+
if (!Strings.isNullOrEmpty(error.getMessage())) {
232+
attemptSpan.setAttribute(
233+
ObservabilityAttributes.STATUS_MESSAGE_ATTRIBUTE, error.getMessage());
234+
}
231235
}
232236

233237
endAttempt();
234238
}
235239

236240
private void endAttempt() {
237-
if (attemptSpan == null) {
238-
return;
241+
if (attemptSpan != null) {
242+
attemptSpan.end();
243+
attemptSpan = null;
239244
}
240-
241-
attemptSpan.end();
242-
attemptSpan = null;
243245
}
244246

245247
@Override

sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/tracing/OpenTelemetryTracingTracerTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ void testAttemptLifecycle_startsAndEndsAttemptSpan() {
8787
verify(span).end();
8888
}
8989

90+
@Test
91+
void testAttemptFailed_setsErrorStatus() {
92+
openTelemetryTracingTracer.attemptStarted(new Object(), 1);
93+
openTelemetryTracingTracer.attemptFailedDuration(
94+
new RuntimeException("Test error"), java.time.Duration.ofSeconds(1));
95+
96+
verify(span).setStatus(io.opentelemetry.api.trace.StatusCode.ERROR);
97+
verify(span).end();
98+
}
99+
90100
@Test
91101
void testAttemptSucceeded_grpc() {
92102
ApiTracerContext context =

0 commit comments

Comments
 (0)