/** * @deprecated Use {@link #putAttributes(Map)} * @param attributes the attributes that will be added and associated with the {@code Span}. */ @Deprecated public void addAttributes(Map<String, AttributeValue> attributes) { putAttributes(attributes); }
/** * Sets a set of attributes to the {@code Span}. The effect of this call is equivalent to that of * calling {@link #putAttribute(String, AttributeValue)} once for each element in the specified * map. * * @deprecated Use {@link #putAttributes(Map)} * @param attributes the attributes that will be added and associated with the {@code Span}. * @since 0.5 */ @Deprecated public void addAttributes(Map<String, AttributeValue> attributes) { putAttributes(attributes); }
/** * Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for * the key, the old value is replaced by the specified value. * * @param key the key for this attribute. * @param value the value for this attribute. */ public void putAttribute(String key, AttributeValue value) { // Not final because for performance reasons we want to override this in the implementation. // Also a default implementation is needed to not break the compatibility (users may extend this // for testing). putAttributes(Collections.singletonMap(key, value)); }
/** * Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for * the key, the old value is replaced by the specified value. * * @param key the key for this attribute. * @param value the value for this attribute. * @since 0.6 */ public void putAttribute(String key, AttributeValue value) { // Not final because for performance reasons we want to override this in the implementation. // Also a default implementation is needed to not break the compatibility (users may extend this // for testing). Utils.checkNotNull(key, "key"); Utils.checkNotNull(value, "value"); putAttributes(Collections.singletonMap(key, value)); }
/** {@inheritDoc} */ @Override public void operationSucceeded() { Map<String, AttributeValue> attributes = baseOperationAttributes(); span.putAttributes(attributes); span.end(); }
/** {@inheritDoc} */ @Override public void operationSucceeded() { Map<String, AttributeValue> attributes = baseOperationAttributes(); span.putAttributes(attributes); span.end(); }
/** {@inheritDoc} */ @Override public void operationCancelled() { Map<String, AttributeValue> attributes = baseOperationAttributes(); span.putAttributes(attributes); span.end( EndSpanOptions.builder() .setStatus(Status.CANCELLED.withDescription("Cancelled by caller")) .build()); }
/** {@inheritDoc} */ @Override public void operationFailed(Throwable error) { Map<String, AttributeValue> attributes = baseOperationAttributes(); span.putAttributes(attributes); span.end(EndSpanOptions.builder().setStatus(convertErrorToStatus(error)).build()); }
/** {@inheritDoc} */ @Override public void operationFailed(Throwable error) { Map<String, AttributeValue> attributes = baseOperationAttributes(); span.putAttributes(attributes); span.end(EndSpanOptions.builder().setStatus(convertErrorToStatus(error)).build()); }
@Test public void putAttributeCallsAddAttributesByDefault() { Span span = Mockito.spy(new NoopSpan(spanContext, spanOptions)); span.putAttribute("MyKey", AttributeValue.booleanAttributeValue(true)); span.end(); verify(span) .putAttributes( eq(Collections.singletonMap("MyKey", AttributeValue.booleanAttributeValue(true)))); }
/** {@inheritDoc} */ @Override public void operationCancelled() { Map<String, AttributeValue> attributes = baseOperationAttributes(); span.putAttributes(attributes); span.end( EndSpanOptions.builder() .setStatus(Status.CANCELLED.withDescription("Cancelled by caller")) .build()); }
@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)); }
@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)); }
@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)); }
@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); }
@Test public void testFailureExample() { tracer.attemptStarted(0); tracer.connectionSelected(1); ApiException error0 = new NotFoundException("not found", null, new FakeStatusCode(Code.NOT_FOUND), false); tracer.attemptPermanentFailure(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( "Attempt failed, error not retryable", ImmutableMap.of( "attempt", AttributeValue.longAttributeValue(0), "status", AttributeValue.stringAttributeValue("NOT_FOUND"))); verify(span) .putAttributes(ImmutableMap.of("attempt count", AttributeValue.longAttributeValue(1))); verify(span) .end( EndSpanOptions.builder() .setStatus(Status.NOT_FOUND.withDescription("not found")) .build()); verifyNoMoreInteractions(span); }
.putAttributes(ImmutableMap.of("attempt count", AttributeValue.longAttributeValue(2))); verify(span).end();