77 * Licensed under the Apache License, Version 2.0 (the "License");
88 * you may not use this file except in compliance with the License.
99 * You may obtain a copy of the License at
10- *
10+ *
1111 * http://www.apache.org/licenses/LICENSE-2.0
12- *
12+ *
1313 * Unless required by applicable law or agreed to in writing, software
1414 * distributed under the License is distributed on an "AS IS" BASIS,
1515 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2020
2121package com .spotify .github .tracing ;
2222
23-
2423import com .spotify .github .tracing .opencensus .OpenCensusTracer ;
2524import io .grpc .Context ;
2625import io .opencensus .trace .Span ;
3130import io .opencensus .trace .samplers .Samplers ;
3231import io .opencensus .trace .unsafe .ContextUtils ;
3332import okhttp3 .Call ;
33+ import okhttp3 .HttpUrl ;
3434import okhttp3 .OkHttpClient ;
35+ import okhttp3 .Request ;
3536import org .junit .jupiter .api .BeforeAll ;
3637import org .junit .jupiter .api .BeforeEach ;
3738import org .junit .jupiter .api .Test ;
39+ import org .junit .jupiter .params .ParameterizedTest ;
40+ import org .junit .jupiter .params .provider .ValueSource ;
3841
3942import java .util .List ;
4043import java .util .Map ;
4346import static io .opencensus .trace .AttributeValue .stringAttributeValue ;
4447import static org .junit .jupiter .api .Assertions .assertEquals ;
4548import static org .junit .jupiter .api .Assertions .assertNotNull ;
49+ import static org .mockito .Mockito .mock ;
50+ import static org .mockito .Mockito .when ;
4651
4752public class OpenCensusTracerTest {
4853
49-
50- private final String rootSpanName = "root span" ;
51- private OcTestExportHandler spanExporterHandler ;
52-
53- /**
54- * Test that trace() a) returns a future that completes when the input future completes and b)
55- * sets up the Spans appropriately so that the Span for the operation is exported with the
56- * rootSpan set as the parent.
57- */
58- @ Test
59- public void testTrace_CompletionStage_Simple () throws Exception {
60- io .opencensus .trace .Span rootSpan = startRootSpan ();
61- final CompletableFuture <String > future = new CompletableFuture <>();
62- OpenCensusTracer tracer = new OpenCensusTracer ();
63-
64- tracer .span ("path" , "GET" , future );
65- future .complete ("all done" );
66- rootSpan .end ();
67-
68- List <SpanData > exportedSpans = spanExporterHandler .waitForSpansToBeExported (2 );
69- assertEquals (2 , exportedSpans .size ());
70-
71- SpanData root = findSpan (exportedSpans , rootSpanName );
72- SpanData inner = findSpan (exportedSpans , "GitHub Request" );
73-
74- assertEquals (root .getContext ().getTraceId (), inner .getContext ().getTraceId ());
75- assertEquals (root .getContext ().getSpanId (), inner .getParentSpanId ());
76- final Map <String , AttributeValue > attributes = inner .getAttributes ().getAttributeMap ();
77- assertEquals (stringAttributeValue ("github-api-client" ), attributes .get ("component" ));
78- assertEquals (stringAttributeValue ("github" ), attributes .get ("peer.service" ));
79- assertEquals (stringAttributeValue ("path" ), attributes .get ("http.url" ));
80- assertEquals (stringAttributeValue ("GET" ), attributes .get ("method" ));
81- assertEquals (Status .OK , inner .getStatus ());
82- }
83-
84- @ Test
85- public void testTrace_CompletionStage_Fails () throws Exception {
86- io .opencensus .trace .Span rootSpan = startRootSpan ();
87- final CompletableFuture <String > future = new CompletableFuture <>();
88- OpenCensusTracer tracer = new OpenCensusTracer ();
89-
90- tracer .span ("path" , "POST" , future );
91- future .completeExceptionally (new Exception ("GitHub failed!" ));
92- rootSpan .end ();
93-
94- List <SpanData > exportedSpans = spanExporterHandler .waitForSpansToBeExported (2 );
95- assertEquals (2 , exportedSpans .size ());
96-
97- SpanData root = findSpan (exportedSpans , rootSpanName );
98- SpanData inner = findSpan (exportedSpans , "GitHub Request" );
99-
100- assertEquals (root .getContext ().getTraceId (), inner .getContext ().getTraceId ());
101- assertEquals (root .getContext ().getSpanId (), inner .getParentSpanId ());
102- final Map <String , AttributeValue > attributes = inner .getAttributes ().getAttributeMap ();
103- assertEquals (stringAttributeValue ("github-api-client" ), attributes .get ("component" ));
104- assertEquals (stringAttributeValue ("github" ), attributes .get ("peer.service" ));
105- assertEquals (stringAttributeValue ("path" ), attributes .get ("http.url" ));
106- assertEquals (stringAttributeValue ("POST" ), attributes .get ("method" ));
107- assertEquals (Status .UNKNOWN , inner .getStatus ());
108- }
109-
110- @ Test
111- public void test_createTracedClient () {
112- OpenCensusTracer tracer = new OpenCensusTracer ();
113- OkHttpClient client = new OkHttpClient .Builder ().build ();
114- Call .Factory callFactory = tracer .createTracedClient (client );
115- assertNotNull (callFactory );
116- }
117-
118- private io .opencensus .trace .Span startRootSpan () {
119- Span rootSpan = Tracing .getTracer ().spanBuilder (rootSpanName ).startSpan ();
120- Context context = ContextUtils .withValue (Context .current (), rootSpan );
121- context .attach ();
122- return rootSpan ;
123- }
124-
125- private SpanData findSpan (final List <SpanData > spans , final String name ) {
126- return spans .stream ().filter (s -> s .getName ().equals (name )).findFirst ().get ();
127- }
128-
129- @ BeforeEach
130- public void setUpExporter () {
131- spanExporterHandler = new OcTestExportHandler ();
132- Tracing .getExportComponent ().getSpanExporter ().registerHandler ("test" , spanExporterHandler );
133- }
134-
135- @ BeforeAll
136- public static void setupTracing () {
137- final TraceConfig traceConfig = Tracing .getTraceConfig ();
138- final Sampler sampler = Samplers .alwaysSample ();
139- final TraceParams newParams =
140- traceConfig .getActiveTraceParams ().toBuilder ().setSampler (sampler ).build ();
141- traceConfig .updateActiveTraceParams (newParams );
54+ private final String rootSpanName = "root span" ;
55+ private OcTestExportHandler spanExporterHandler ;
56+
57+ /**
58+ * Test that trace() a) returns a future that completes when the input future completes and b)
59+ * sets up the Spans appropriately so that the Span for the operation is exported with the
60+ * rootSpan set as the parent.
61+ */
62+ @ ParameterizedTest
63+ @ ValueSource (strings = {"GET" , "POST" , "PUT" , "DELETE" })
64+ public void traceCompletionStageSimple (final String requestMethod ) throws Exception {
65+ io .opencensus .trace .Span rootSpan = startRootSpan ();
66+ final CompletableFuture <String > future = new CompletableFuture <>();
67+ OpenCensusTracer tracer = new OpenCensusTracer ();
68+
69+ tracer .span ("path" , requestMethod , future );
70+ future .complete ("all done" );
71+ rootSpan .end ();
72+
73+ List <SpanData > exportedSpans = spanExporterHandler .waitForSpansToBeExported (2 );
74+ assertEquals (2 , exportedSpans .size ());
75+
76+ SpanData root = findSpan (exportedSpans , rootSpanName );
77+ SpanData inner = findSpan (exportedSpans , "GitHub Request" );
78+
79+ assertEquals (root .getContext ().getTraceId (), inner .getContext ().getTraceId ());
80+ assertEquals (root .getContext ().getSpanId (), inner .getParentSpanId ());
81+ final Map <String , AttributeValue > attributes = inner .getAttributes ().getAttributeMap ();
82+ assertEquals (stringAttributeValue ("github-api-client" ), attributes .get ("component" ));
83+ assertEquals (stringAttributeValue ("github" ), attributes .get ("peer.service" ));
84+ assertEquals (stringAttributeValue ("path" ), attributes .get ("http.url" ));
85+ assertEquals (stringAttributeValue (requestMethod ), attributes .get ("method" ));
86+ assertEquals (Status .OK , inner .getStatus ());
87+ }
88+
89+ @ ParameterizedTest
90+ @ ValueSource (strings = {"GET" , "POST" , "PUT" , "DELETE" })
91+ public void traceCompletionStageFails (final String requestMethod ) throws Exception {
92+ io .opencensus .trace .Span rootSpan = startRootSpan ();
93+ final CompletableFuture <String > future = new CompletableFuture <>();
94+ OpenCensusTracer tracer = new OpenCensusTracer ();
95+
96+ tracer .span ("path" , requestMethod , future );
97+ future .completeExceptionally (new Exception ("GitHub failed!" ));
98+ rootSpan .end ();
99+
100+ List <SpanData > exportedSpans = spanExporterHandler .waitForSpansToBeExported (2 );
101+ assertEquals (2 , exportedSpans .size ());
102+
103+ SpanData root = findSpan (exportedSpans , rootSpanName );
104+ SpanData inner = findSpan (exportedSpans , "GitHub Request" );
105+
106+ assertEquals (root .getContext ().getTraceId (), inner .getContext ().getTraceId ());
107+ assertEquals (root .getContext ().getSpanId (), inner .getParentSpanId ());
108+ final Map <String , AttributeValue > attributes = inner .getAttributes ().getAttributeMap ();
109+ assertEquals (stringAttributeValue ("github-api-client" ), attributes .get ("component" ));
110+ assertEquals (stringAttributeValue ("github" ), attributes .get ("peer.service" ));
111+ assertEquals (stringAttributeValue ("path" ), attributes .get ("http.url" ));
112+ assertEquals (stringAttributeValue (requestMethod ), attributes .get ("method" ));
113+ assertEquals (Status .UNKNOWN , inner .getStatus ());
114+ }
115+
116+ @ ParameterizedTest
117+ @ ValueSource (strings = {"GET" , "POST" , "PUT" , "DELETE" })
118+ public void traceCompletionStageWithRequest (final String requestMethod ) throws Exception {
119+ io .opencensus .trace .Span rootSpan = startRootSpan ();
120+ OpenCensusTracer tracer = new OpenCensusTracer ();
121+ final CompletableFuture <String > future = new CompletableFuture <>();
122+ Request mockRequest = mock (Request .class );
123+ when (mockRequest .url ())
124+ .thenReturn (HttpUrl .parse ("https://api.github.com/repos/spotify/github-java-client" ));
125+ when (mockRequest .method ()).thenReturn (requestMethod );
126+
127+ try (com .spotify .github .tracing .Span span = tracer .span (mockRequest )) {
128+ tracer .attachSpanToFuture (span , future );
129+ future .complete ("all done" );
142130 }
143- }
131+ rootSpan .end ();
132+
133+ List <SpanData > exportedSpans = spanExporterHandler .waitForSpansToBeExported (2 );
134+ assertEquals (2 , exportedSpans .size ());
135+
136+ SpanData root = findSpan (exportedSpans , rootSpanName );
137+ SpanData inner = findSpan (exportedSpans , "GitHub Request" );
138+
139+ assertEquals (root .getContext ().getTraceId (), inner .getContext ().getTraceId ());
140+ assertEquals (root .getContext ().getSpanId (), inner .getParentSpanId ());
141+ final Map <String , AttributeValue > attributes = inner .getAttributes ().getAttributeMap ();
142+ assertEquals (stringAttributeValue ("github-api-client" ), attributes .get ("component" ));
143+ assertEquals (stringAttributeValue ("github" ), attributes .get ("peer.service" ));
144+ assertEquals (
145+ stringAttributeValue ("https://api.github.com/repos/spotify/github-java-client" ),
146+ attributes .get ("http.url" ));
147+ assertEquals (stringAttributeValue (requestMethod ), attributes .get ("method" ));
148+ assertEquals (Status .OK , inner .getStatus ());
149+ }
150+
151+ @ Test
152+ public void createTracedClient () {
153+ OpenCensusTracer tracer = new OpenCensusTracer ();
154+ OkHttpClient client = new OkHttpClient .Builder ().build ();
155+ Call .Factory callFactory = tracer .createTracedClient (client );
156+ assertNotNull (callFactory );
157+ }
158+
159+ @ SuppressWarnings ("deprecation" )
160+ private io .opencensus .trace .Span startRootSpan () {
161+ Span rootSpan = Tracing .getTracer ().spanBuilder (rootSpanName ).startSpan ();
162+ Context context = ContextUtils .withValue (Context .current (), rootSpan );
163+ context .attach ();
164+ return rootSpan ;
165+ }
166+
167+ private SpanData findSpan (final List <SpanData > spans , final String name ) {
168+ return spans .stream ().filter (s -> s .getName ().equals (name )).findFirst ().get ();
169+ }
170+
171+ @ BeforeEach
172+ public void setUpExporter () {
173+ spanExporterHandler = new OcTestExportHandler ();
174+ Tracing .getExportComponent ().getSpanExporter ().registerHandler ("test" , spanExporterHandler );
175+ }
176+
177+ @ BeforeAll
178+ public static void setupTracing () {
179+ final TraceConfig traceConfig = Tracing .getTraceConfig ();
180+ final Sampler sampler = Samplers .alwaysSample ();
181+ final TraceParams newParams =
182+ traceConfig .getActiveTraceParams ().toBuilder ().setSampler (sampler ).build ();
183+ traceConfig .updateActiveTraceParams (newParams );
184+ }
185+ }
0 commit comments