99import static io .opentelemetry .sdk .testing .assertj .OpenTelemetryAssertions .equalTo ;
1010import static io .opentelemetry .semconv .incubating .CodeIncubatingAttributes .CODE_FUNCTION ;
1111import static io .opentelemetry .semconv .incubating .CodeIncubatingAttributes .CODE_NAMESPACE ;
12- import static org .assertj .core .api .Assertions .assertThatThrownBy ;
1312
14- import io .opentelemetry .api .trace .Span ;
1513import io .opentelemetry .api .trace .SpanKind ;
1614import io .opentelemetry .instrumentation .testing .junit .AgentInstrumentationExtension ;
1715import io .opentelemetry .instrumentation .testing .junit .InstrumentationExtension ;
1816import java .util .concurrent .Callable ;
19- import java .util .concurrent .CompletableFuture ;
20- import java .util .concurrent .CountDownLatch ;
21- import java .util .concurrent .TimeUnit ;
22- import java .util .concurrent .atomic .AtomicReference ;
23- import javax .naming .NoInitialContextException ;
24- import javax .naming .directory .InitialDirContext ;
25- import javax .naming .ldap .InitialLdapContext ;
2617import org .junit .jupiter .api .Test ;
2718import org .junit .jupiter .api .extension .RegisterExtension ;
2819
@@ -41,88 +32,16 @@ void methodTraced() {
4132 trace .hasSpansSatisfyingExactly (
4233 span ->
4334 span .hasName ("ConfigTracedCallable.call" )
44- .hasKind (SpanKind .INTERNAL )
35+ .hasKind (SpanKind .SERVER )
4536 .hasAttributesSatisfyingExactly (
4637 equalTo (CODE_NAMESPACE , ConfigTracedCallable .class .getName ()),
4738 equalTo (CODE_FUNCTION , "call" ))));
4839 }
49-
50- @ Test
51- void bootLoaderMethodTraced () throws Exception {
52- InitialLdapContext context = new InitialLdapContext ();
53- AtomicReference <Throwable > throwableReference = new AtomicReference <>();
54- assertThatThrownBy (
55- () -> {
56- try {
57- context .search ("foo" , null );
58- } catch (Throwable throwable ) {
59- throwableReference .set (throwable );
60- throw throwable ;
61- }
62- })
63- .isInstanceOf (NoInitialContextException .class );
64-
65- testing .waitAndAssertTraces (
66- trace ->
67- trace .hasSpansSatisfyingExactly (
68- span ->
69- span .hasName ("InitialDirContext.search" )
70- .hasKind (SpanKind .CLIENT )
71- .hasException (throwableReference .get ())
72- .hasAttributesSatisfyingExactly (
73- equalTo (CODE_NAMESPACE , InitialDirContext .class .getName ()),
74- equalTo (CODE_FUNCTION , "search" ))));
75- }
76-
7740 static class ConfigTracedCallable implements Callable <String > {
7841
7942 @ Override
8043 public String call () {
8144 return "Hello!" ;
8245 }
8346 }
84-
85- @ Test
86- void methodTracedWithAsyncStop () throws Exception {
87- ConfigTracedCompletableFuture traced = new ConfigTracedCompletableFuture ();
88- CompletableFuture <String > future = traced .getResult ();
89-
90- // span is ended when CompletableFuture is completed
91- // verify that span has not been ended yet
92- assertThat (traced .span ).isNotNull ().satisfies (span -> assertThat (span .isRecording ()).isTrue ());
93-
94- traced .countDownLatch .countDown ();
95- assertThat (future .get (10 , TimeUnit .SECONDS )).isEqualTo ("Hello!" );
96-
97- testing .waitAndAssertTraces (
98- trace ->
99- trace .hasSpansSatisfyingExactly (
100- span ->
101- span .hasName ("ConfigTracedCompletableFuture.getResult" )
102- .hasKind (SpanKind .SERVER )
103- .hasAttributesSatisfyingExactly (
104- equalTo (CODE_NAMESPACE , ConfigTracedCompletableFuture .class .getName ()),
105- equalTo (CODE_FUNCTION , "getResult" ))));
106- }
107-
108- static class ConfigTracedCompletableFuture {
109- final CountDownLatch countDownLatch = new CountDownLatch (1 );
110- Span span ;
111-
112- CompletableFuture <String > getResult () {
113- CompletableFuture <String > completableFuture = new CompletableFuture <>();
114- span = Span .current ();
115- new Thread (
116- () -> {
117- try {
118- countDownLatch .await ();
119- } catch (InterruptedException exception ) {
120- // ignore
121- }
122- completableFuture .complete ("Hello!" );
123- })
124- .start ();
125- return completableFuture ;
126- }
127- }
12847}
0 commit comments