Search in sources :

Example 1 with AssertableCallback

use of brave.test.util.AssertableCallback in project brave by openzipkin.

the class BaseITTracingClientInterceptor method callbackContextIsFromInvocationTime.

/**
 * This ensures that response callbacks run in the invocation context, not the client one. This
 * allows async chaining to appear caused by the parent, not by the most recent client. Otherwise,
 * we would see a client span child of a client span, which could be confused with duplicate
 * instrumentation and affect dependency link counts.
 */
@Test
public void callbackContextIsFromInvocationTime() {
    AssertableCallback<HelloReply> callback = new AssertableCallback<>();
    // Capture the current trace context when onSuccess or onError occur
    AtomicReference<TraceContext> invocationContext = new AtomicReference<>();
    callback.setListener(() -> invocationContext.set(currentTraceContext.get()));
    TraceContext parent = newTraceContext(SamplingFlags.SAMPLED);
    try (Scope scope = currentTraceContext.newScope(parent)) {
        GreeterGrpc.newStub(client).sayHello(HELLO_REQUEST, new StreamObserverAdapter(callback));
    }
    // ensures listener ran
    callback.join();
    assertThat(invocationContext.get()).isSameAs(parent);
    assertChildOf(testSpanHandler.takeRemoteSpan(CLIENT), parent);
}
Also used : AssertableCallback(brave.test.util.AssertableCallback) Scope(brave.propagation.CurrentTraceContext.Scope) TraceContext(brave.propagation.TraceContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) HelloReply(io.grpc.examples.helloworld.HelloReply) Test(org.junit.Test)

Example 2 with AssertableCallback

use of brave.test.util.AssertableCallback in project brave by openzipkin.

the class ITHttpAsyncClient method callbackContextIsFromInvocationTime.

/**
 * This ensures that response callbacks run in the invocation context, not the client one. This
 * allows async chaining to appear caused by the parent, not by the most recent client. Otherwise,
 * we would see a client span child of a client span, which could be confused with duplicate
 * instrumentation and affect dependency link counts.
 */
@Test
public void callbackContextIsFromInvocationTime() {
    server.enqueue(new MockResponse());
    AssertableCallback<Integer> callback = new AssertableCallback<>();
    // Capture the current trace context when onSuccess or onError occur
    AtomicReference<TraceContext> invocationContext = new AtomicReference<>();
    callback.setListener(() -> invocationContext.set(currentTraceContext.get()));
    TraceContext parent = newTraceContext(SamplingFlags.SAMPLED);
    try (Scope scope = currentTraceContext.newScope(parent)) {
        get(client, "/foo", callback);
    }
    // ensures listener ran
    callback.join();
    assertThat(invocationContext.get()).isSameAs(parent);
    assertChildOf(testSpanHandler.takeRemoteSpan(CLIENT), parent);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AssertableCallback(brave.test.util.AssertableCallback) Scope(brave.propagation.CurrentTraceContext.Scope) TraceContext(brave.propagation.TraceContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 3 with AssertableCallback

use of brave.test.util.AssertableCallback in project brave by openzipkin.

the class ITHttpAsyncClient method usesParentFromInvocationTime.

/**
 * This tests that the parent is determined at the time the request was made, not when the request
 * was executed.
 */
@Test
public void usesParentFromInvocationTime() {
    server.enqueue(new MockResponse().setBodyDelay(300, TimeUnit.MILLISECONDS));
    server.enqueue(new MockResponse());
    AssertableCallback<Integer> items1 = new AssertableCallback<>();
    AssertableCallback<Integer> items2 = new AssertableCallback<>();
    TraceContext parent = newTraceContext(SamplingFlags.SAMPLED);
    try (Scope scope = currentTraceContext.newScope(parent)) {
        get(client, "/items/1", items1);
        get(client, "/items/2", items2);
    }
    try (Scope scope = currentTraceContext.newScope(null)) {
        // complete within a different scope
        items1.join();
        items2.join();
        for (int i = 0; i < 2; i++) {
            TraceContext extracted = extract(takeRequest());
            assertChildOf(extracted, parent);
        }
    }
    // The spans may report in a different order than the requests
    for (int i = 0; i < 2; i++) {
        assertChildOf(testSpanHandler.takeRemoteSpan(CLIENT), parent);
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AssertableCallback(brave.test.util.AssertableCallback) Scope(brave.propagation.CurrentTraceContext.Scope) TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Example 4 with AssertableCallback

use of brave.test.util.AssertableCallback in project brave by openzipkin.

the class ITHttpAsyncClient method callbackContextIsFromInvocationTime_root.

/**
 * This ensures that response callbacks run when there is no invocation trace context.
 */
@Test
public void callbackContextIsFromInvocationTime_root() {
    server.enqueue(new MockResponse());
    AssertableCallback<Integer> callback = new AssertableCallback<>();
    // Capture the current trace context when onSuccess or onError occur
    AtomicReference<TraceContext> invocationContext = new AtomicReference<>();
    callback.setListener(() -> invocationContext.set(currentTraceContext.get()));
    get(client, "/foo", callback);
    // ensures listener ran
    callback.join();
    assertThat(invocationContext.get()).isNull();
    assertThat(testSpanHandler.takeRemoteSpan(CLIENT).parentId()).isNull();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AssertableCallback(brave.test.util.AssertableCallback) TraceContext(brave.propagation.TraceContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 5 with AssertableCallback

use of brave.test.util.AssertableCallback in project brave by openzipkin.

the class ITTracingFilter_Consumer method usesParentFromInvocationTime.

/**
 * This tests that the parent is determined at the time the request was made, not when the request
 * was executed.
 */
@Test
public void usesParentFromInvocationTime() {
    AssertableCallback<String> items1 = new AssertableCallback<>();
    AssertableCallback<String> items2 = new AssertableCallback<>();
    TraceContext parent = newTraceContext(SamplingFlags.SAMPLED);
    try (Scope scope = currentTraceContext.newScope(parent)) {
        RpcContext.getContext().asyncCall(() -> client.get().sayHello("jorge")).whenComplete(items1);
        RpcContext.getContext().asyncCall(() -> client.get().sayHello("romeo")).whenComplete(items2);
    }
    try (Scope scope = currentTraceContext.newScope(null)) {
        // complete within a different scope
        items1.join();
        items2.join();
        for (int i = 0; i < 2; i++) {
            TraceContext extracted = server.takeRequest().context();
            assertChildOf(extracted, parent);
        }
    }
    // The spans may report in a different order than the requests
    for (int i = 0; i < 2; i++) {
        assertChildOf(testSpanHandler.takeRemoteSpan(CLIENT), parent);
    }
}
Also used : AssertableCallback(brave.test.util.AssertableCallback) Scope(brave.propagation.CurrentTraceContext.Scope) TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Aggregations

TraceContext (brave.propagation.TraceContext)5 AssertableCallback (brave.test.util.AssertableCallback)5 Test (org.junit.Test)5 Scope (brave.propagation.CurrentTraceContext.Scope)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 HelloReply (io.grpc.examples.helloworld.HelloReply)1