77
88import static org .assertj .core .api .Assertions .assertThat ;
99
10+ import com .azure .core .annotation .ExpectedResponses ;
11+ import com .azure .core .annotation .Get ;
12+ import com .azure .core .annotation .Host ;
13+ import com .azure .core .annotation .ServiceInterface ;
14+ import com .azure .core .http .HttpClient ;
15+ import com .azure .core .http .HttpPipeline ;
16+ import com .azure .core .http .HttpPipelineBuilder ;
17+ import com .azure .core .http .policy .HttpPipelinePolicy ;
18+ import com .azure .core .http .policy .HttpPolicyProviders ;
19+ import com .azure .core .http .rest .Response ;
20+ import com .azure .core .http .rest .RestProxy ;
21+ import com .azure .core .test .http .MockHttpResponse ;
22+ import com .azure .core .util .ClientOptions ;
1023import com .azure .core .util .Context ;
24+ import com .azure .core .util .TracingOptions ;
25+ import io .opentelemetry .api .common .AttributeKey ;
1126import io .opentelemetry .api .common .Attributes ;
1227import io .opentelemetry .api .trace .SpanKind ;
28+ import io .opentelemetry .instrumentation .api .internal .SpanKey ;
1329import io .opentelemetry .instrumentation .testing .junit .AgentInstrumentationExtension ;
1430import io .opentelemetry .instrumentation .testing .junit .InstrumentationExtension ;
1531import io .opentelemetry .sdk .trace .data .StatusData ;
32+ import java .util .ArrayList ;
33+ import java .util .List ;
34+ import java .util .concurrent .atomic .AtomicBoolean ;
1635import org .junit .jupiter .api .Test ;
1736import org .junit .jupiter .api .extension .RegisterExtension ;
37+ import reactor .core .publisher .Mono ;
38+ import reactor .test .StepVerifier ;
1839
1940class AzureSdkTest {
2041
@@ -27,7 +48,9 @@ void testHelperClassesInjected() {
2748 assertThat (azTracer .isEnabled ()).isTrue ();
2849
2950 assertThat (azTracer .getClass ().getName ())
30- .endsWith ("com.azure.core.tracing.opentelemetry.OpenTelemetryTracer" );
51+ .isEqualTo (
52+ "io.opentelemetry.javaagent.instrumentation.azurecore.v1_36.shaded"
53+ + ".com.azure.core.tracing.opentelemetry.OpenTelemetryTracer" );
3154 }
3255
3356 @ Test
@@ -46,9 +69,94 @@ void testSpan() {
4669 .hasAttributesSatisfying (Attributes ::isEmpty )));
4770 }
4871
72+ @ Test
73+ void testPipelineAndSuppression () {
74+ AtomicBoolean hasClientAndHttpKeys = new AtomicBoolean (false );
75+
76+ HttpClient mockClient =
77+ request ->
78+ Mono .defer (
79+ () -> {
80+ // check if suppression is working
81+ hasClientAndHttpKeys .set (hasClientAndHttpSpans ());
82+ return Mono .just (new MockHttpResponse (request , 200 ));
83+ });
84+
85+ StepVerifier .create (createService (mockClient , true ).testMethod ())
86+ .expectNextCount (1 )
87+ .expectComplete ()
88+ .verify ();
89+
90+ assertThat (hasClientAndHttpKeys .get ()).isTrue ();
91+ testing .waitAndAssertTracesWithoutScopeVersionVerification (
92+ trace ->
93+ trace .hasSpansSatisfyingExactly (
94+ span ->
95+ span .hasName ("myService.testMethod" )
96+ .hasKind (SpanKind .INTERNAL )
97+ .hasStatus (StatusData .unset ())
98+ .hasAttributesSatisfyingExactly (),
99+ span ->
100+ span .hasKind (SpanKind .CLIENT )
101+ .hasName (Boolean .getBoolean ("testLatestDeps" ) ? "GET" : "HTTP GET" )
102+ .hasStatus (StatusData .unset ())
103+ .hasAttribute (AttributeKey .longKey ("http.response.status_code" ), 200L )));
104+ }
105+
106+ @ Test
107+ void testDisabledTracingNoSuppression () {
108+ AtomicBoolean hasClientAndHttpKeys = new AtomicBoolean (false );
109+
110+ HttpClient mockClient =
111+ request ->
112+ Mono .defer (
113+ () -> {
114+ // check no suppression
115+ hasClientAndHttpKeys .set (hasClientAndHttpSpans ());
116+ return Mono .just (new MockHttpResponse (request , 200 ));
117+ });
118+
119+ StepVerifier .create (createService (mockClient , false ).testMethod ())
120+ .expectNextCount (1 )
121+ .expectComplete ()
122+ .verify ();
123+
124+ assertThat (hasClientAndHttpKeys .get ()).isFalse ();
125+ }
126+
49127 private static com .azure .core .util .tracing .Tracer createAzTracer () {
50128 com .azure .core .util .tracing .TracerProvider azProvider =
51129 com .azure .core .util .tracing .TracerProvider .getDefaultProvider ();
52130 return azProvider .createTracer ("test-lib" , "test-version" , "otel.tests" , null );
53131 }
132+
133+ private static TestInterface createService (HttpClient httpClient , boolean tracingEnabled ) {
134+ List <HttpPipelinePolicy > policies = new ArrayList <>();
135+ HttpPolicyProviders .addAfterRetryPolicies (policies );
136+
137+ ClientOptions clientOptions =
138+ new ClientOptions ().setTracingOptions (new TracingOptions ().setEnabled (tracingEnabled ));
139+ HttpPipeline pipeline =
140+ new HttpPipelineBuilder ()
141+ .policies (policies .toArray (new HttpPipelinePolicy [0 ]))
142+ .httpClient (httpClient )
143+ .clientOptions (clientOptions )
144+ .build ();
145+
146+ return RestProxy .create (TestInterface .class , pipeline );
147+ }
148+
149+ private static boolean hasClientAndHttpSpans () {
150+ io .opentelemetry .context .Context ctx = io .opentelemetry .context .Context .current ();
151+ return SpanKey .KIND_CLIENT .fromContextOrNull (ctx ) != null
152+ && SpanKey .HTTP_CLIENT .fromContextOrNull (ctx ) != null ;
153+ }
154+
155+ @ Host ("https://azure.com" )
156+ @ ServiceInterface (name = "myService" )
157+ interface TestInterface {
158+ @ Get ("path" )
159+ @ ExpectedResponses ({200 })
160+ Mono <Response <Void >> testMethod ();
161+ }
54162}
0 commit comments