|  | 
|  | 1 | +<?php | 
|  | 2 | + | 
|  | 3 | +declare(strict_types=1); | 
|  | 4 | + | 
|  | 5 | +namespace OpenTelemetry\Tests\Aws\Integration; | 
|  | 6 | + | 
|  | 7 | +use OpenTelemetry\Aws\AwsSdkInstrumentation; | 
|  | 8 | +use OpenTelemetry\Aws\Xray\Propagator; | 
|  | 9 | +use OpenTelemetry\SDK\Trace\ReadWriteSpanInterface; | 
|  | 10 | +use OpenTelemetry\SDK\Trace\TracerProvider; | 
|  | 11 | +use PHPUnit\Framework\TestCase; | 
|  | 12 | + | 
|  | 13 | +class AwsSdkInstrumentationTest extends TestCase | 
|  | 14 | +{ | 
|  | 15 | +    use UsesServiceTrait; | 
|  | 16 | + | 
|  | 17 | +    private AwsSdkInstrumentation $awsSdkInstrumentation; | 
|  | 18 | + | 
|  | 19 | +    protected function setUp(): void | 
|  | 20 | +    { | 
|  | 21 | +        $this->awsSdkInstrumentation = new AwsSdkInstrumentation(); | 
|  | 22 | +    } | 
|  | 23 | + | 
|  | 24 | +    public function testProperClientNameAndRegionIsPassedToSpanForSingleClientCall() | 
|  | 25 | +    { | 
|  | 26 | +        $sqsClient = $this->getTestClient('SQS', ['region' => 'eu-west-1']); | 
|  | 27 | +        $s3Client = $this->getTestClient('S3', ['region' => 'us-east-1']); | 
|  | 28 | +        $this->addMockResults($s3Client, [[]]); | 
|  | 29 | +        $eventBridgeClient = $this->getTestClient('EventBridge', ['region' => 'ap-southeast-2']); | 
|  | 30 | + | 
|  | 31 | +        $spanProcessor = new CollectingSpanProcessor(); | 
|  | 32 | +        $this->awsSdkInstrumentation->instrumentClients([$sqsClient, $s3Client, $eventBridgeClient]); | 
|  | 33 | +        $this->awsSdkInstrumentation->setPropagator(new Propagator()); | 
|  | 34 | +        $this->awsSdkInstrumentation->setTracerProvider(new TracerProvider([$spanProcessor])); | 
|  | 35 | +        $this->awsSdkInstrumentation->init(); | 
|  | 36 | +        $this->awsSdkInstrumentation->activate(); | 
|  | 37 | + | 
|  | 38 | +        $s3Client->listBuckets(); | 
|  | 39 | + | 
|  | 40 | +        $collectedSpans = $spanProcessor->getCollectedSpans(); | 
|  | 41 | +        $this->assertCount(1, $collectedSpans); | 
|  | 42 | + | 
|  | 43 | +        /** @var ReadWriteSpanInterface $span */ | 
|  | 44 | +        $span = reset($collectedSpans); | 
|  | 45 | +        $this->assertTrue($span->hasEnded()); | 
|  | 46 | + | 
|  | 47 | +        $attributes = $span->toSpanData()->getAttributes()->toArray(); | 
|  | 48 | +        $this->assertArrayHasKey('rpc.service', $attributes); | 
|  | 49 | +        $this->assertSame('s3', $attributes['rpc.service']); | 
|  | 50 | +        $this->assertArrayHasKey('aws.region', $attributes); | 
|  | 51 | +        $this->assertSame('us-east-1', $attributes['aws.region']); | 
|  | 52 | +    } | 
|  | 53 | + | 
|  | 54 | +    public function testProperClientNameAndRegionIsPassedToSpanForDoubleCallToSameClient() | 
|  | 55 | +    { | 
|  | 56 | +        $sqsClient = $this->getTestClient('SQS', ['region' => 'eu-west-1']); | 
|  | 57 | +        $s3Client = $this->getTestClient('S3', ['region' => 'us-east-1']); | 
|  | 58 | +        $this->addMockResults($s3Client, [[], []]); | 
|  | 59 | +        $eventBridgeClient = $this->getTestClient('EventBridge', ['region' => 'ap-southeast-2']); | 
|  | 60 | + | 
|  | 61 | +        $spanProcessor = new CollectingSpanProcessor(); | 
|  | 62 | +        $this->awsSdkInstrumentation->instrumentClients([$sqsClient, $s3Client, $eventBridgeClient]); | 
|  | 63 | +        $this->awsSdkInstrumentation->setPropagator(new Propagator()); | 
|  | 64 | +        $this->awsSdkInstrumentation->setTracerProvider(new TracerProvider([$spanProcessor])); | 
|  | 65 | +        $this->awsSdkInstrumentation->init(); | 
|  | 66 | +        $this->awsSdkInstrumentation->activate(); | 
|  | 67 | + | 
|  | 68 | +        $s3Client->listBuckets(); | 
|  | 69 | +        $s3Client->listObjects(['Bucket' => 'foo']); | 
|  | 70 | + | 
|  | 71 | +        $collectedSpans = $spanProcessor->getCollectedSpans(); | 
|  | 72 | +        $this->assertCount(2, $collectedSpans); | 
|  | 73 | + | 
|  | 74 | +        /** @var ReadWriteSpanInterface $span */ | 
|  | 75 | +        foreach ($collectedSpans as $span) { | 
|  | 76 | +            $this->assertTrue($span->hasEnded()); | 
|  | 77 | +            $attributes = $span->toSpanData()->getAttributes()->toArray(); | 
|  | 78 | +            $this->assertArrayHasKey('rpc.service', $attributes); | 
|  | 79 | +            $this->assertSame('s3', $attributes['rpc.service']); | 
|  | 80 | +            $this->assertArrayHasKey('aws.region', $attributes); | 
|  | 81 | +            $this->assertSame('us-east-1', $attributes['aws.region']); | 
|  | 82 | +        } | 
|  | 83 | +    } | 
|  | 84 | + | 
|  | 85 | +    public function testProperClientNameAndRegionIsPassedToSpanForDoubleCallToDifferentClients() | 
|  | 86 | +    { | 
|  | 87 | +        $sqsClient = $this->getTestClient('SQS', ['region' => 'eu-west-1']); | 
|  | 88 | +        $s3Client = $this->getTestClient('S3', ['region' => 'us-east-1']); | 
|  | 89 | +        $this->addMockResults($s3Client, [[]]); | 
|  | 90 | +        $eventBridgeClient = $this->getTestClient('EventBridge', ['region' => 'ap-southeast-2']); | 
|  | 91 | +        $this->addMockResults($eventBridgeClient, [[]]); | 
|  | 92 | + | 
|  | 93 | +        $spanProcessor = new CollectingSpanProcessor(); | 
|  | 94 | +        $this->awsSdkInstrumentation->instrumentClients([$sqsClient, $s3Client, $eventBridgeClient]); | 
|  | 95 | +        $this->awsSdkInstrumentation->setPropagator(new Propagator()); | 
|  | 96 | +        $this->awsSdkInstrumentation->setTracerProvider(new TracerProvider([$spanProcessor])); | 
|  | 97 | +        $this->awsSdkInstrumentation->init(); | 
|  | 98 | +        $this->awsSdkInstrumentation->activate(); | 
|  | 99 | + | 
|  | 100 | +        $eventBridgeClient->putEvents([ | 
|  | 101 | +            'Entries' => [ | 
|  | 102 | +                [ | 
|  | 103 | +                    'Version' => 1, | 
|  | 104 | +                    'EventBusName' => 'foo', | 
|  | 105 | +                    'Source' => 'bar', | 
|  | 106 | +                    'DetailType' => 'type', | 
|  | 107 | +                    'Detail' => '{}' | 
|  | 108 | +                ] | 
|  | 109 | +            ] | 
|  | 110 | +        ]); | 
|  | 111 | +        $s3Client->listBuckets(); | 
|  | 112 | + | 
|  | 113 | +        $collectedSpans = $spanProcessor->getCollectedSpans(); | 
|  | 114 | +        $this->assertCount(2, $collectedSpans); | 
|  | 115 | + | 
|  | 116 | +        /** @var ReadWriteSpanInterface $span */ | 
|  | 117 | +        $span = array_pop($collectedSpans); | 
|  | 118 | +        $this->assertTrue($span->hasEnded()); | 
|  | 119 | +        $attributes = $span->toSpanData()->getAttributes()->toArray(); | 
|  | 120 | +        $this->assertArrayHasKey('rpc.service', $attributes); | 
|  | 121 | +        $this->assertSame('s3', $attributes['rpc.service']); | 
|  | 122 | +        $this->assertArrayHasKey('aws.region', $attributes); | 
|  | 123 | +        $this->assertSame('us-east-1', $attributes['aws.region']); | 
|  | 124 | + | 
|  | 125 | +        /** @var ReadWriteSpanInterface $span */ | 
|  | 126 | +        $span = array_pop($collectedSpans); | 
|  | 127 | +        $this->assertTrue($span->hasEnded()); | 
|  | 128 | +        $attributes = $span->toSpanData()->getAttributes()->toArray(); | 
|  | 129 | +        $this->assertArrayHasKey('rpc.service', $attributes); | 
|  | 130 | +        $this->assertSame('eventbridge', $attributes['rpc.service']); | 
|  | 131 | +        $this->assertArrayHasKey('aws.region', $attributes); | 
|  | 132 | +        $this->assertSame('ap-southeast-2', $attributes['aws.region']); | 
|  | 133 | +    } | 
|  | 134 | +} | 
0 commit comments