3131import io .opencensus .trace .samplers .Samplers ;
3232import io .opencensus .trace .unsafe .ContextUtils ;
3333import okhttp3 .Call ;
34+ import okhttp3 .HttpUrl ;
3435import okhttp3 .OkHttpClient ;
36+ import okhttp3 .Request ;
3537import org .junit .jupiter .api .BeforeAll ;
3638import org .junit .jupiter .api .BeforeEach ;
3739import org .junit .jupiter .api .Test ;
4345import static io .opencensus .trace .AttributeValue .stringAttributeValue ;
4446import static org .junit .jupiter .api .Assertions .assertEquals ;
4547import static org .junit .jupiter .api .Assertions .assertNotNull ;
48+ import static org .mockito .Mockito .mock ;
49+ import static org .mockito .Mockito .when ;
4650
4751public class OpenCensusTracerTest {
4852
@@ -56,7 +60,7 @@ public class OpenCensusTracerTest {
5660 * rootSpan set as the parent.
5761 */
5862 @ Test
59- public void testTrace_CompletionStage_Simple () throws Exception {
63+ public void traceCompletionStageSimple () throws Exception {
6064 io .opencensus .trace .Span rootSpan = startRootSpan ();
6165 final CompletableFuture <String > future = new CompletableFuture <>();
6266 OpenCensusTracer tracer = new OpenCensusTracer ();
@@ -82,7 +86,7 @@ public void testTrace_CompletionStage_Simple() throws Exception {
8286 }
8387
8488 @ Test
85- public void testTrace_CompletionStage_Fails () throws Exception {
89+ public void traceCompletionStageFails () throws Exception {
8690 io .opencensus .trace .Span rootSpan = startRootSpan ();
8791 final CompletableFuture <String > future = new CompletableFuture <>();
8892 OpenCensusTracer tracer = new OpenCensusTracer ();
@@ -108,13 +112,45 @@ public void testTrace_CompletionStage_Fails() throws Exception {
108112 }
109113
110114 @ Test
111- public void test_createTracedClient () {
115+ public void traceCompletionStageWithRequest () throws Exception {
116+ io .opencensus .trace .Span rootSpan = startRootSpan ();
117+ OpenCensusTracer tracer = new OpenCensusTracer ();
118+ final CompletableFuture <String > future = new CompletableFuture <>();
119+ Request mockRequest = mock (Request .class );
120+ when (mockRequest .url ()).thenReturn (HttpUrl .parse ("https://api.github.com/repos/spotify/github-java-client" ));
121+ when (mockRequest .method ()).thenReturn ("GET" );
122+
123+ try (com .spotify .github .tracing .Span span = tracer .span (mockRequest )){
124+ tracer .attachSpanToFuture (span , future );
125+ future .complete ("all done" );
126+ }
127+ rootSpan .end ();
128+
129+ List <SpanData > exportedSpans = spanExporterHandler .waitForSpansToBeExported (2 );
130+ assertEquals (2 , exportedSpans .size ());
131+
132+ SpanData root = findSpan (exportedSpans , rootSpanName );
133+ SpanData inner = findSpan (exportedSpans , "GitHub Request" );
134+
135+ assertEquals (root .getContext ().getTraceId (), inner .getContext ().getTraceId ());
136+ assertEquals (root .getContext ().getSpanId (), inner .getParentSpanId ());
137+ final Map <String , AttributeValue > attributes = inner .getAttributes ().getAttributeMap ();
138+ assertEquals (stringAttributeValue ("github-api-client" ), attributes .get ("component" ));
139+ assertEquals (stringAttributeValue ("github" ), attributes .get ("peer.service" ));
140+ assertEquals (stringAttributeValue ("https://api.github.com/repos/spotify/github-java-client" ), attributes .get ("http.url" ));
141+ assertEquals (stringAttributeValue ("GET" ), attributes .get ("method" ));
142+ assertEquals (Status .OK , inner .getStatus ());
143+ }
144+
145+ @ Test
146+ public void createTracedClient () {
112147 OpenCensusTracer tracer = new OpenCensusTracer ();
113148 OkHttpClient client = new OkHttpClient .Builder ().build ();
114149 Call .Factory callFactory = tracer .createTracedClient (client );
115150 assertNotNull (callFactory );
116151 }
117152
153+ @ SuppressWarnings ("deprecation" )
118154 private io .opencensus .trace .Span startRootSpan () {
119155 Span rootSpan = Tracing .getTracer ().spanBuilder (rootSpanName ).startSpan ();
120156 Context context = ContextUtils .withValue (Context .current (), rootSpan );
0 commit comments