Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensures current span is always closed prior to finishing (#393)
This changes logic in http handlers such that a span is always closed prior to being finished. This has two advantages: one is that we can test that instrumentation do not leak span contexts. Another is that log context synchronization will not add trace IDs of a closed span.
- Loading branch information
1 parent
7703057
commit 9060ecb
Showing
18 changed files
with
297 additions
and
290 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
instrumentation/http-tests/src/main/java/brave/http/ITHttp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package brave.http; | ||
|
||
import brave.Tracing; | ||
import brave.internal.StrictCurrentTraceContext; | ||
import brave.propagation.CurrentTraceContext; | ||
import brave.propagation.TraceContext; | ||
import brave.sampler.Sampler; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import org.junit.Rule; | ||
import org.junit.rules.ExpectedException; | ||
import zipkin.Endpoint; | ||
import zipkin.Span; | ||
import zipkin.internal.Util; | ||
import zipkin.storage.InMemoryStorage; | ||
|
||
import static java.util.Arrays.asList; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public abstract class ITHttp { | ||
@Rule public ExpectedException thrown = ExpectedException.none(); | ||
@Rule public MockWebServer server = new MockWebServer(); | ||
|
||
Endpoint local = Endpoint.builder().serviceName("local").ipv4(127 << 24 | 1).port(100).build(); | ||
InMemoryStorage storage = new InMemoryStorage(); | ||
|
||
protected CurrentTraceContext currentTraceContext = new StrictCurrentTraceContext(); | ||
protected HttpTracing httpTracing; | ||
|
||
Tracing.Builder tracingBuilder(Sampler sampler) { | ||
return Tracing.newBuilder() | ||
.reporter(s -> { | ||
// make sure the context was cleared prior to finish.. no leaks! | ||
TraceContext current = httpTracing.tracing().currentTraceContext().get(); | ||
if (current != null) { | ||
assertThat(current.spanId()) | ||
.isNotEqualTo(s.id); | ||
} | ||
storage.spanConsumer().accept(asList(s)); | ||
}) | ||
.currentTraceContext(currentTraceContext) | ||
.localEndpoint(local) | ||
.sampler(sampler); | ||
} | ||
|
||
List<Span> collectedSpans() { | ||
List<List<Span>> result = storage.spanStore().getRawTraces(); | ||
if (result.isEmpty()) return Collections.emptyList(); | ||
assertThat(result).hasSize(1); | ||
return result.get(0); | ||
} | ||
|
||
void assertReportedTagsInclude(String key, String... values) { | ||
assertThat(collectedSpans()) | ||
.flatExtracting(s -> s.binaryAnnotations) | ||
.filteredOn(b -> b.key.equals(key)) | ||
.extracting(b -> new String(b.value, Util.UTF_8)) | ||
.containsExactly(values); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.