OpencensusTracer implements ApiTracer { @InternalApi("Visible for testing") static Status convertErrorToStatus(Throwable error) { StatusCode.Code gaxCode = Code.UNKNOWN; if (error instanceof ApiException) { gaxCode = ((ApiException) error).getStatusCode().getCode(); } else if (error.getCause() instanceof ApiException) { gaxCode = ((ApiException) error.getCause()).getStatusCode().getCode(); } Status.CanonicalCode code; try { code = Status.CanonicalCode.valueOf(gaxCode.name()); } catch (IllegalArgumentException e) { code = CanonicalCode.UNKNOWN; } return code.toStatus().withDescription(error.getMessage()); } OpencensusTracer(
      @Nonnull Tracer tracer, @Nonnull Span span, @Nonnull OperationType operationType); @Override Scope inScope(); @Override void operationSucceeded(); @Override void operationCancelled(); @Override void operationFailed(Throwable error); @Override void lroStartFailed(Throwable error); @Override void lroStartSucceeded(); @Override void connectionSelected(String id); @Override void attemptStarted(int attemptNumber); @Override void attemptSucceeded(); @Override void attemptCancelled(); @Override void attemptFailed(Throwable error, Duration delay); @Override void attemptFailedRetriesExhausted(Throwable error); @Override void attemptPermanentFailure(Throwable error); @Override void responseReceived(); @Override void requestSent(); @Override void batchRequestSent(long elementCount, long requestSize);  }
@Test public void testErrorConversion() { for (Code code : Code.values()) { ApiException error = new ApiException("fake message", null, new FakeStatusCode(code), false); Status opencensusStatus = OpencensusTracer.convertErrorToStatus(error); assertThat(opencensusStatus.getDescription()).isEqualTo("fake message"); assertThat(opencensusStatus.getCanonicalCode().toString()).isEqualTo(code.toString()); } }
@Test public void testStreamingErrorConversion() { ServerStreamingAttemptException error = new ServerStreamingAttemptException( new DeadlineExceededException( "timeout", null, new FakeStatusCode(Code.DEADLINE_EXCEEDED), true), true, true); Status opencensusStatus = OpencensusTracer.convertErrorToStatus(error); assertThat(opencensusStatus.getDescription()).isEqualTo("timeout"); assertThat(opencensusStatus.getCanonicalCode()).isEqualTo(CanonicalCode.DEADLINE_EXCEEDED); }