|
5 | 5 | namespace OpenTelemetry\Tests\Aws\Integration; |
6 | 6 |
|
7 | 7 | use Aws\AwsClientInterface; |
| 8 | +use Aws\CommandInterface; |
8 | 9 | use Aws\EventBridge\EventBridgeClient; |
| 10 | +use Aws\Kms\Exception\KmsException; |
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 Psr\Http\Message\RequestInterface; |
| 21 | +use stdClass; |
16 | 22 |
|
17 | 23 | class AwsSdkInstrumentationTest extends TestCase |
18 | 24 | { |
@@ -213,4 +219,91 @@ public function testPreventsRepeatedInstrumentationOfSameClient() |
213 | 219 | ); |
214 | 220 | } |
215 | 221 | } |
| 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 | + } |
216 | 309 | } |
0 commit comments