|
5 | 5 | namespace Sentry\Tests\Tracing; |
6 | 6 |
|
7 | 7 | use PHPUnit\Framework\TestCase; |
| 8 | +use Psr\Log\LoggerInterface; |
| 9 | +use Sentry\ClientInterface; |
| 10 | +use Sentry\Options; |
| 11 | +use Sentry\SentrySdk; |
| 12 | +use Sentry\State\Hub; |
8 | 13 | use Sentry\Tracing\DynamicSamplingContext; |
9 | 14 | use Sentry\Tracing\SpanId; |
10 | 15 | use Sentry\Tracing\TraceId; |
@@ -135,6 +140,67 @@ public static function tracingDataProvider(): iterable |
135 | 140 | ]; |
136 | 141 | } |
137 | 142 |
|
| 143 | + /** |
| 144 | + * @dataProvider invalidSampleRandDataProvider |
| 145 | + */ |
| 146 | + public function testInvalidSampleRandIsIgnored(string $sampleRand): void |
| 147 | + { |
| 148 | + $context = TransactionContext::fromHeaders( |
| 149 | + '566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-1', |
| 150 | + 'sentry-sample_rate=0.4,sentry-sample_rand=' . rawurlencode($sampleRand) |
| 151 | + ); |
| 152 | + |
| 153 | + $generatedSampleRand = $context->getMetadata()->getSampleRand(); |
| 154 | + |
| 155 | + $this->assertNotNull($generatedSampleRand); |
| 156 | + $this->assertGreaterThanOrEqual(0.0, $generatedSampleRand); |
| 157 | + $this->assertLessThan(0.4, $generatedSampleRand); |
| 158 | + } |
| 159 | + |
| 160 | + public function testSampleRandIsIgnoredWithoutSentryTraceHeader(): void |
| 161 | + { |
| 162 | + $context = TransactionContext::fromHeaders('', 'sentry-sample_rand=-1.0'); |
| 163 | + $sampleRand = $context->getMetadata()->getSampleRand(); |
| 164 | + |
| 165 | + $this->assertNotNull($sampleRand); |
| 166 | + $this->assertGreaterThanOrEqual(0.0, $sampleRand); |
| 167 | + $this->assertLessThanOrEqual(1.0, $sampleRand); |
| 168 | + } |
| 169 | + |
| 170 | + public function testInvalidSampleRandIsLogged(): void |
| 171 | + { |
| 172 | + $logger = $this->createMock(LoggerInterface::class); |
| 173 | + $logger->expects($this->once()) |
| 174 | + ->method('debug') |
| 175 | + ->with( |
| 176 | + $this->stringContains('Ignoring invalid sentry-sample_rand baggage value'), |
| 177 | + ['sample_rand' => '-1.0'] |
| 178 | + ); |
| 179 | + |
| 180 | + $client = $this->createMock(ClientInterface::class); |
| 181 | + $client->method('getOptions') |
| 182 | + ->willReturn(new Options(['logger' => $logger])); |
| 183 | + |
| 184 | + SentrySdk::setCurrentHub(new Hub($client)); |
| 185 | + |
| 186 | + try { |
| 187 | + TransactionContext::fromHeaders( |
| 188 | + '566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8', |
| 189 | + 'sentry-sample_rand=-1.0' |
| 190 | + ); |
| 191 | + } finally { |
| 192 | + SentrySdk::setCurrentHub(new Hub()); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + public static function invalidSampleRandDataProvider(): iterable |
| 197 | + { |
| 198 | + yield ['-1.0']; |
| 199 | + yield ['1']; |
| 200 | + yield ['2.0']; |
| 201 | + yield ['foo']; |
| 202 | + } |
| 203 | + |
138 | 204 | public function testSampleRandRangeWhenParentNotSampledAndSampleRateProvided(): void |
139 | 205 | { |
140 | 206 | $context = TransactionContext::fromHeaders( |
|
0 commit comments