Skip to content

Commit 8599ba4

Browse files
fix phpstan and php-cs-fixer errors (#359)
* fix phpstan and php-cs-fixer errors * add test cases
1 parent 5bf5a1a commit 8599ba4

File tree

2 files changed

+102
-4
lines changed

2 files changed

+102
-4
lines changed

src/Aws/src/AwsSdkInstrumentation.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Aws\Middleware;
99
use Aws\ResultInterface;
1010
use Closure;
11-
use Error;
1211
use GuzzleHttp\Promise;
1312
use OpenTelemetry\API\Instrumentation\InstrumentationInterface;
1413
use OpenTelemetry\API\Instrumentation\InstrumentationTrait;
@@ -18,6 +17,7 @@
1817
use OpenTelemetry\API\Trace\TracerProviderInterface;
1918
use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface;
2019
use Psr\Http\Message\RequestInterface;
20+
use Stringable;
2121
use Throwable;
2222

2323
/**
@@ -197,10 +197,10 @@ private function normalizeReason(mixed $reason): ?string
197197
return $reason->getMessage();
198198
}
199199

200-
try {
201-
return strval($reason);
202-
} catch (Error) {
200+
if (is_object($reason) && ! $reason instanceof Stringable) {
203201
return null;
204202
}
203+
204+
return (string) $reason;
205205
}
206206
}

src/Aws/tests/Integration/AwsSdkInstrumentationTest.php

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

77
use Aws\AwsClientInterface;
88
use Aws\EventBridge\EventBridgeClient;
9+
use Aws\Kms\Exception\KmsException;
10+
use Aws\Kms\KmsClient;
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 stdClass;
1621

1722
class AwsSdkInstrumentationTest extends TestCase
1823
{
@@ -213,4 +218,97 @@ public function testPreventsRepeatedInstrumentationOfSameClient()
213218
);
214219
}
215220
}
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+
}
216314
}

0 commit comments

Comments
 (0)