55
66package io .opentelemetry .javaagent .instrumentation .kubernetesclient ;
77
8+ import static io .opentelemetry .api .common .AttributeKey .stringKey ;
89import static io .opentelemetry .instrumentation .testing .util .TelemetryDataUtil .orderByRootSpanName ;
910import static io .opentelemetry .sdk .testing .assertj .OpenTelemetryAssertions .equalTo ;
1011import static io .opentelemetry .semconv .ErrorAttributes .ERROR_TYPE ;
2021import io .kubernetes .client .openapi .ApiClient ;
2122import io .kubernetes .client .openapi .ApiException ;
2223import io .kubernetes .client .openapi .apis .CoreV1Api ;
23- import io .opentelemetry .api .common .AttributeKey ;
2424import io .opentelemetry .api .trace .SpanKind ;
2525import io .opentelemetry .instrumentation .testing .junit .AgentInstrumentationExtension ;
2626import io .opentelemetry .instrumentation .testing .junit .InstrumentationExtension ;
3333import java .util .Map ;
3434import java .util .concurrent .CountDownLatch ;
3535import java .util .concurrent .atomic .AtomicReference ;
36+ import javax .annotation .Nullable ;
3637import org .junit .jupiter .api .AfterEach ;
3738import org .junit .jupiter .api .BeforeEach ;
3839import org .junit .jupiter .api .Test ;
@@ -49,6 +50,14 @@ class KubernetesClientTest {
4950
5051 private CoreV1Api coreV1Api ;
5152
53+ @ Nullable
54+ private static String experimental (String value ) {
55+ if (Boolean .getBoolean ("otel.instrumentation.kubernetes.experimental-span-attributes" )) {
56+ return value ;
57+ }
58+ return null ;
59+ }
60+
5261 @ BeforeEach
5362 void beforeEach () {
5463 mockWebServer .start ();
@@ -92,8 +101,9 @@ void synchronousCall() throws ApiException {
92101 equalTo (SERVER_PORT , mockWebServer .httpPort ()),
93102 equalTo (PEER_SERVICE , "test-peer-service" ),
94103 equalTo (
95- AttributeKey .stringKey ("kubernetes-client.namespace" ), "namespace" ),
96- equalTo (AttributeKey .stringKey ("kubernetes-client.name" ), "name" ))));
104+ stringKey ("kubernetes-client.namespace" ),
105+ experimental ("namespace" )),
106+ equalTo (stringKey ("kubernetes-client.name" ), experimental ("name" )))));
97107 }
98108
99109 @ Test
@@ -103,10 +113,7 @@ void handleErrorsInSyncCall() {
103113 ApiException exception = null ;
104114 try {
105115 testing .runWithSpan (
106- "parent" ,
107- () -> {
108- coreV1Api .connectGetNamespacedPodProxy ("name" , "namespace" , "path" );
109- });
116+ "parent" , () -> coreV1Api .connectGetNamespacedPodProxy ("name" , "namespace" , "path" ));
110117 } catch (ApiException e ) {
111118 exception = e ;
112119 }
@@ -141,8 +148,9 @@ void handleErrorsInSyncCall() {
141148 equalTo (PEER_SERVICE , "test-peer-service" ),
142149 equalTo (ERROR_TYPE , "451" ),
143150 equalTo (
144- AttributeKey .stringKey ("kubernetes-client.namespace" ), "namespace" ),
145- equalTo (AttributeKey .stringKey ("kubernetes-client.name" ), "name" ))));
151+ stringKey ("kubernetes-client.namespace" ),
152+ experimental ("namespace" )),
153+ equalTo (stringKey ("kubernetes-client.name" ), experimental ("name" )))));
146154 }
147155
148156 @ Test
@@ -154,21 +162,20 @@ void asynchronousCall() throws ApiException, InterruptedException {
154162
155163 testing .runWithSpan (
156164 "parent" ,
157- () -> {
158- coreV1Api .connectGetNamespacedPodProxyAsync (
159- "name" ,
160- "namespace" ,
161- "path" ,
162- new ApiCallbackTemplate () {
163- @ Override
164- public void onSuccess (
165- String result , int statusCode , Map <String , List <String >> responseHeaders ) {
166- responseBodyReference .set (result );
167- countDownLatch .countDown ();
168- testing .runWithSpan ("callback" , () -> {});
169- }
170- });
171- });
165+ () ->
166+ coreV1Api .connectGetNamespacedPodProxyAsync (
167+ "name" ,
168+ "namespace" ,
169+ "path" ,
170+ new ApiCallbackTemplate () {
171+ @ Override
172+ public void onSuccess (
173+ String result , int statusCode , Map <String , List <String >> responseHeaders ) {
174+ responseBodyReference .set (result );
175+ countDownLatch .countDown ();
176+ testing .runWithSpan ("callback" , () -> {});
177+ }
178+ }));
172179
173180 countDownLatch .await ();
174181
@@ -195,8 +202,9 @@ public void onSuccess(
195202 equalTo (SERVER_PORT , mockWebServer .httpPort ()),
196203 equalTo (PEER_SERVICE , "test-peer-service" ),
197204 equalTo (
198- AttributeKey .stringKey ("kubernetes-client.namespace" ), "namespace" ),
199- equalTo (AttributeKey .stringKey ("kubernetes-client.name" ), "name" )),
205+ stringKey ("kubernetes-client.namespace" ),
206+ experimental ("namespace" )),
207+ equalTo (stringKey ("kubernetes-client.name" ), experimental ("name" ))),
200208 span ->
201209 span .hasName ("callback" )
202210 .hasKind (SpanKind .INTERNAL )
@@ -214,21 +222,20 @@ void handleErrorsInAsynchronousCall() throws ApiException, InterruptedException
214222
215223 testing .runWithSpan (
216224 "parent" ,
217- () -> {
218- coreV1Api .connectGetNamespacedPodProxyAsync (
219- "name" ,
220- "namespace" ,
221- "path" ,
222- new ApiCallbackTemplate () {
223- @ Override
224- public void onFailure (
225- ApiException e , int statusCode , Map <String , List <String >> responseHeaders ) {
226- exceptionReference .set (e );
227- countDownLatch .countDown ();
228- testing .runWithSpan ("callback" , () -> {});
229- }
230- });
231- });
225+ () ->
226+ coreV1Api .connectGetNamespacedPodProxyAsync (
227+ "name" ,
228+ "namespace" ,
229+ "path" ,
230+ new ApiCallbackTemplate () {
231+ @ Override
232+ public void onFailure (
233+ ApiException e , int statusCode , Map <String , List <String >> responseHeaders ) {
234+ exceptionReference .set (e );
235+ countDownLatch .countDown ();
236+ testing .runWithSpan ("callback" , () -> {});
237+ }
238+ }));
232239
233240 countDownLatch .await ();
234241
@@ -258,8 +265,9 @@ public void onFailure(
258265 equalTo (PEER_SERVICE , "test-peer-service" ),
259266 equalTo (ERROR_TYPE , "451" ),
260267 equalTo (
261- AttributeKey .stringKey ("kubernetes-client.namespace" ), "namespace" ),
262- equalTo (AttributeKey .stringKey ("kubernetes-client.name" ), "name" )),
268+ stringKey ("kubernetes-client.namespace" ),
269+ experimental ("namespace" )),
270+ equalTo (stringKey ("kubernetes-client.name" ), experimental ("name" ))),
263271 span ->
264272 span .hasName ("callback" )
265273 .hasKind (SpanKind .INTERNAL )
0 commit comments