Skip to content

Commit edfc031

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

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

src/Aws/tests/Integration/AwsSdkInstrumentationTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@
55
namespace OpenTelemetry\Tests\Aws\Integration;
66

77
use Aws\AwsClientInterface;
8+
use Aws\CommandInterface;
89
use Aws\EventBridge\EventBridgeClient;
10+
use Aws\Kms\Exception\KmsException;
911
use Aws\S3\S3Client;
1012
use Aws\Sqs\SqsClient;
13+
use GuzzleHttp\Promise;
14+
use OpenTelemetry\API\Trace\StatusCode;
1115
use OpenTelemetry\Aws\AwsSdkInstrumentation;
1216
use OpenTelemetry\Aws\Xray\Propagator;
1317
use OpenTelemetry\SDK\Trace\ReadWriteSpanInterface;
1418
use OpenTelemetry\SDK\Trace\TracerProvider;
1519
use PHPUnit\Framework\TestCase;
20+
use Psr\Http\Message\RequestInterface;
21+
use stdClass;
1622

1723
class AwsSdkInstrumentationTest extends TestCase
1824
{
@@ -213,4 +219,91 @@ public function testPreventsRepeatedInstrumentationOfSameClient()
213219
);
214220
}
215221
}
222+
223+
public function testFailedOperationRecordsSpan()
224+
{
225+
$kmsClient = $this->getTestClient('KMS', ['region' => 'eu-west-1']);
226+
227+
$spanProcessor = new CollectingSpanProcessor();
228+
$this->awsSdkInstrumentation->setTracerProvider(new TracerProvider([$spanProcessor]));
229+
$this->awsSdkInstrumentation->setPropagator(new Propagator());
230+
$this->awsSdkInstrumentation->instrumentClients([$kmsClient]);
231+
$this->awsSdkInstrumentation->init();
232+
$this->awsSdkInstrumentation->activate();
233+
234+
try {
235+
$kmsClient->decrypt(['CiphertextBlob' => random_bytes(16)]);
236+
} catch (KmsException) {}
237+
238+
$collectedSpans = $spanProcessor->getCollectedSpans();
239+
$this->assertCount(1, $collectedSpans);
240+
241+
$span = array_shift($collectedSpans);
242+
243+
/** @var ReadWriteSpanInterface $span */
244+
$this->assertTrue($span->hasEnded());
245+
$this->assertSame(StatusCode::STATUS_ERROR, $span->toSpanData()->getStatus()->getCode());
246+
}
247+
248+
public function testRejectsSafelyWithNonStringableObject()
249+
{
250+
$kmsClient = $this->getTestClient('KMS', ['region' => 'eu-west-1']);
251+
252+
$spanProcessor = new CollectingSpanProcessor();
253+
$this->awsSdkInstrumentation->setTracerProvider(new TracerProvider([$spanProcessor]));
254+
$this->awsSdkInstrumentation->setPropagator(new Propagator());
255+
$this->awsSdkInstrumentation->instrumentClients([$kmsClient]);
256+
$this->awsSdkInstrumentation->init();
257+
$this->awsSdkInstrumentation->activate();
258+
259+
$kmsClient->getHandlerList()->appendSign(function (callable $handler) {
260+
return function () use ($handler) {
261+
return Promise\Create::rejectionFor(new stdClass());
262+
};
263+
});
264+
265+
try {
266+
$kmsClient->decrypt(['CiphertextBlob' => random_bytes(16)]);
267+
} catch (Promise\RejectionException) {}
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 (callable $handler) {
291+
return function () use ($handler) {
292+
return Promise\Create::rejectionFor('failed');
293+
};
294+
});
295+
296+
try {
297+
$kmsClient->decrypt(['CiphertextBlob' => random_bytes(16)]);
298+
} catch (Promise\RejectionException) {}
299+
300+
$collectedSpans = $spanProcessor->getCollectedSpans();
301+
$this->assertCount(1, $collectedSpans);
302+
303+
$span = array_shift($collectedSpans);
304+
305+
/** @var ReadWriteSpanInterface $span */
306+
$this->assertTrue($span->hasEnded());
307+
$this->assertSame(StatusCode::STATUS_ERROR, $span->toSpanData()->getStatus()->getCode());
308+
}
216309
}

0 commit comments

Comments
 (0)