Search in sources :

Example 76 with MutableSpan

use of brave.handler.MutableSpan in project brave by openzipkin.

the class IntegrationTestSpanHandler method doTakeSpan.

/**
 * This is hidden because the historical takeSpan() method led to numerous bugs. For example,
 * tests passed even though the span asserted against wasn't the intended one (local vs remote).
 * Also, tests passed even though there was an error inside the span. Even error tests have passed
 * for the wrong reason (ex setup failure not the error raised in instrumentation).
 */
MutableSpan doTakeSpan(@Nullable Throwable error, @Nullable String errorMessage, @Nullable String errorTag, boolean flushed) {
    MutableSpan result = doTakeSpan(flushed);
    if (ANY_STRING.equals(errorTag)) {
        // to save us from yet another parameter
        boolean hasError = result.error() != null || result.tag("error") != null;
        assertThat(hasError).withFailMessage("Expected %s to have an error, but there was no error", result).isTrue();
    } else if (error != null) {
        assertThat(result.error()).withFailMessage("Expected %s to have an error, but there was no error", result).isNotNull();
        assertThat(result.error()).isEqualTo(error);
        assertNoErrorTag(result);
    } else if (errorMessage != null) {
        assertThat(result.error()).withFailMessage("Expected %s to have an error message matching [%s], but there was no error", result, errorMessage).isNotNull();
        // Some exception messages are multi-line
        Pattern regex = Pattern.compile(errorMessage, Pattern.DOTALL);
        String actual = result.error().getMessage();
        assertThat(actual).withFailMessage("Expected %s to have an error message matching [%s], but was [%s]", result, errorMessage, actual).matches(regex);
        assertNoErrorTag(result);
    } else if (errorTag != null) {
        // Some exception messages are multi-line
        Pattern regex = Pattern.compile(errorTag, Pattern.DOTALL);
        assertThat(result.tags().get("error")).withFailMessage("Expected %s to have an error tag matching %s", result, errorTag).matches(regex);
    } else {
        assertNoError(result);
        assertNoErrorTag(result);
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) MutableSpan(brave.handler.MutableSpan)

Example 77 with MutableSpan

use of brave.handler.MutableSpan in project brave by openzipkin.

the class IntegrationTestSpanHandler method takeRemoteSpanWithError.

/**
 * Like {@link #takeRemoteSpan(Kind)} except a {@link MutableSpan#error()} must equal the given
 * value.
 *
 * <p><em>Note</em>: This enforces there is no "error" tag. If your framework clarifies the
 * "error" tag when there is also an unhandled exception, use {@link
 * #takeRemoteSpanWithErrorTag(Kind, String)} first, then check for error using normal
 * assertions.
 *
 * @see #takeRemoteSpanWithError(Kind)
 * @see #takeRemoteSpanWithErrorMessage(Kind, String)
 * @see #takeRemoteSpanWithErrorTag(Kind, String)
 */
public MutableSpan takeRemoteSpanWithError(Kind kind, Throwable error) {
    MutableSpan result = doTakeSpan(error, null, null, false);
    assertRemoteSpan(result, kind);
    return result;
}
Also used : MutableSpan(brave.handler.MutableSpan)

Example 78 with MutableSpan

use of brave.handler.MutableSpan in project brave by openzipkin.

the class OrphanTracker method end.

/**
 * In the case of {@link Cause#ORPHANED}, the calling thread will be an arbitrary invocation of
 * {@link Span} or {@link ScopedSpan} as spans orphaned from GC are expunged inline (not on the GC
 * thread). While this class is used for troubleshooting, it should do the least work possible to
 * prevent harm to arbitrary callers.
 */
@Override
public boolean end(TraceContext context, MutableSpan span, Cause cause) {
    Throwable caller = spanToCaller.remove(span);
    if (cause != Cause.ORPHANED)
        return true;
    boolean allocatedButNotUsed = span.equals(new MutableSpan(context, defaultSpan));
    if (caller != null)
        log(context, allocatedButNotUsed, caller);
    // skip adding an annotation
    if (allocatedButNotUsed)
        return true;
    span.annotate(clock.currentTimeMicroseconds(), "brave.flush");
    return true;
}
Also used : MutableSpan(brave.handler.MutableSpan)

Example 79 with MutableSpan

use of brave.handler.MutableSpan in project brave by openzipkin.

the class BaseITTracingClientInterceptor method clientParserTest.

@Test
public void clientParserTest() {
    closeClient(client);
    grpcTracing = grpcTracing.toBuilder().clientParser(new GrpcClientParser() {

        @Override
        protected <M> void onMessageSent(M message, SpanCustomizer span) {
            span.tag("grpc.message_sent", message.toString());
            if (tracing.currentTraceContext().get() != null) {
                span.tag("grpc.message_sent.visible", "true");
            }
        }

        @Override
        protected <M> void onMessageReceived(M message, SpanCustomizer span) {
            span.tag("grpc.message_received", message.toString());
            if (tracing.currentTraceContext().get() != null) {
                span.tag("grpc.message_received.visible", "true");
            }
        }

        @Override
        protected <ReqT, RespT> String spanName(MethodDescriptor<ReqT, RespT> methodDescriptor) {
            return methodDescriptor.getType().name();
        }
    }).build();
    client = newClient();
    ScopedSpan parent = tracing.tracer().startScopedSpan("parent");
    try {
        GreeterGrpc.newBlockingStub(client).sayHello(HELLO_REQUEST);
    } finally {
        parent.finish();
    }
    MutableSpan span = testSpanHandler.takeRemoteSpan(CLIENT);
    assertThat(span.name()).isEqualTo("UNARY");
    assertThat(span.tags()).containsKeys("grpc.message_received", "grpc.message_sent", "grpc.message_received.visible", "grpc.message_sent.visible");
    testSpanHandler.takeLocalSpan();
}
Also used : MutableSpan(brave.handler.MutableSpan) CurrentSpanCustomizer(brave.CurrentSpanCustomizer) SpanCustomizer(brave.SpanCustomizer) ScopedSpan(brave.ScopedSpan) Test(org.junit.Test)

Example 80 with MutableSpan

use of brave.handler.MutableSpan in project brave by openzipkin.

the class BaseITTracingClientInterceptor method onTransportException_setsError.

@Test
public void onTransportException_setsError() {
    server.stop();
    assertThatThrownBy(() -> GraterGrpc.newBlockingStub(client).seyHallo(HELLO_REQUEST)).isInstanceOf(StatusRuntimeException.class);
    // The error format of the exception message can differ from the span's "error" tag in CI
    MutableSpan span = testSpanHandler.takeRemoteSpanWithErrorMessage(CLIENT, ".*Connection refused.*");
    assertThat(span.tags()).containsEntry("grpc.status_code", "UNAVAILABLE");
}
Also used : MutableSpan(brave.handler.MutableSpan) Test(org.junit.Test)

Aggregations

MutableSpan (brave.handler.MutableSpan)141 Test (org.junit.Test)107 TraceContext (brave.propagation.TraceContext)36 KafkaStreams (org.apache.kafka.streams.KafkaStreams)28 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)28 Topology (org.apache.kafka.streams.Topology)28 Scope (brave.propagation.CurrentTraceContext.Scope)14 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)12 KeyValue (org.apache.kafka.streams.KeyValue)9 ArrayList (java.util.ArrayList)8 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)8 MessagingTracing (brave.messaging.MessagingTracing)7 FileNotFoundException (java.io.FileNotFoundException)7 Arrays (java.util.Arrays)7 List (java.util.List)7 CONSUMER (brave.Span.Kind.CONSUMER)6 PRODUCER (brave.Span.Kind.PRODUCER)6 SpanHandler (brave.handler.SpanHandler)6 KafkaTracing (brave.kafka.clients.KafkaTracing)6 KAFKA_STREAMS_FILTERED_TAG (brave.kafka.streams.KafkaStreamsTags.KAFKA_STREAMS_FILTERED_TAG)6