Skip to content

Commit 1f59788

Browse files
mitchierichiemitchierichie
authored andcommitted
add test cases
1 parent 8a95f95 commit 1f59788

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

src/Aws/tests/Integration/AwsSdkInstrumentationTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66

77
use Aws\AwsClientInterface;
88
use Aws\EventBridge\EventBridgeClient;
9+
use Aws\Kms\Exception\KmsException;
910
use Aws\S3\S3Client;
1011
use Aws\Sqs\SqsClient;
12+
use GuzzleHttp\Promise;
13+
use OpenTelemetry\API\Trace\StatusCode;
1114
use OpenTelemetry\Aws\AwsSdkInstrumentation;
1215
use OpenTelemetry\Aws\Xray\Propagator;
1316
use OpenTelemetry\SDK\Trace\ReadWriteSpanInterface;
1417
use OpenTelemetry\SDK\Trace\TracerProvider;
1518
use PHPUnit\Framework\TestCase;
19+
use stdClass;
1620

1721
class AwsSdkInstrumentationTest extends TestCase
1822
{
@@ -213,4 +217,94 @@ public function testPreventsRepeatedInstrumentationOfSameClient()
213217
);
214218
}
215219
}
220+
221+
public function testFailedOperationRecordsSpan()
222+
{
223+
$kmsClient = $this->getTestClient('KMS', ['region' => 'eu-west-1']);
224+
225+
$spanProcessor = new CollectingSpanProcessor();
226+
$this->awsSdkInstrumentation->setTracerProvider(new TracerProvider([$spanProcessor]));
227+
$this->awsSdkInstrumentation->setPropagator(new Propagator());
228+
$this->awsSdkInstrumentation->instrumentClients([$kmsClient]);
229+
$this->awsSdkInstrumentation->init();
230+
$this->awsSdkInstrumentation->activate();
231+
232+
try {
233+
$kmsClient->decrypt(['CiphertextBlob' => random_bytes(16)]);
234+
} catch (KmsException) {
235+
}
236+
237+
$collectedSpans = $spanProcessor->getCollectedSpans();
238+
$this->assertCount(1, $collectedSpans);
239+
240+
$span = array_shift($collectedSpans);
241+
242+
/** @var ReadWriteSpanInterface $span */
243+
$this->assertTrue($span->hasEnded());
244+
$this->assertSame(StatusCode::STATUS_ERROR, $span->toSpanData()->getStatus()->getCode());
245+
}
246+
247+
public function testRejectsSafelyWithNonStringableObject()
248+
{
249+
$kmsClient = $this->getTestClient('KMS', ['region' => 'eu-west-1']);
250+
251+
$spanProcessor = new CollectingSpanProcessor();
252+
$this->awsSdkInstrumentation->setTracerProvider(new TracerProvider([$spanProcessor]));
253+
$this->awsSdkInstrumentation->setPropagator(new Propagator());
254+
$this->awsSdkInstrumentation->instrumentClients([$kmsClient]);
255+
$this->awsSdkInstrumentation->init();
256+
$this->awsSdkInstrumentation->activate();
257+
258+
$kmsClient->getHandlerList()->appendSign(function () {
259+
return function () {
260+
return Promise\Create::rejectionFor(new stdClass());
261+
};
262+
});
263+
264+
try {
265+
$kmsClient->decrypt(['CiphertextBlob' => random_bytes(16)]);
266+
} catch (Promise\RejectionException) {
267+
}
268+
269+
$collectedSpans = $spanProcessor->getCollectedSpans();
270+
$this->assertCount(1, $collectedSpans);
271+
272+
$span = array_shift($collectedSpans);
273+
274+
/** @var ReadWriteSpanInterface $span */
275+
$this->assertTrue($span->hasEnded());
276+
$this->assertSame(StatusCode::STATUS_ERROR, $span->toSpanData()->getStatus()->getCode());
277+
}
278+
279+
public function testRejectsSafelyWithString()
280+
{
281+
$kmsClient = $this->getTestClient('KMS', ['region' => 'eu-west-1']);
282+
283+
$spanProcessor = new CollectingSpanProcessor();
284+
$this->awsSdkInstrumentation->setTracerProvider(new TracerProvider([$spanProcessor]));
285+
$this->awsSdkInstrumentation->setPropagator(new Propagator());
286+
$this->awsSdkInstrumentation->instrumentClients([$kmsClient]);
287+
$this->awsSdkInstrumentation->init();
288+
$this->awsSdkInstrumentation->activate();
289+
290+
$kmsClient->getHandlerList()->appendSign(function () {
291+
return function () {
292+
return Promise\Create::rejectionFor('failed');
293+
};
294+
});
295+
296+
try {
297+
$kmsClient->decrypt(['CiphertextBlob' => random_bytes(16)]);
298+
} catch (Promise\RejectionException) {
299+
}
300+
301+
$collectedSpans = $spanProcessor->getCollectedSpans();
302+
$this->assertCount(1, $collectedSpans);
303+
304+
$span = array_shift($collectedSpans);
305+
306+
/** @var ReadWriteSpanInterface $span */
307+
$this->assertTrue($span->hasEnded());
308+
$this->assertSame(StatusCode::STATUS_ERROR, $span->toSpanData()->getStatus()->getCode());
309+
}
216310
}

0 commit comments

Comments
 (0)