55
66package io .opentelemetry .test .annotation ;
77
8+ import static io .opentelemetry .api .common .AttributeKey .stringKey ;
89import static io .opentelemetry .sdk .testing .assertj .OpenTelemetryAssertions .assertThat ;
9- import static org .assertj .core .api .Assertions .entry ;
10+ import static io .opentelemetry .sdk .testing .assertj .OpenTelemetryAssertions .equalTo ;
11+ import static io .opentelemetry .semconv .incubating .CodeIncubatingAttributes .CODE_FUNCTION ;
12+ import static io .opentelemetry .semconv .incubating .CodeIncubatingAttributes .CODE_NAMESPACE ;
1013
11- import io .opentelemetry .api .common .AttributeKey ;
1214import io .opentelemetry .api .trace .Span ;
13- import io .opentelemetry .api .trace .SpanId ;
1415import io .opentelemetry .api .trace .SpanKind ;
1516import io .opentelemetry .instrumentation .testing .junit .AgentInstrumentationExtension ;
16- import io .opentelemetry .semconv .incubating .CodeIncubatingAttributes ;
1717import org .junit .jupiter .api .Test ;
1818import org .junit .jupiter .api .extension .RegisterExtension ;
1919
@@ -24,89 +24,60 @@ class AddingSpanAttributesInstrumentationTest {
2424 AgentInstrumentationExtension .create ();
2525
2626 @ Test
27- void captureAttributesInNewSpan () throws Exception {
28-
27+ void captureAttributesInNewSpan () {
2928 testing .runWithSpan (
3029 "root" ,
3130 () ->
3231 new ExtractAttributesUsingAddingSpanAttributes ()
3332 .withSpanTakesPrecedence ("foo" , "bar" , null , "baz" ));
3433
35- assertThat (testing .waitForTraces (1 ))
36- .satisfiesExactly (
37- trace ->
38- assertThat (trace )
39- .satisfiesExactly (
40- span ->
41- assertThat (span )
42- .hasName ("root" )
43- .hasKind (SpanKind .INTERNAL )
44- .hasParentSpanId (SpanId .getInvalid ()),
45- span ->
46- assertThat (span )
47- .hasName (
48- "ExtractAttributesUsingAddingSpanAttributes.withSpanTakesPrecedence" )
49- .hasKind (SpanKind .INTERNAL )
50- .hasParentSpanId (trace .get (0 ).getSpanId ())
51- .hasAttributesSatisfying (
52- attributes ->
53- assertThat (attributes )
54- .containsOnly (
55- entry (
56- CodeIncubatingAttributes .CODE_NAMESPACE ,
57- ExtractAttributesUsingAddingSpanAttributes .class
58- .getName ()),
59- entry (
60- CodeIncubatingAttributes .CODE_FUNCTION ,
61- "withSpanTakesPrecedence" ),
62- entry (
63- AttributeKey .stringKey ("implicitName" ), "foo" ),
64- entry (
65- AttributeKey .stringKey ("explicitName" ),
66- "bar" )))));
34+ testing .waitAndAssertTraces (
35+ trace ->
36+ trace .hasSpansSatisfyingExactly (
37+ span -> span .hasName ("root" ).hasKind (SpanKind .INTERNAL ).hasNoParent (),
38+ span ->
39+ span .hasName (
40+ "ExtractAttributesUsingAddingSpanAttributes.withSpanTakesPrecedence" )
41+ .hasKind (SpanKind .INTERNAL )
42+ .hasParentSpanId (trace .getSpan (0 ).getSpanId ())
43+ .hasAttributesSatisfyingExactly (
44+ equalTo (
45+ CODE_NAMESPACE ,
46+ ExtractAttributesUsingAddingSpanAttributes .class .getName ()),
47+ equalTo (CODE_FUNCTION , "withSpanTakesPrecedence" ),
48+ equalTo (stringKey ("implicitName" ), "foo" ),
49+ equalTo (stringKey ("explicitName" ), "bar" ))));
6750 }
6851
6952 @ Test
70- void captureAttributesInCurrentSpan () throws Exception {
71-
53+ void captureAttributesInCurrentSpan () {
7254 testing .runWithSpan (
7355 "root" ,
7456 () ->
7557 new ExtractAttributesUsingAddingSpanAttributes ()
7658 .withSpanAttributes ("foo" , "bar" , null , "baz" ));
7759
78- assertThat (testing .waitForTraces (1 ))
79- .satisfiesExactly (
80- trace ->
81- assertThat (trace )
82- .satisfiesExactly (
83- span ->
84- assertThat (span )
85- .hasName ("root" )
86- .hasKind (SpanKind .INTERNAL )
87- .hasParentSpanId (SpanId .getInvalid ())
88- .hasAttributesSatisfying (
89- attributes ->
90- assertThat (attributes )
91- .containsOnly (
92- entry (
93- AttributeKey .stringKey ("implicitName" ), "foo" ),
94- entry (
95- AttributeKey .stringKey ("explicitName" ),
96- "bar" )))));
60+ testing .waitAndAssertTraces (
61+ trace ->
62+ trace .hasSpansSatisfyingExactly (
63+ span ->
64+ span .hasName ("root" )
65+ .hasKind (SpanKind .INTERNAL )
66+ .hasNoParent ()
67+ .hasAttributesSatisfyingExactly (
68+ equalTo (stringKey ("implicitName" ), "foo" ),
69+ equalTo (stringKey ("explicitName" ), "bar" ))));
9770 }
9871
9972 @ Test
100- void noExistingSpan () throws Exception {
101-
73+ void noExistingSpan () {
10274 new ExtractAttributesUsingAddingSpanAttributes ().withSpanAttributes ("foo" , "bar" , null , "baz" );
10375
10476 assertThat (testing .waitForTraces (0 )).isEmpty ();
10577 }
10678
10779 @ Test
108- void overwriteAttributes () throws Exception {
109-
80+ void overwriteAttributes () {
11081 testing .runWithSpan (
11182 "root" ,
11283 () -> {
@@ -116,31 +87,21 @@ void overwriteAttributes() throws Exception {
11687 .withSpanAttributes ("foo" , "bar" , null , "baz" );
11788 });
11889
119- assertThat (testing .waitForTraces (1 ))
120- .satisfiesExactly (
121- trace ->
122- assertThat (trace )
123- .satisfiesExactly (
124- span ->
125- assertThat (span )
126- .hasName ("root" )
127- .hasKind (SpanKind .INTERNAL )
128- .hasParentSpanId (SpanId .getInvalid ())
129- .hasAttributesSatisfying (
130- attributes ->
131- assertThat (attributes )
132- .containsOnly (
133- entry (AttributeKey .stringKey ("keep" ), "willbekept" ),
134- entry (
135- AttributeKey .stringKey ("implicitName" ), "foo" ),
136- entry (
137- AttributeKey .stringKey ("explicitName" ),
138- "bar" )))));
90+ testing .waitAndAssertTraces (
91+ trace ->
92+ trace .hasSpansSatisfyingExactly (
93+ span ->
94+ span .hasName ("root" )
95+ .hasKind (SpanKind .INTERNAL )
96+ .hasNoParent ()
97+ .hasAttributesSatisfyingExactly (
98+ equalTo (stringKey ("keep" ), "willbekept" ),
99+ equalTo (stringKey ("implicitName" ), "foo" ),
100+ equalTo (stringKey ("explicitName" ), "bar" ))));
139101 }
140102
141103 @ Test
142- void multiMethodOverwriteAttributes () throws Exception {
143-
104+ void multiMethodOverwriteAttributes () {
144105 testing .runWithSpan (
145106 "root" ,
146107 () -> {
@@ -150,25 +111,16 @@ void multiMethodOverwriteAttributes() throws Exception {
150111 .withSpanAttributesParent ("parentbegone" , "parentbegone" , null , "parentbegone" );
151112 });
152113
153- assertThat (testing .waitForTraces (1 ))
154- .satisfiesExactly (
155- trace ->
156- assertThat (trace )
157- .satisfiesExactly (
158- span ->
159- assertThat (span )
160- .hasName ("root" )
161- .hasKind (SpanKind .INTERNAL )
162- .hasParentSpanId (SpanId .getInvalid ())
163- .hasAttributesSatisfying (
164- attributes ->
165- assertThat (attributes )
166- .containsOnly (
167- entry (AttributeKey .stringKey ("keep" ), "willbekept" ),
168- entry (
169- AttributeKey .stringKey ("implicitName" ), "foo" ),
170- entry (
171- AttributeKey .stringKey ("explicitName" ),
172- "bar" )))));
114+ testing .waitAndAssertTraces (
115+ trace ->
116+ trace .hasSpansSatisfyingExactly (
117+ span ->
118+ span .hasName ("root" )
119+ .hasKind (SpanKind .INTERNAL )
120+ .hasNoParent ()
121+ .hasAttributesSatisfyingExactly (
122+ equalTo (stringKey ("keep" ), "willbekept" ),
123+ equalTo (stringKey ("implicitName" ), "foo" ),
124+ equalTo (stringKey ("explicitName" ), "bar" ))));
173125 }
174126}
0 commit comments