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;
}
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;
}
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;
}
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();
}
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");
}
Aggregations