private void populateError(Map<String, AttributeValue> attributes, Throwable error) { if (error == null) { attributes.put("status", null); return; } Status status = convertErrorToStatus(error); attributes.put( "status", AttributeValue.stringAttributeValue(status.getCanonicalCode().toString())); }
/** {@inheritDoc} */ @Override public void attemptFailedRetriesExhausted(Throwable error) { Map<String, AttributeValue> attributes = baseAttemptAttributes(); populateError(attributes, error); span.addAnnotation("Attempts exhausted", attributes); }
/** {@inheritDoc} */ @Override public void operationSucceeded() { Map<String, AttributeValue> attributes = baseOperationAttributes(); span.putAttributes(attributes); span.end(); }
@Test public void testRequestCount() { // Initial attempt sent 2 messages, then failed tracer.attemptStarted(0); tracer.requestSent(); tracer.requestSent(); tracer.attemptFailed(new RuntimeException(), Duration.ofMillis(1)); // Next attempt sent 1 message, then successfully finished the attempt and the logical operation. tracer.attemptStarted(1); tracer.requestSent(); tracer.attemptSucceeded(); tracer.operationSucceeded(); verify(span) .addAnnotation(eq("Attempt failed, scheduling next attempt"), attributeCaptor.capture()); assertThat(attributeCaptor.getValue()) .containsEntry("attempt request count", AttributeValue.longAttributeValue(2)); verify(span).addAnnotation(eq("Attempt succeeded"), attributeCaptor.capture()); assertThat(attributeCaptor.getValue()) .containsEntry("attempt request count", AttributeValue.longAttributeValue(1)); verify(span).putAttributes(attributeCaptor.capture()); assertThat(attributeCaptor.getValue()) .containsEntry("total request count", AttributeValue.longAttributeValue(3)); }
/** {@inheritDoc} */ @Override public void operationFailed(Throwable error) { Map<String, AttributeValue> attributes = baseOperationAttributes(); span.putAttributes(attributes); span.end(EndSpanOptions.builder().setStatus(convertErrorToStatus(error)).build()); }
@Before public void setUp() { tracer = new OpencensusTracer(internalTracer, span); }
@Test public void testStatusCode() { tracer.attemptStarted(0); tracer.attemptFailed( new DeadlineExceededException( "deadline exceeded", null, new FakeStatusCode(Code.DEADLINE_EXCEEDED), true), Duration.ofMillis(1)); tracer.attemptStarted(1); ApiException permanentError = new NotFoundException("not found", null, new FakeStatusCode(Code.NOT_FOUND), false); tracer.attemptPermanentFailure(permanentError); tracer.operationFailed(permanentError); verify(span) .addAnnotation(eq("Attempt failed, scheduling next attempt"), attributeCaptor.capture()); assertThat(attributeCaptor.getValue()) .containsEntry("status", AttributeValue.stringAttributeValue("DEADLINE_EXCEEDED")); verify(span) .addAnnotation(eq("Attempt failed, error not retryable"), attributeCaptor.capture()); assertThat(attributeCaptor.getValue()) .containsEntry("status", AttributeValue.stringAttributeValue("NOT_FOUND")); }
@Test public void testCancellationExample() { tracer.attemptStarted(0); tracer.connectionSelected(1); tracer.attemptCancelled(); tracer.operationCancelled(); verify(span) .addAnnotation( "Attempt started", ImmutableMap.of("attempt", AttributeValue.longAttributeValue(0))); verify(span) .addAnnotation( "Connection selected", ImmutableMap.of("id", AttributeValue.longAttributeValue(1))); verify(span) .addAnnotation( "Attempt cancelled", ImmutableMap.of("attempt", AttributeValue.longAttributeValue(0))); verify(span) .putAttributes(ImmutableMap.of("attempt count", AttributeValue.longAttributeValue(1))); verify(span) .end( EndSpanOptions.builder() .setStatus(Status.CANCELLED.withDescription("Cancelled by caller")) .build()); verifyNoMoreInteractions(span); }
@Test public void testRetriesExhaustedExample() { tracer.attemptStarted(0); tracer.connectionSelected(1); ApiException error0 = new DeadlineExceededException( "deadline exceeded", null, new FakeStatusCode(Code.DEADLINE_EXCEEDED), false); tracer.attemptFailedRetriesExhausted(error0); tracer.operationFailed(error0); verify(span) .addAnnotation( "Attempt started", ImmutableMap.of("attempt", AttributeValue.longAttributeValue(0))); verify(span) .addAnnotation( "Connection selected", ImmutableMap.of("id", AttributeValue.longAttributeValue(1))); verify(span) .addAnnotation( "Attempts exhausted", ImmutableMap.of( "attempt", AttributeValue.longAttributeValue(0), "status", AttributeValue.stringAttributeValue("DEADLINE_EXCEEDED"))); verify(span) .putAttributes(ImmutableMap.of("attempt count", AttributeValue.longAttributeValue(1))); verify(span) .end( EndSpanOptions.builder() .setStatus(Status.DEADLINE_EXCEEDED.withDescription("deadline exceeded")) .build()); verifyNoMoreInteractions(span); }
@Override public void attemptCancelled() { Map<String, AttributeValue> attributes = baseAttemptAttributes(); span.addAnnotation("Attempt cancelled", attributes); }
/** {@inheritDoc} */ @Override public void attemptStarted(int attemptNumber) { currentAttemptId = attemptNumber; attemptSentMessages.set(0); attemptReceivedMessages = 0; HashMap<String, AttributeValue> attributes = new HashMap<>(); populateAttemptNumber(attributes); span.addAnnotation("Attempt started", attributes); }
@Test public void testResponseCount() { // Initial attempt got 2 messages, then failed tracer.attemptStarted(0); tracer.responseReceived(); tracer.responseReceived(); tracer.attemptFailed(new RuntimeException(), Duration.ofMillis(1)); // Next attempt got 1 message, then successfully finished the attempt and the logical operation. tracer.attemptStarted(1); tracer.responseReceived(); tracer.attemptSucceeded(); tracer.operationSucceeded(); verify(span) .addAnnotation(eq("Attempt failed, scheduling next attempt"), attributeCaptor.capture()); assertThat(attributeCaptor.getValue()) .containsEntry("attempt response count", AttributeValue.longAttributeValue(2)); verify(span).addAnnotation(eq("Attempt succeeded"), attributeCaptor.capture()); assertThat(attributeCaptor.getValue()) .containsEntry("attempt response count", AttributeValue.longAttributeValue(1)); verify(span).putAttributes(attributeCaptor.capture()); assertThat(attributeCaptor.getValue()) .containsEntry("total response count", AttributeValue.longAttributeValue(3)); }
/** {@inheritDoc} */ @Override public void operationFailed(Throwable error) { Map<String, AttributeValue> attributes = baseOperationAttributes(); span.putAttributes(attributes); span.end(EndSpanOptions.builder().setStatus(convertErrorToStatus(error)).build()); }
@Override public ApiTracer newRootTracer(SpanName spanName) { if (clientNameOverride != null) { spanName = spanName.withClientName(clientNameOverride); } Span span = internalTracer .spanBuilderWithExplicitParent(spanName.toString(), BlankSpan.INSTANCE) .setRecordEvents(true) .startSpan(); return new OpencensusTracer(internalTracer, span); }
/** {@inheritDoc} */ @Override public void attemptSucceeded() { Map<String, AttributeValue> attributes = baseAttemptAttributes(); span.addAnnotation("Attempt succeeded", attributes); }
/** {@inheritDoc} */ @Override public void attemptStarted(int attemptNumber) { currentAttemptId = attemptNumber; attemptSentMessages.set(0); attemptReceivedMessages = 0; HashMap<String, AttributeValue> attributes = new HashMap<>(); populateAttemptNumber(attributes); span.addAnnotation("Attempt started", attributes); }
@Test public void testAttemptNumber() { tracer.attemptStarted(0); tracer.attemptFailed(new RuntimeException(), Duration.ofMillis(1)); tracer.attemptStarted(1); tracer.attemptSucceeded(); tracer.operationSucceeded(); verify(span) .addAnnotation(eq("Attempt failed, scheduling next attempt"), attributeCaptor.capture()); assertThat(attributeCaptor.getValue()) .containsEntry("attempt", AttributeValue.longAttributeValue(0)); verify(span).addAnnotation(eq("Attempt succeeded"), attributeCaptor.capture()); assertThat(attributeCaptor.getValue()) .containsEntry("attempt", AttributeValue.longAttributeValue(1)); verify(span).putAttributes(attributeCaptor.capture()); assertThat(attributeCaptor.getValue()) .containsEntry("attempt count", AttributeValue.longAttributeValue(2)); }
/** {@inheritDoc} */ @Override public void attemptPermanentFailure(Throwable error) { Map<String, AttributeValue> attributes = baseAttemptAttributes(); populateError(attributes, error); span.addAnnotation("Attempt failed, error not retryable", attributes); }
/** {@inheritDoc } */ @Override public ApiTracer newTracer(SpanName spanName) { if (clientNameOverride != null) { spanName = spanName.withClientName(clientNameOverride); } Span span = internalTracer.spanBuilder(spanName.toString()).setRecordEvents(true).startSpan(); return new OpencensusTracer(internalTracer, span); }
private void populateError(Map<String, AttributeValue> attributes, Throwable error) { if (error == null) { attributes.put("status", null); return; } Status status = convertErrorToStatus(error); attributes.put( "status", AttributeValue.stringAttributeValue(status.getCanonicalCode().toString())); }