File tree Expand file tree Collapse file tree
sdk-platform-java/gax-java/gax/src
main/java/com/google/api/gax/tracing
test/java/com/google/api/gax/tracing Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3535import io .opentelemetry .api .trace .Span ;
3636import io .opentelemetry .api .trace .SpanBuilder ;
3737import io .opentelemetry .api .trace .SpanKind ;
38+ import io .opentelemetry .api .trace .StatusCode ;
3839import io .opentelemetry .api .trace .Tracer ;
3940import java .util .HashMap ;
4041import 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
Original file line number Diff line number Diff 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 =
You can’t perform that action at this time.
0 commit comments