|
6 | 6 |
|
7 | 7 | use Aws\AwsClientInterface; |
8 | 8 | use Aws\EventBridge\EventBridgeClient; |
| 9 | +use Aws\Kms\Exception\KmsException; |
| 10 | +use Aws\Kms\KmsClient; |
9 | 11 | use Aws\S3\S3Client; |
10 | 12 | use Aws\Sqs\SqsClient; |
| 13 | +use GuzzleHttp\Promise; |
| 14 | +use OpenTelemetry\API\Trace\StatusCode; |
11 | 15 | use OpenTelemetry\Aws\AwsSdkInstrumentation; |
12 | 16 | use OpenTelemetry\Aws\Xray\Propagator; |
13 | 17 | use OpenTelemetry\SDK\Trace\ReadWriteSpanInterface; |
14 | 18 | use OpenTelemetry\SDK\Trace\TracerProvider; |
15 | 19 | use PHPUnit\Framework\TestCase; |
| 20 | +use stdClass; |
16 | 21 |
|
17 | 22 | class AwsSdkInstrumentationTest extends TestCase |
18 | 23 | { |
@@ -213,4 +218,97 @@ public function testPreventsRepeatedInstrumentationOfSameClient() |
213 | 218 | ); |
214 | 219 | } |
215 | 220 | } |
| 221 | + |
| 222 | + public function testFailedOperationRecordsSpan() |
| 223 | + { |
| 224 | + /** @var KmsClient $kmsClient */ |
| 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 | + |
| 239 | + $collectedSpans = $spanProcessor->getCollectedSpans(); |
| 240 | + $this->assertCount(1, $collectedSpans); |
| 241 | + |
| 242 | + $span = array_shift($collectedSpans); |
| 243 | + |
| 244 | + /** @var ReadWriteSpanInterface $span */ |
| 245 | + $this->assertTrue($span->hasEnded()); |
| 246 | + $this->assertSame(StatusCode::STATUS_ERROR, $span->toSpanData()->getStatus()->getCode()); |
| 247 | + } |
| 248 | + |
| 249 | + public function testRejectsSafelyWithNonStringableObject() |
| 250 | + { |
| 251 | + /** @var KmsClient $kmsClient */ |
| 252 | + $kmsClient = $this->getTestClient('KMS', ['region' => 'eu-west-1']); |
| 253 | + |
| 254 | + $spanProcessor = new CollectingSpanProcessor(); |
| 255 | + $this->awsSdkInstrumentation->setTracerProvider(new TracerProvider([$spanProcessor])); |
| 256 | + $this->awsSdkInstrumentation->setPropagator(new Propagator()); |
| 257 | + $this->awsSdkInstrumentation->instrumentClients([$kmsClient]); |
| 258 | + $this->awsSdkInstrumentation->init(); |
| 259 | + $this->awsSdkInstrumentation->activate(); |
| 260 | + |
| 261 | + $kmsClient->getHandlerList()->appendSign(function () { |
| 262 | + return function () { |
| 263 | + return Promise\Create::rejectionFor(new stdClass()); |
| 264 | + }; |
| 265 | + }); |
| 266 | + |
| 267 | + try { |
| 268 | + $kmsClient->decrypt(['CiphertextBlob' => random_bytes(16)]); |
| 269 | + } catch (Promise\RejectionException) { |
| 270 | + } |
| 271 | + |
| 272 | + $collectedSpans = $spanProcessor->getCollectedSpans(); |
| 273 | + $this->assertCount(1, $collectedSpans); |
| 274 | + |
| 275 | + $span = array_shift($collectedSpans); |
| 276 | + |
| 277 | + /** @var ReadWriteSpanInterface $span */ |
| 278 | + $this->assertTrue($span->hasEnded()); |
| 279 | + $this->assertSame(StatusCode::STATUS_ERROR, $span->toSpanData()->getStatus()->getCode()); |
| 280 | + } |
| 281 | + |
| 282 | + public function testRejectsSafelyWithString() |
| 283 | + { |
| 284 | + /** @var KmsClient $kmsClient */ |
| 285 | + $kmsClient = $this->getTestClient('KMS', ['region' => 'eu-west-1']); |
| 286 | + |
| 287 | + $spanProcessor = new CollectingSpanProcessor(); |
| 288 | + $this->awsSdkInstrumentation->setTracerProvider(new TracerProvider([$spanProcessor])); |
| 289 | + $this->awsSdkInstrumentation->setPropagator(new Propagator()); |
| 290 | + $this->awsSdkInstrumentation->instrumentClients([$kmsClient]); |
| 291 | + $this->awsSdkInstrumentation->init(); |
| 292 | + $this->awsSdkInstrumentation->activate(); |
| 293 | + |
| 294 | + $kmsClient->getHandlerList()->appendSign(function () { |
| 295 | + return function () { |
| 296 | + return Promise\Create::rejectionFor('failed'); |
| 297 | + }; |
| 298 | + }); |
| 299 | + |
| 300 | + try { |
| 301 | + $kmsClient->decrypt(['CiphertextBlob' => random_bytes(16)]); |
| 302 | + } catch (Promise\RejectionException) { |
| 303 | + } |
| 304 | + |
| 305 | + $collectedSpans = $spanProcessor->getCollectedSpans(); |
| 306 | + $this->assertCount(1, $collectedSpans); |
| 307 | + |
| 308 | + $span = array_shift($collectedSpans); |
| 309 | + |
| 310 | + /** @var ReadWriteSpanInterface $span */ |
| 311 | + $this->assertTrue($span->hasEnded()); |
| 312 | + $this->assertSame(StatusCode::STATUS_ERROR, $span->toSpanData()->getStatus()->getCode()); |
| 313 | + } |
216 | 314 | } |
0 commit comments