Skip to content

Commit e09ab98

Browse files
committed
chore: add and use test-utils
1 parent f915b64 commit e09ab98

File tree

2 files changed

+62
-33
lines changed

2 files changed

+62
-33
lines changed

src/Instrumentation/Symfony/composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"vimeo/psalm": "6.4.0",
3333
"symfony/http-client": "^5.4||^6.0",
3434
"symfony/messenger": "^5.4||^6.0",
35+
"open-telemetry/test-utils": "^0.2.0",
3536
"open-telemetry/opentelemetry-propagation-traceresponse": "*",
3637
"open-telemetry/opentelemetry-propagation-server-timing": "*"
3738
},

src/Instrumentation/Symfony/tests/Integration/MessengerInstrumentationTest.php

+61-33
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use OpenTelemetry\API\Trace\SpanKind;
88
use OpenTelemetry\Contrib\Instrumentation\Symfony\MessengerInstrumentation;
99
use OpenTelemetry\SemConv\TraceAttributes;
10+
use OpenTelemetry\TestUtils\TraceStructureAssertionTrait;
1011
use Symfony\Component\Messenger\Envelope;
1112
use Symfony\Component\Messenger\MessageBus;
1213
use Symfony\Component\Messenger\MessageBusInterface;
@@ -57,6 +58,7 @@ public function __construct()
5758

5859
final class MessengerInstrumentationTest extends AbstractTest
5960
{
61+
use TraceStructureAssertionTrait;
6062
protected function getMessenger(): MessageBusInterface
6163
{
6264
return new MessageBus();
@@ -85,17 +87,16 @@ public function test_dispatch_message($message, string $spanName, int $kind, arr
8587

8688
$bus->dispatch($message);
8789

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+
);
99100
}
100101

101102
/**
@@ -110,17 +111,16 @@ public function test_send_message($message, string $spanName, int $kind, array $
110111
$transport = $this->getTransport();
111112
$transport->send(new Envelope($message));
112113

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+
);
124124
}
125125

126126
public function test_can_sustain_throw_while_dispatching()
@@ -134,9 +134,23 @@ public function dispatch(object $message, array $stamps = []): Envelope
134134

135135
try {
136136
$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+
);
140154
}
141155
}
142156

@@ -167,8 +181,22 @@ public function send(Envelope $envelope): Envelope
167181
try {
168182
$transport->send(new Envelope(new SendEmailMessage('Hello Again')));
169183
} 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+
);
172200
}
173201
}
174202

@@ -189,7 +217,7 @@ public function test_handle_message()
189217
// Get and handle the message
190218
$messages = iterator_to_array($transport->get());
191219
$message = $messages[0];
192-
220+
193221
// Use reflection to call the protected handleMessage method
194222
$reflection = new \ReflectionClass($worker);
195223
$handleMessageMethod = $reflection->getMethod('handleMessage');
@@ -276,7 +304,7 @@ public function test_stamp_information()
276304
{
277305
$transport = $this->getTransport();
278306
$message = new SendEmailMessage('Hello Again');
279-
307+
280308
// Add various stamps to the envelope
281309
$envelope = new Envelope($message, [
282310
new \Symfony\Component\Messenger\Stamp\BusNameStamp('test_bus'),
@@ -289,17 +317,17 @@ public function test_stamp_information()
289317
// We should have a send span with all stamp information
290318
$this->assertCount(1, $this->storage);
291319
$sendSpan = $this->storage[0];
292-
320+
293321
// Check stamp attributes
294322
$this->assertTrue($sendSpan->getAttributes()->has('messaging.symfony.bus'));
295323
$this->assertEquals('test_bus', $sendSpan->getAttributes()->get('messaging.symfony.bus'));
296-
324+
297325
$this->assertTrue($sendSpan->getAttributes()->has('messaging.symfony.delay'));
298326
$this->assertEquals(1000, $sendSpan->getAttributes()->get('messaging.symfony.delay'));
299-
327+
300328
$this->assertTrue($sendSpan->getAttributes()->has(TraceAttributes::MESSAGING_MESSAGE_ID));
301329
$this->assertEquals('test-id', $sendSpan->getAttributes()->get(TraceAttributes::MESSAGING_MESSAGE_ID));
302-
330+
303331
// Check stamps count
304332
$this->assertTrue($sendSpan->getAttributes()->has('messaging.symfony.stamps'));
305333
$stamps = json_decode($sendSpan->getAttributes()->get('messaging.symfony.stamps'), true);

0 commit comments

Comments
 (0)