|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenTelemetry\Tests\Aws\Integration; |
| 6 | + |
| 7 | +use Aws\AwsClientInterface; |
| 8 | +use Aws\EventBridge\EventBridgeClient; |
| 9 | +use Aws\S3\S3Client; |
| 10 | +use Aws\Sqs\SqsClient; |
| 11 | +use OpenTelemetry\Aws\AwsSdkInstrumentation; |
| 12 | +use OpenTelemetry\Aws\Xray\Propagator; |
| 13 | +use OpenTelemetry\SDK\Trace\ReadWriteSpanInterface; |
| 14 | +use OpenTelemetry\SDK\Trace\TracerProvider; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +class AwsSdkInstrumentationTest extends TestCase |
| 18 | +{ |
| 19 | + use UsesServiceTrait; |
| 20 | + |
| 21 | + private const HANDLERS_PER_ACTIVATION = 2; // one init and one sign middleware |
| 22 | + |
| 23 | + private AwsSdkInstrumentation $awsSdkInstrumentation; |
| 24 | + |
| 25 | + protected function setUp(): void |
| 26 | + { |
| 27 | + $this->awsSdkInstrumentation = new AwsSdkInstrumentation(); |
| 28 | + } |
| 29 | + |
| 30 | + public function testProperClientNameAndRegionIsPassedToSpanForSingleClientCall() |
| 31 | + { |
| 32 | + /** @var SqsClient $sqsClient */ |
| 33 | + $sqsClient = $this->getTestClient('SQS', ['region' => 'eu-west-1']); |
| 34 | + /** @var S3Client $s3Client */ |
| 35 | + $s3Client = $this->getTestClient('S3', ['region' => 'us-east-1']); |
| 36 | + $this->addMockResults($s3Client, [[]]); |
| 37 | + /** @var EventBridgeClient $eventBridgeClient */ |
| 38 | + $eventBridgeClient = $this->getTestClient('EventBridge', ['region' => 'ap-southeast-2']); |
| 39 | + |
| 40 | + $spanProcessor = new CollectingSpanProcessor(); |
| 41 | + $this->awsSdkInstrumentation->instrumentClients([$sqsClient, $s3Client, $eventBridgeClient]); |
| 42 | + $this->awsSdkInstrumentation->setPropagator(new Propagator()); |
| 43 | + $this->awsSdkInstrumentation->setTracerProvider(new TracerProvider([$spanProcessor])); |
| 44 | + $this->awsSdkInstrumentation->init(); |
| 45 | + $this->awsSdkInstrumentation->activate(); |
| 46 | + |
| 47 | + $s3Client->listBuckets(); |
| 48 | + |
| 49 | + $collectedSpans = $spanProcessor->getCollectedSpans(); |
| 50 | + $this->assertCount(1, $collectedSpans); |
| 51 | + |
| 52 | + /** @var ReadWriteSpanInterface $span */ |
| 53 | + $span = reset($collectedSpans); |
| 54 | + $this->assertTrue($span->hasEnded()); |
| 55 | + |
| 56 | + $attributes = $span->toSpanData()->getAttributes()->toArray(); |
| 57 | + $this->assertArrayHasKey('rpc.service', $attributes); |
| 58 | + $this->assertSame('s3', $attributes['rpc.service']); |
| 59 | + $this->assertArrayHasKey('aws.region', $attributes); |
| 60 | + $this->assertSame('us-east-1', $attributes['aws.region']); |
| 61 | + } |
| 62 | + |
| 63 | + public function testProperClientNameAndRegionIsPassedToSpanForDoubleCallToSameClient() |
| 64 | + { |
| 65 | + /** @var SqsClient $sqsClient */ |
| 66 | + $sqsClient = $this->getTestClient('SQS', ['region' => 'eu-west-1']); |
| 67 | + /** @var S3Client $s3Client */ |
| 68 | + $s3Client = $this->getTestClient('S3', ['region' => 'us-east-1']); |
| 69 | + $this->addMockResults($s3Client, [[], []]); |
| 70 | + /** @var EventBridgeClient $eventBridgeClient */ |
| 71 | + $eventBridgeClient = $this->getTestClient('EventBridge', ['region' => 'ap-southeast-2']); |
| 72 | + |
| 73 | + $spanProcessor = new CollectingSpanProcessor(); |
| 74 | + $this->awsSdkInstrumentation->instrumentClients([$sqsClient, $s3Client, $eventBridgeClient]); |
| 75 | + $this->awsSdkInstrumentation->setPropagator(new Propagator()); |
| 76 | + $this->awsSdkInstrumentation->setTracerProvider(new TracerProvider([$spanProcessor])); |
| 77 | + $this->awsSdkInstrumentation->init(); |
| 78 | + $this->awsSdkInstrumentation->activate(); |
| 79 | + |
| 80 | + $s3Client->listBuckets(); |
| 81 | + $s3Client->listObjects(['Bucket' => 'foo']); |
| 82 | + |
| 83 | + $collectedSpans = $spanProcessor->getCollectedSpans(); |
| 84 | + $this->assertCount(2, $collectedSpans); |
| 85 | + |
| 86 | + /** @var ReadWriteSpanInterface $span */ |
| 87 | + foreach ($collectedSpans as $span) { |
| 88 | + $this->assertTrue($span->hasEnded()); |
| 89 | + $attributes = $span->toSpanData()->getAttributes()->toArray(); |
| 90 | + $this->assertArrayHasKey('rpc.service', $attributes); |
| 91 | + $this->assertSame('s3', $attributes['rpc.service']); |
| 92 | + $this->assertArrayHasKey('aws.region', $attributes); |
| 93 | + $this->assertSame('us-east-1', $attributes['aws.region']); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public function testProperClientNameAndRegionIsPassedToSpanForDoubleCallToDifferentClients() |
| 98 | + { |
| 99 | + /** @var SqsClient $sqsClient */ |
| 100 | + $sqsClient = $this->getTestClient('SQS', ['region' => 'eu-west-1']); |
| 101 | + /** @var S3Client $s3Client */ |
| 102 | + $s3Client = $this->getTestClient('S3', ['region' => 'us-east-1']); |
| 103 | + $this->addMockResults($s3Client, [[]]); |
| 104 | + /** @var EventBridgeClient $eventBridgeClient */ |
| 105 | + $eventBridgeClient = $this->getTestClient('EventBridge', ['region' => 'ap-southeast-2']); |
| 106 | + $this->addMockResults($eventBridgeClient, [[]]); |
| 107 | + |
| 108 | + $spanProcessor = new CollectingSpanProcessor(); |
| 109 | + $this->awsSdkInstrumentation->instrumentClients([$sqsClient, $s3Client, $eventBridgeClient]); |
| 110 | + $this->awsSdkInstrumentation->setPropagator(new Propagator()); |
| 111 | + $this->awsSdkInstrumentation->setTracerProvider(new TracerProvider([$spanProcessor])); |
| 112 | + $this->awsSdkInstrumentation->init(); |
| 113 | + $this->awsSdkInstrumentation->activate(); |
| 114 | + |
| 115 | + $eventBridgeClient->putEvents([ |
| 116 | + 'Entries' => [ |
| 117 | + [ |
| 118 | + 'Version' => 1, |
| 119 | + 'EventBusName' => 'foo', |
| 120 | + 'Source' => 'bar', |
| 121 | + 'DetailType' => 'type', |
| 122 | + 'Detail' => '{}', |
| 123 | + ], |
| 124 | + ], |
| 125 | + ]); |
| 126 | + $s3Client->listBuckets(); |
| 127 | + |
| 128 | + $collectedSpans = $spanProcessor->getCollectedSpans(); |
| 129 | + $this->assertCount(2, $collectedSpans); |
| 130 | + |
| 131 | + /** @var ReadWriteSpanInterface $span */ |
| 132 | + $span = array_pop($collectedSpans); |
| 133 | + $this->assertTrue($span->hasEnded()); |
| 134 | + $attributes = $span->toSpanData()->getAttributes()->toArray(); |
| 135 | + $this->assertArrayHasKey('rpc.service', $attributes); |
| 136 | + $this->assertSame('s3', $attributes['rpc.service']); |
| 137 | + $this->assertArrayHasKey('aws.region', $attributes); |
| 138 | + $this->assertSame('us-east-1', $attributes['aws.region']); |
| 139 | + |
| 140 | + /** @var ReadWriteSpanInterface $span */ |
| 141 | + $span = array_pop($collectedSpans); |
| 142 | + $this->assertTrue($span->hasEnded()); |
| 143 | + $attributes = $span->toSpanData()->getAttributes()->toArray(); |
| 144 | + $this->assertArrayHasKey('rpc.service', $attributes); |
| 145 | + $this->assertSame('eventbridge', $attributes['rpc.service']); |
| 146 | + $this->assertArrayHasKey('aws.region', $attributes); |
| 147 | + $this->assertSame('ap-southeast-2', $attributes['aws.region']); |
| 148 | + } |
| 149 | + |
| 150 | + public function testSpansFromDifferentClientsAreNotOverwritingOneAnother() |
| 151 | + { |
| 152 | + try { |
| 153 | + /** @var SqsClient $sqsClient */ |
| 154 | + $sqsClient = $this->getTestClient('SQS', ['region' => 'eu-west-1']); |
| 155 | + $this->addMockResults($sqsClient, [[]]); |
| 156 | + /** @var S3Client $s3Client */ |
| 157 | + $s3Client = $this->getTestClient('S3', ['region' => 'us-east-1']); |
| 158 | + $this->addMockResults($s3Client, [[]]); |
| 159 | + |
| 160 | + $spanProcessor = new CollectingSpanProcessor(); |
| 161 | + $this->awsSdkInstrumentation->instrumentClients([$sqsClient, $s3Client]); |
| 162 | + $this->awsSdkInstrumentation->setPropagator(new Propagator()); |
| 163 | + $this->awsSdkInstrumentation->setTracerProvider(new TracerProvider([$spanProcessor])); |
| 164 | + $this->awsSdkInstrumentation->init(); |
| 165 | + $this->awsSdkInstrumentation->activate(); |
| 166 | + |
| 167 | + $sqsClient->listQueuesAsync(); |
| 168 | + $s3Client->listBucketsAsync(); |
| 169 | + |
| 170 | + $collectedSpans = $spanProcessor->getCollectedSpans(); |
| 171 | + $this->assertCount(2, $collectedSpans); |
| 172 | + |
| 173 | + /** @var ReadWriteSpanInterface $span */ |
| 174 | + $span = array_shift($collectedSpans); |
| 175 | + $attributes = $span->toSpanData()->getAttributes()->toArray(); |
| 176 | + $this->assertArrayHasKey('rpc.service', $attributes); |
| 177 | + $this->assertSame('sqs', $attributes['rpc.service']); |
| 178 | + |
| 179 | + /** @var ReadWriteSpanInterface $span */ |
| 180 | + $span = array_shift($collectedSpans); |
| 181 | + $attributes = $span->toSpanData()->getAttributes()->toArray(); |
| 182 | + $this->assertArrayHasKey('rpc.service', $attributes); |
| 183 | + $this->assertSame('s3', $attributes['rpc.service']); |
| 184 | + } catch (\Throwable $throwable) { |
| 185 | + /** @phpstan-ignore-next-line */ |
| 186 | + $this->assertFalse(true, sprintf('Exception %s occurred: %s', get_class($throwable), $throwable->getMessage())); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + public function testPreventsRepeatedInstrumentationOfSameClient() |
| 191 | + { |
| 192 | + $clients = [ |
| 193 | + 'SQS' => $sqsClient = $this->getTestClient('SQS', ['region' => 'eu-west-1']), |
| 194 | + 'S3' => $s3Client = $this->getTestClient('S3', ['region' => 'us-east-1']), |
| 195 | + 'EventBridge' => $eventBridgeClient = $this->getTestClient('EventBridge', ['region' => 'ap-southeast-2']), |
| 196 | + ]; |
| 197 | + |
| 198 | + $preInstrumentationHandlersCount = array_map(static fn (AwsClientInterface $client) => $client->getHandlerList()->count(), $clients); |
| 199 | + |
| 200 | + $this->awsSdkInstrumentation->instrumentClients([$sqsClient, $eventBridgeClient]); |
| 201 | + $this->awsSdkInstrumentation->init(); |
| 202 | + $this->awsSdkInstrumentation->activate(); |
| 203 | + |
| 204 | + $this->awsSdkInstrumentation->instrumentClients([$s3Client, $eventBridgeClient]); |
| 205 | + $this->awsSdkInstrumentation->init(); |
| 206 | + $this->awsSdkInstrumentation->activate(); |
| 207 | + |
| 208 | + foreach ($clients as $name => $client) { |
| 209 | + $this->assertSame( |
| 210 | + $preInstrumentationHandlersCount[$name] + self::HANDLERS_PER_ACTIVATION, |
| 211 | + $client->getHandlerList()->count(), |
| 212 | + sprintf('Failed asserting that %s client was instrumented once', $name) |
| 213 | + ); |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments