5
5
6
6
package io .opentelemetry .test .annotation ;
7
7
8
+ import static io .opentelemetry .api .common .AttributeKey .stringKey ;
8
9
import 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 ;
10
13
11
- import io .opentelemetry .api .common .AttributeKey ;
12
14
import io .opentelemetry .api .trace .Span ;
13
- import io .opentelemetry .api .trace .SpanId ;
14
15
import io .opentelemetry .api .trace .SpanKind ;
15
16
import io .opentelemetry .instrumentation .testing .junit .AgentInstrumentationExtension ;
16
- import io .opentelemetry .semconv .incubating .CodeIncubatingAttributes ;
17
17
import org .junit .jupiter .api .Test ;
18
18
import org .junit .jupiter .api .extension .RegisterExtension ;
19
19
@@ -24,89 +24,60 @@ class AddingSpanAttributesInstrumentationTest {
24
24
AgentInstrumentationExtension .create ();
25
25
26
26
@ Test
27
- void captureAttributesInNewSpan () throws Exception {
28
-
27
+ void captureAttributesInNewSpan () {
29
28
testing .runWithSpan (
30
29
"root" ,
31
30
() ->
32
31
new ExtractAttributesUsingAddingSpanAttributes ()
33
32
.withSpanTakesPrecedence ("foo" , "bar" , null , "baz" ));
34
33
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" ))));
67
50
}
68
51
69
52
@ Test
70
- void captureAttributesInCurrentSpan () throws Exception {
71
-
53
+ void captureAttributesInCurrentSpan () {
72
54
testing .runWithSpan (
73
55
"root" ,
74
56
() ->
75
57
new ExtractAttributesUsingAddingSpanAttributes ()
76
58
.withSpanAttributes ("foo" , "bar" , null , "baz" ));
77
59
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" ))));
97
70
}
98
71
99
72
@ Test
100
- void noExistingSpan () throws Exception {
101
-
73
+ void noExistingSpan () {
102
74
new ExtractAttributesUsingAddingSpanAttributes ().withSpanAttributes ("foo" , "bar" , null , "baz" );
103
75
104
76
assertThat (testing .waitForTraces (0 )).isEmpty ();
105
77
}
106
78
107
79
@ Test
108
- void overwriteAttributes () throws Exception {
109
-
80
+ void overwriteAttributes () {
110
81
testing .runWithSpan (
111
82
"root" ,
112
83
() -> {
@@ -116,31 +87,21 @@ void overwriteAttributes() throws Exception {
116
87
.withSpanAttributes ("foo" , "bar" , null , "baz" );
117
88
});
118
89
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" ))));
139
101
}
140
102
141
103
@ Test
142
- void multiMethodOverwriteAttributes () throws Exception {
143
-
104
+ void multiMethodOverwriteAttributes () {
144
105
testing .runWithSpan (
145
106
"root" ,
146
107
() -> {
@@ -150,25 +111,16 @@ void multiMethodOverwriteAttributes() throws Exception {
150
111
.withSpanAttributesParent ("parentbegone" , "parentbegone" , null , "parentbegone" );
151
112
});
152
113
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" ))));
173
125
}
174
126
}
0 commit comments