-
-
Notifications
You must be signed in to change notification settings - Fork 0
Reproduction for sentry-php#1999 #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cleptric
wants to merge
1
commit into
main
Choose a base branch
from
repro/sentry-php-1999
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Reproduction for sentry-php#1999 | ||
|
|
||
| **Issue:** https://github.com/getsentry/sentry-php/issues/1999 | ||
|
|
||
| ## Description | ||
|
|
||
| The SDK incorrectly uses the `sentry-sample_rate` value from the baggage header to make a sampling decision when the `sentry-trace` header is missing. Without a `sentry-trace` header, the SDK should treat this as a new trace head and use the locally configured `traces_sample_rate` instead. | ||
|
|
||
| ## Steps to Reproduce | ||
|
|
||
| 1. Install dependencies: | ||
| ```bash | ||
| composer install | ||
| ``` | ||
|
|
||
| 2. Run the reproduction: | ||
| ```bash | ||
| php reproduce.php | ||
| ``` | ||
|
|
||
| ## Expected Behavior | ||
|
|
||
| When there is no `sentry-trace` header, the SDK should make an independent sampling decision using the configured `traces_sample_rate` (0.0 in this case). The transaction should NOT be sampled. | ||
|
|
||
| ## Actual Behavior | ||
|
|
||
| The SDK extracts the sample rate from the incoming baggage header (`sentry-sample_rate=1.0`) and uses it to determine whether to sample the transaction. The transaction IS sampled despite `traces_sample_rate=0.0`. | ||
|
|
||
| ## Why This Matters | ||
|
|
||
| - The baggage sample rate reflects the sampling decision of an upstream service that may have different sampling configuration | ||
| - Without `sentry-trace`, there's no parent span to continue, so the local SDK should be the trace head | ||
| - This can lead to unexpected sampling behavior where traces are sampled at rates not configured locally | ||
|
|
||
| ## Environment | ||
|
|
||
| - PHP: 8.x | ||
| - sentry/sentry: ^4.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "require": { | ||
| "sentry/sentry": "^4.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php | ||
|
|
||
| require_once __DIR__ . '/vendor/autoload.php'; | ||
|
|
||
| use Sentry\SentrySdk; | ||
| use Sentry\Tracing\TransactionContext; | ||
|
|
||
| // Initialize Sentry with a low sample rate to demonstrate the bug | ||
| \Sentry\init([ | ||
| 'dsn' => getenv('SENTRY_DSN') ?: null, | ||
| 'traces_sample_rate' => 0.0, // Local config: 0% sampling - should NEVER sample | ||
| ]); | ||
|
|
||
| // Simulate incoming request with baggage header but NO sentry-trace header | ||
| // This represents a scenario where: | ||
| // - An upstream service sent baggage with sample_rate=1.0 (100% sampling) | ||
| // - But the sentry-trace header is missing (e.g., stripped by a proxy) | ||
| $_SERVER['HTTP_BAGGAGE'] = 'sentry-sample_rate=1.0,sentry-sampled=true,sentry-trace_id=12345678901234567890123456789012,sentry-public_key=abc123'; | ||
|
|
||
| // Note: HTTP_SENTRY_TRACE is intentionally NOT set | ||
|
|
||
| $hub = SentrySdk::getCurrentHub(); | ||
| $client = $hub->getClient(); | ||
|
|
||
| // Create a transaction context from the incoming headers | ||
| // Since there's no sentry-trace header, this should be treated as a new trace head | ||
| $context = TransactionContext::fromHeaders( | ||
| $_SERVER['HTTP_SENTRY_TRACE'] ?? '', | ||
| $_SERVER['HTTP_BAGGAGE'] ?? '' | ||
| ); | ||
|
|
||
| $context->setName('test-transaction'); | ||
| $context->setOp('http.server'); | ||
|
|
||
| $transaction = $hub->startTransaction($context); | ||
|
|
||
| echo "=== Reproduction for sentry-php#1999 ===\n\n"; | ||
| echo "Configuration:\n"; | ||
| echo " traces_sample_rate: 0.0 (0% - should never sample)\n\n"; | ||
|
|
||
| echo "Incoming Headers:\n"; | ||
| echo " sentry-trace: (not present)\n"; | ||
| echo " baggage: sentry-sample_rate=1.0,sentry-sampled=true,...\n\n"; | ||
|
|
||
| echo "Result:\n"; | ||
| echo " Transaction sampled: " . ($transaction->getSampled() ? 'YES' : 'NO') . "\n\n"; | ||
|
|
||
| if ($transaction->getSampled()) { | ||
| echo "BUG CONFIRMED: Transaction was sampled despite traces_sample_rate=0.0\n"; | ||
| echo "The SDK incorrectly used the baggage sample_rate (1.0) instead of the local config.\n"; | ||
| } else { | ||
| echo "Expected behavior: Transaction was not sampled (respecting local config).\n"; | ||
| } | ||
|
|
||
| $transaction->finish(); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.