|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +use PHPUnit\Framework\TestCase; |
| 5 | +use OpenTelemetry\Contrib\Sampler\Xray\_AWSXRayRemoteSampler; |
| 6 | +use OpenTelemetry\Contrib\Sampler\Xray\Clock; |
| 7 | +use OpenTelemetry\Contrib\Sampler\Xray\RulesCache; |
| 8 | +use OpenTelemetry\Contrib\Sampler\Xray\AWSXRaySamplerClient; |
| 9 | +use OpenTelemetry\Contrib\Sampler\Xray\FallbackSampler; |
| 10 | +use OpenTelemetry\SDK\Resource\ResourceInfo; |
| 11 | +use OpenTelemetry\Context\ContextInterface; |
| 12 | +use OpenTelemetry\SDK\Common\Attribute\Attributes; |
| 13 | +use OpenTelemetry\SDK\Trace\SamplingResult; |
| 14 | + |
| 15 | +final class AWSXRayRemoteSamplerTest extends TestCase |
| 16 | +{ |
| 17 | + public function testShouldSampleUpdatesRulesAndTargets(): void |
| 18 | + { |
| 19 | + $resource = ResourceInfo::create(Attributes::create([])); |
| 20 | + |
| 21 | + // 1) Mock client |
| 22 | + $mockClient = $this->createMock(AWSXRaySamplerClient::class); |
| 23 | + $dummyRules = ['rule1', 'rule2']; |
| 24 | + $mockClient->expects($this->once()) |
| 25 | + ->method('getSamplingRules') |
| 26 | + ->willReturn($dummyRules); |
| 27 | + $mockClient->expects($this->once()) |
| 28 | + ->method('getSamplingTargets') |
| 29 | + ->willReturn((object)[ |
| 30 | + 'SamplingTargetDocuments' => [], |
| 31 | + 'Interval' => 5, |
| 32 | + 'LastRuleModification' => 0, |
| 33 | + ]); |
| 34 | + |
| 35 | + // 2) Mock RulesCache |
| 36 | + $mockRulesCache = $this->getMockBuilder(RulesCache::class) |
| 37 | + ->disableOriginalConstructor() |
| 38 | + ->getMock(); |
| 39 | + $mockRulesCache->expects($this->once()) |
| 40 | + ->method('updateRules') |
| 41 | + ->with($dummyRules); |
| 42 | + $mockRulesCache->expects($this->once()) |
| 43 | + ->method('getAppliers') |
| 44 | + ->willReturn([]); |
| 45 | + $mockRulesCache->expects($this->once()) |
| 46 | + ->method('updateTargets') |
| 47 | + ->with([]); |
| 48 | + $mockRulesCache->expects($this->once()) |
| 49 | + ->method('expired') |
| 50 | + ->willReturn(false); |
| 51 | + $expectedResult = new SamplingResult(SamplingResult::RECORD_AND_SAMPLE); |
| 52 | + $mockRulesCache->expects($this->once()) |
| 53 | + ->method('shouldSample') |
| 54 | + ->willReturn($expectedResult); |
| 55 | + |
| 56 | + // 3) Instantiate and inject mocks |
| 57 | + $sampler = new _AWSXRayRemoteSampler($resource, 'host', 0); |
| 58 | + $ref = new ReflectionClass($sampler); |
| 59 | + $ref->getProperty('client')->setAccessible(true); |
| 60 | + $ref->getProperty('client')->setValue($sampler, $mockClient); |
| 61 | + $ref->getProperty('rulesCache')->setAccessible(true); |
| 62 | + $ref->getProperty('rulesCache')->setValue($sampler, $mockRulesCache); |
| 63 | + $ref->getProperty('fallback')->setAccessible(true); |
| 64 | + $ref->getProperty('fallback')->setValue($sampler, $this->createMock(FallbackSampler::class)); |
| 65 | + |
| 66 | + // 4) Force fetch times into the past so updates run |
| 67 | + $now = (new Clock())->now(); |
| 68 | + $ref->getProperty('nextRulesFetchTime')->setAccessible(true); |
| 69 | + $ref->getProperty('nextRulesFetchTime')->setValue($sampler, $now->sub(new DateInterval('PT1S'))); |
| 70 | + $ref->getProperty('nextTargetFetchTime')->setAccessible(true); |
| 71 | + $ref->getProperty('nextTargetFetchTime')->setValue($sampler, $now->sub(new DateInterval('PT1S'))); |
| 72 | + |
| 73 | + // 5) Call shouldSample |
| 74 | + $result = $sampler->shouldSample( |
| 75 | + $this->createMock(ContextInterface::class), |
| 76 | + 'traceId', 'spanName', 1, |
| 77 | + Attributes::create([]), |
| 78 | + [] |
| 79 | + ); |
| 80 | + $this->assertSame($expectedResult, $result); |
| 81 | + } |
| 82 | + |
| 83 | + public function testShouldSampleFallbackWhenExpired(): void |
| 84 | + { |
| 85 | + $resource = ResourceInfo::create(Attributes::create([])); |
| 86 | + $mockClient = $this->createMock(AWSXRaySamplerClient::class); |
| 87 | + |
| 88 | + $mockRulesCache = $this->getMockBuilder(RulesCache::class) |
| 89 | + ->disableOriginalConstructor() |
| 90 | + ->getMock(); |
| 91 | + // No updateRules call since expired |
| 92 | + $mockRulesCache->expects($this->never()) |
| 93 | + ->method('updateRules'); |
| 94 | + $mockRulesCache->expects($this->once()) |
| 95 | + ->method('expired') |
| 96 | + ->willReturn(true); |
| 97 | + |
| 98 | + $fallback = $this->createMock(FallbackSampler::class); |
| 99 | + $expected = new SamplingResult(SamplingResult::DROP); |
| 100 | + $fallback->expects($this->once()) |
| 101 | + ->method('shouldSample') |
| 102 | + ->willReturn($expected); |
| 103 | + |
| 104 | + $sampler = new _AWSXRayRemoteSampler($resource, 'host', 0); |
| 105 | + $ref = new ReflectionClass($sampler); |
| 106 | + $ref->getProperty('client')->setAccessible(true); |
| 107 | + $ref->getProperty('client')->setValue($sampler, $mockClient); |
| 108 | + $ref->getProperty('rulesCache')->setAccessible(true); |
| 109 | + $ref->getProperty('rulesCache')->setValue($sampler, $mockRulesCache); |
| 110 | + $ref->getProperty('fallback')->setAccessible(true); |
| 111 | + $ref->getProperty('fallback')->setValue($sampler, $fallback); |
| 112 | + |
| 113 | + $result = $sampler->shouldSample( |
| 114 | + $this->createMock(ContextInterface::class), |
| 115 | + 't', 'n', 1, |
| 116 | + Attributes::create([]), |
| 117 | + [] |
| 118 | + ); |
| 119 | + $this->assertSame($expected, $result); |
| 120 | + } |
| 121 | +} |
0 commit comments