@Override public void submit() { Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_BATCH_SUBMIT); Scope scope = tracer.withSpan(span); try { span.putAttribute("batch size", AttributeValue.longAttributeValue(batches.size())); for (BatchRequest batch : batches) { // TODO(hailongwen@): instrument 'google-api-java-client' to further break down the span. // Here we only add a annotation to at least know how much time each batch takes. span.addAnnotation("Execute batch request"); batch.setBatchUrl( new GenericUrl(String.format("%s/batch/storage/v1", options.getHost()))); batch.execute(); } } catch (IOException ex) { span.setStatus(Status.UNKNOWN.withDescription(ex.getMessage())); throw translate(ex); } finally { scope.close(); span.end(); } } }
@Override public void intercept(HttpRequest request) throws IOException { checkNotNull(request); if (this.interceptor != null) { this.interceptor.intercept(request); } if (propagationTextFormat != null && propagationTextFormatSetter != null) { SpanContext spanContext = tracer.getCurrentSpan().getContext(); if (!SpanContext.INVALID.equals(spanContext)) { propagationTextFormat.inject( spanContext, request.getHeaders(), propagationTextFormatSetter); } } } }
private void maybeRetry() { if (attemptsRemaining > 0) { span.addAnnotation("retrying"); runTransactionAttempt( transactionCallback, resultFuture, new TransactionOptions( attemptsRemaining, options.getExecutor(), transaction.getTransactionId()), span); } else { span.setStatus(TOO_MANY_RETRIES_STATUS); rejectTransaction( FirestoreException.serverRejected( Status.ABORTED, "Transaction was cancelled because of too many retries.")); } }
void spanEnd(Span span, @Nullable P response, @Nullable Throwable error) { int statusCode = extractor.getStatusCode(response); if (span.getOptions().contains(Options.RECORD_EVENTS)) { span.putAttribute( HttpTraceAttributeConstants.HTTP_STATUS_CODE, AttributeValue.longAttributeValue(statusCode)); } span.setStatus(HttpTraceUtil.parseResponseStatus(statusCode, error)); span.end(); }
/** {@inheritDoc} */ @Override public void operationSucceeded() { Map<String, AttributeValue> attributes = baseOperationAttributes(); span.putAttributes(attributes); span.end(); }
@Override public void onCompleted() { tracer.getCurrentSpan().addAnnotation("Firestore.BatchGet: Complete"); List<DocumentSnapshot> documentSnapshots = new ArrayList<>(); for (DocumentReference documentReference : documentReferences) { documentSnapshots.add(resultMap.get(documentReference)); } futureList.set(documentSnapshots); } };
@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); }
@Override public void close() { span.end(); synchronized (lock) { isClosed = true; } }
public static <T> Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> tracer( Tracer tracer, String service) { return innerHandler -> requestContext -> { final Request request = requestContext.request(); final URI uri = URI.create(request.uri()); final Span span = tracer.spanBuilder(service + '/' + uri.getPath()).startSpan(); span.putAttribute("method", stringAttributeValue(request.method())); span.putAttribute("uri", stringAttributeValue(request.uri())); final CompletionStage<Response<T>> response; try { response = innerHandler.invoke(requestContext); } catch (Exception e) { span.setStatus(UNKNOWN); span.end(); Throwables.throwIfUnchecked(e); throw new RuntimeException(e); } response.whenComplete((r, ex) -> { if (ex != null) { span.setStatus(UNKNOWN); } span.end(); }); return response; }; }
void commit() { span.addAnnotation("Starting Commit"); CommitRequest.Builder builder = CommitRequest.newBuilder().setSession(session.getName()).setTransactionId(transactionId); opSpan.end(); } catch (RuntimeException e) { span.addAnnotation("Commit Failed", TraceUtil.getExceptionAnnotations(e)); TraceUtil.endSpanWithFailure(opSpan, e); throw e; span.addAnnotation("Commit Done");
@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)))); }
@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)); }
@Override public final void handle(HttpExchange httpExchange) throws IOException { Span span = tracer .spanBuilderWithExplicitParent(httpServerSpanName, null) .setRecordEvents(true) .startSpan(); try (Scope ss = tracer.withSpan(span)) { span.putAttribute( "/http/method ", AttributeValue.stringAttributeValue(httpExchange.getRequestMethod())); httpExchange.sendResponseHeaders(200, 0); zpageHandler.emitHtml( uriQueryToMap(httpExchange.getRequestURI()), httpExchange.getResponseBody()); } finally { httpExchange.close(); span.end(END_SPAN_OPTIONS); } }
private static void putAttributeIfNotEmptyOrNull(Span span, String key, @Nullable String value) { if (value != null && !value.isEmpty()) { span.putAttribute(key, AttributeValue.stringAttributeValue(value)); } }
@Test public void getContextAndOptions() { Span span = new NoopSpan(spanContext, spanOptions); assertThat(span.getContext()).isEqualTo(spanContext); assertThat(span.getOptions()).isEqualTo(spanOptions); }
private static void linkSpans(Span span, List<Span> parentLinks) { if (!parentLinks.isEmpty()) { Link childLink = Link.fromSpanContext(span.getContext(), Type.CHILD_LINKED_SPAN); for (Span linkedSpan : parentLinks) { linkedSpan.addLink(childLink); span.addLink(Link.fromSpanContext(linkedSpan.getContext(), Type.PARENT_LINKED_SPAN)); } } }
/** * @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); }
/** * Adds a NetworkEvent to the {@code Span}. * * <p>This function is only intended to be used by RPC systems (either client or server), not by * higher level applications. * * @param networkEvent the network event to add. * @deprecated Use {@link #addMessageEvent}. * @since 0.5 */ @Deprecated public void addNetworkEvent(NetworkEvent networkEvent) { addMessageEvent(BaseMessageEventUtils.asMessageEvent(networkEvent)); }
@Test public void addMessageEventDefaultImplementation() { Span mockSpan = Mockito.mock(Span.class); MessageEvent messageEvent = MessageEvent.builder(MessageEvent.Type.SENT, 123) .setUncompressedMessageSize(456) .setCompressedMessageSize(789) .build(); NetworkEvent networkEvent = NetworkEvent.builder(NetworkEvent.Type.SENT, 123) .setUncompressedMessageSize(456) .setCompressedMessageSize(789) .build(); Mockito.doCallRealMethod().when(mockSpan).addMessageEvent(messageEvent); mockSpan.addMessageEvent(messageEvent); verify(mockSpan).addNetworkEvent(eq(networkEvent)); } }