7
7
use OpenTelemetry \API \Trace \SpanKind ;
8
8
use OpenTelemetry \Contrib \Instrumentation \Symfony \MessengerInstrumentation ;
9
9
use OpenTelemetry \SemConv \TraceAttributes ;
10
+ use OpenTelemetry \TestUtils \TraceStructureAssertionTrait ;
10
11
use Symfony \Component \Messenger \Envelope ;
11
12
use Symfony \Component \Messenger \MessageBus ;
12
13
use Symfony \Component \Messenger \MessageBusInterface ;
@@ -57,6 +58,7 @@ public function __construct()
57
58
58
59
final class MessengerInstrumentationTest extends AbstractTest
59
60
{
61
+ use TraceStructureAssertionTrait;
60
62
protected function getMessenger (): MessageBusInterface
61
63
{
62
64
return new MessageBus ();
@@ -85,17 +87,16 @@ public function test_dispatch_message($message, string $spanName, int $kind, arr
85
87
86
88
$ bus ->dispatch ($ message );
87
89
88
- $ this ->assertCount (1 , $ this ->storage );
89
-
90
- $ span = $ this ->storage [0 ];
91
-
92
- $ this ->assertEquals ($ spanName , $ span ->getName ());
93
- $ this ->assertEquals ($ kind , $ span ->getKind ());
94
-
95
- foreach ($ attributes as $ key => $ value ) {
96
- $ this ->assertTrue ($ span ->getAttributes ()->has ($ key ), sprintf ('Attribute %s not found ' , $ key ));
97
- $ this ->assertEquals ($ value , $ span ->getAttributes ()->get ($ key ));
98
- }
90
+ $ this ->assertTraceStructure (
91
+ $ this ->storage ,
92
+ [
93
+ [
94
+ 'name ' => $ spanName ,
95
+ 'kind ' => $ kind ,
96
+ 'attributes ' => $ attributes ,
97
+ ],
98
+ ]
99
+ );
99
100
}
100
101
101
102
/**
@@ -110,17 +111,16 @@ public function test_send_message($message, string $spanName, int $kind, array $
110
111
$ transport = $ this ->getTransport ();
111
112
$ transport ->send (new Envelope ($ message ));
112
113
113
- $ this ->assertCount (1 , $ this ->storage );
114
-
115
- $ span = $ this ->storage [0 ];
116
-
117
- $ this ->assertEquals ($ spanName , $ span ->getName ());
118
- $ this ->assertEquals ($ kind , $ span ->getKind ());
119
-
120
- foreach ($ attributes as $ key => $ value ) {
121
- $ this ->assertTrue ($ span ->getAttributes ()->has ($ key ), sprintf ('Attribute %s not found ' , $ key ));
122
- $ this ->assertEquals ($ value , $ span ->getAttributes ()->get ($ key ));
123
- }
114
+ $ this ->assertTraceStructure (
115
+ $ this ->storage ,
116
+ [
117
+ [
118
+ 'name ' => $ spanName ,
119
+ 'kind ' => $ kind ,
120
+ 'attributes ' => $ attributes ,
121
+ ],
122
+ ]
123
+ );
124
124
}
125
125
126
126
public function test_can_sustain_throw_while_dispatching ()
@@ -134,9 +134,23 @@ public function dispatch(object $message, array $stamps = []): Envelope
134
134
135
135
try {
136
136
$ bus ->dispatch (new SendEmailMessage ('Hello Again ' ));
137
- } catch (\Throwable $ e ) {
138
- $ this ->assertCount (1 , $ this ->storage );
139
- $ this ->assertArrayHasKey (0 , $ this ->storage );
137
+ } catch (\Exception $ e ) {
138
+ // Expected exception
139
+ $ this ->assertEquals ('booo! ' , $ e ->getMessage ());
140
+
141
+ // Now check the trace structure
142
+ $ this ->assertTraceStructure (
143
+ $ this ->storage ,
144
+ [
145
+ [
146
+ 'name ' => 'DISPATCH OpenTelemetry \\Tests \\Instrumentation \\Symfony \\tests \\Integration \\SendEmailMessage ' ,
147
+ 'kind ' => SpanKind::KIND_PRODUCER ,
148
+ 'attributes ' => [
149
+ MessengerInstrumentation::ATTRIBUTE_MESSENGER_MESSAGE => 'OpenTelemetry\Tests\Instrumentation\Symfony\tests\Integration\SendEmailMessage ' ,
150
+ ],
151
+ ],
152
+ ]
153
+ );
140
154
}
141
155
}
142
156
@@ -167,8 +181,22 @@ public function send(Envelope $envelope): Envelope
167
181
try {
168
182
$ transport ->send (new Envelope (new SendEmailMessage ('Hello Again ' )));
169
183
} catch (\Throwable $ e ) {
170
- $ this ->assertCount (1 , $ this ->storage );
171
- $ this ->assertArrayHasKey (0 , $ this ->storage );
184
+ // Expected exception
185
+ $ this ->assertEquals ('booo! ' , $ e ->getMessage ());
186
+
187
+ // Now check the trace structure
188
+ $ this ->assertTraceStructure (
189
+ $ this ->storage ,
190
+ [
191
+ [
192
+ 'name ' => 'SEND OpenTelemetry \\Tests \\Instrumentation \\Symfony \\tests \\Integration \\SendEmailMessage ' ,
193
+ 'kind ' => SpanKind::KIND_PRODUCER ,
194
+ 'attributes ' => [
195
+ MessengerInstrumentation::ATTRIBUTE_MESSENGER_MESSAGE => 'OpenTelemetry\Tests\Instrumentation\Symfony\tests\Integration\SendEmailMessage ' ,
196
+ ],
197
+ ],
198
+ ]
199
+ );
172
200
}
173
201
}
174
202
@@ -189,7 +217,7 @@ public function test_handle_message()
189
217
// Get and handle the message
190
218
$ messages = iterator_to_array ($ transport ->get ());
191
219
$ message = $ messages [0 ];
192
-
220
+
193
221
// Use reflection to call the protected handleMessage method
194
222
$ reflection = new \ReflectionClass ($ worker );
195
223
$ handleMessageMethod = $ reflection ->getMethod ('handleMessage ' );
@@ -276,7 +304,7 @@ public function test_stamp_information()
276
304
{
277
305
$ transport = $ this ->getTransport ();
278
306
$ message = new SendEmailMessage ('Hello Again ' );
279
-
307
+
280
308
// Add various stamps to the envelope
281
309
$ envelope = new Envelope ($ message , [
282
310
new \Symfony \Component \Messenger \Stamp \BusNameStamp ('test_bus ' ),
@@ -289,17 +317,17 @@ public function test_stamp_information()
289
317
// We should have a send span with all stamp information
290
318
$ this ->assertCount (1 , $ this ->storage );
291
319
$ sendSpan = $ this ->storage [0 ];
292
-
320
+
293
321
// Check stamp attributes
294
322
$ this ->assertTrue ($ sendSpan ->getAttributes ()->has ('messaging.symfony.bus ' ));
295
323
$ this ->assertEquals ('test_bus ' , $ sendSpan ->getAttributes ()->get ('messaging.symfony.bus ' ));
296
-
324
+
297
325
$ this ->assertTrue ($ sendSpan ->getAttributes ()->has ('messaging.symfony.delay ' ));
298
326
$ this ->assertEquals (1000 , $ sendSpan ->getAttributes ()->get ('messaging.symfony.delay ' ));
299
-
327
+
300
328
$ this ->assertTrue ($ sendSpan ->getAttributes ()->has (TraceAttributes::MESSAGING_MESSAGE_ID ));
301
329
$ this ->assertEquals ('test-id ' , $ sendSpan ->getAttributes ()->get (TraceAttributes::MESSAGING_MESSAGE_ID ));
302
-
330
+
303
331
// Check stamps count
304
332
$ this ->assertTrue ($ sendSpan ->getAttributes ()->has ('messaging.symfony.stamps ' ));
305
333
$ stamps = json_decode ($ sendSpan ->getAttributes ()->get ('messaging.symfony.stamps ' ), true );
0 commit comments