|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenTelemetry\Tests\Contrib\Instrumentation\AwsSdk\Integration; |
| 6 | + |
| 7 | +use ArrayObject; |
| 8 | +use Aws\MockHandler; |
| 9 | +use Aws\Result; |
| 10 | +use Aws\S3\S3Client; |
| 11 | +use OpenTelemetry\API\Instrumentation\Configurator; |
| 12 | +use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator; |
| 13 | +use OpenTelemetry\Context\ScopeInterface; |
| 14 | +use OpenTelemetry\SDK\Trace\ImmutableSpan; |
| 15 | +use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter; |
| 16 | +use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; |
| 17 | +use OpenTelemetry\SDK\Trace\TracerProvider; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * @covers \OpenTelemetry\Contrib\Instrumentation\AwsSdk\AwsSdkInstrumentation |
| 22 | + */ |
| 23 | +class AwsSdkInstrumentationTest extends TestCase |
| 24 | +{ |
| 25 | + private S3Client $client; |
| 26 | + private MockHandler $mock; |
| 27 | + private ArrayObject $spans; |
| 28 | + private ScopeInterface $scope; |
| 29 | + |
| 30 | + public function setUp(): void |
| 31 | + { |
| 32 | + $this->spans = new ArrayObject(); |
| 33 | + $tracerProvider = new TracerProvider( |
| 34 | + new SimpleSpanProcessor(new InMemoryExporter($this->spans)) |
| 35 | + ); |
| 36 | + $this->scope = Configurator::create() |
| 37 | + ->withTracerProvider($tracerProvider) |
| 38 | + ->withPropagator(TraceContextPropagator::getInstance()) |
| 39 | + ->activate(); |
| 40 | + |
| 41 | + $this->mock = new MockHandler(); |
| 42 | + $this->mock->append(new Result([ |
| 43 | + '@metadata' => [ |
| 44 | + 'statusCode' => 200, |
| 45 | + 'headers' => [ |
| 46 | + 'x-amz-request-id' => 'TEST-REQUEST-ID', |
| 47 | + ], |
| 48 | + ], |
| 49 | + ])); |
| 50 | + |
| 51 | + $this->client = new S3Client([ |
| 52 | + 'region' => 'us-west-2', |
| 53 | + 'version' => 'latest', |
| 54 | + 'handler' => $this->mock, |
| 55 | + ]); |
| 56 | + } |
| 57 | + |
| 58 | + public function tearDown(): void |
| 59 | + { |
| 60 | + $this->scope->detach(); |
| 61 | + } |
| 62 | + |
| 63 | + public function test_listBuckets_generates_one_aws_span_with_expected_attributes(): void |
| 64 | + { |
| 65 | + $this->client->listBuckets(); |
| 66 | + |
| 67 | + $this->assertCount(1, $this->spans); |
| 68 | + |
| 69 | + $span = $this->spans->offsetGet(0); |
| 70 | + |
| 71 | + $this->assertInstanceOf(ImmutableSpan::class, $span); |
| 72 | + |
| 73 | + $this->assertSame('s3.ListBuckets', $span->getName()); |
| 74 | + |
| 75 | + $attrs = $span->getAttributes(); |
| 76 | + $this->assertSame('aws-api', $attrs->get('rpc.system')); |
| 77 | + $this->assertSame('s3', $attrs->get('rpc.service')); |
| 78 | + $this->assertSame('ListBuckets', $attrs->get('rpc.method')); |
| 79 | + $this->assertSame('us-west-2', $attrs->get('aws.region')); |
| 80 | + $this->assertSame(200, $attrs->get('http.status_code')); |
| 81 | + $this->assertSame('TEST-REQUEST-ID', $attrs->get('aws.requestId')); |
| 82 | + } |
| 83 | +} |
0 commit comments