Skip to content

Commit

Permalink
Merge pull request #24 from llinder/drmaas-21
Browse files Browse the repository at this point in the history
 ensure the same span is available after parsing requests, closes #21
  • Loading branch information
llinder authored Mar 11, 2019
2 parents a82b31a + a56a9b2 commit 47387d8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 The OpenZipkin Authors
* Copyright 2016-2019 The OpenZipkin Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand All @@ -19,8 +19,6 @@
import brave.http.HttpServerHandler;
import brave.http.HttpTracing;
import brave.propagation.TraceContext;
import javax.inject.Inject;

import com.google.common.net.HostAndPort;
import ratpack.handling.Context;
import ratpack.handling.Handler;
Expand All @@ -35,6 +33,7 @@
import ratpack.zipkin.ServerResponse;
import ratpack.zipkin.ServerTracingHandler;

import javax.inject.Inject;
import java.util.Optional;

/**
Expand All @@ -58,15 +57,16 @@ public void handle(Context ctx) throws Exception {
ServerRequest request = new ServerRequestImpl(ctx.getRequest());
final Span span = handler.handleReceive(extractor, request);

//place the Span in scope so that downstream code (e.g. Ratpack handlers
//further on in the chain) can see the Span.
Tracer.SpanInScope scope = tracing.tracer().withSpanInScope(span);

ctx.getResponse().beforeSend(response -> {
scope.close();
ServerResponse serverResponse = new ServerResponseImpl(response, request, ctx.getPathBinding());
handler.handleSend(serverResponse, null, span);
});
//place the Span in scope so that downstream code (e.g. Ratpack handlers
//further on in the chain) can see the Span.
try (Tracer.SpanInScope scope = tracing.tracer().withSpanInScope(span)) {
ctx.next();
}
ctx.next();
}

private static class ServerRequestImpl implements ServerRequest {
Expand Down
66 changes: 56 additions & 10 deletions src/test/groovy/ratpack/zipkin/ServerTracingModuleSpec.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 The OpenZipkin Authors
* Copyright 2016-2019 The OpenZipkin Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand All @@ -14,28 +14,35 @@
package ratpack.zipkin

import brave.SpanCustomizer
import brave.Tracer
import brave.http.HttpSampler
import brave.propagation.B3Propagation
import brave.sampler.Sampler
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import io.netty.handler.codec.http.HttpResponseStatus
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import ratpack.form.Form
import ratpack.groovy.test.embed.GroovyEmbeddedApp
import ratpack.guice.Guice
import ratpack.handling.Context
import ratpack.handling.Handler
import ratpack.http.HttpMethod
import ratpack.jackson.Jackson
import ratpack.path.PathBinding
import ratpack.zipkin.internal.ZipkinHttpClientImpl
import ratpack.zipkin.support.B3PropagationHeaders
import ratpack.zipkin.support.TestReporter
import spock.lang.Specification
import spock.lang.Unroll
import zipkin2.Span
import zipkin2.reporter.Reporter

import static org.assertj.core.api.Assertions.*
import java.util.concurrent.CountDownLatch
import java.util.concurrent.atomic.AtomicReference

import brave.sampler.Sampler
import io.netty.handler.codec.http.HttpResponseStatus
import ratpack.groovy.test.embed.GroovyEmbeddedApp
import ratpack.guice.Guice
import ratpack.http.HttpMethod
import ratpack.zipkin.support.TestReporter
import spock.lang.Specification
import zipkin2.Span
import static org.assertj.core.api.Assertions.assertThat

class ServerTracingModuleSpec extends Specification {

Expand Down Expand Up @@ -573,4 +580,43 @@ class ServerTracingModuleSpec extends Specification {
then:
assertThat(reporter.getSpans()).isEmpty()
}
def 'Should provide the same trace context before and after parsing the request'() {
given:
def app = GroovyEmbeddedApp.of { server ->
server.registry(Guice.registry { binding ->
binding.module(ServerTracingModule.class, { config ->
config
.serviceName("embedded")
.sampler(Sampler.ALWAYS_SAMPLE)
.spanReporterV2(reporter)
})
}).handlers {
chain ->
chain.all { ctx ->
def tracer = ctx.get(Tracer)
tracer.currentSpan().tag("handler", "")
ctx.parse(Form).then { form ->
tracer.currentSpan().tag("before_render", form.get("param"))
ctx.render("ok")
tracer.currentSpan().tag("after_render", form.get("param"))
}
}
}
}
when:
app.test { t ->
t.request { spec ->
spec.post().body.text("param=the_param")
}
}
then:
reporter.getSpans().size() == 1
def span = reporter.getSpans().first()
assertThat(span.tags()).containsKey("handler")
assertThat(span.tags()).containsKey("before_render")
assertThat(span.tags()).containsKey("after_render")
assertThat(span.tags()).containsValue("the_param")
}
}

0 comments on commit 47387d8

Please sign in to comment.