|
| 1 | +* Start Date: 2022-09-26 |
| 2 | +* RFC Type: feature |
| 3 | +* RFC PR: https://github.com/getsentry/rfcs/pull/14 |
| 4 | + |
| 5 | +# Summary |
| 6 | + |
| 7 | +This RFC proposes a new way to continue a trace when creating nested transactions. |
| 8 | + |
| 9 | +# Motivation |
| 10 | + |
| 11 | +The current way we propagate `sentry-trace` and `baggage`, is to pass a correctly populated `TransactionContext` as the first argument to `startTransaction()`. |
| 12 | + |
| 13 | +```php |
| 14 | +use Sentry\Tracing\TransactionContext; |
| 15 | +use function Sentry\startTransaction; |
| 16 | + |
| 17 | +$transactionContext = TransactionContext::continueFromHeaders($sentryTraceHeader, $baggageHeader); |
| 18 | +$transaction = startTransaction($transactionContext); |
| 19 | + |
| 20 | +``` |
| 21 | + |
| 22 | +In case someone starts another nested transaction without passing in any context, a new trace will be started and the Dynamic Sampling Context is lost as well. |
| 23 | + |
| 24 | +# Options Considered |
| 25 | + |
| 26 | +## Add TransactionContext::fromParent() |
| 27 | + |
| 28 | +```php |
| 29 | +use Sentry\Tracing\TransactionContext; |
| 30 | +use function Sentry\startTransaction; |
| 31 | + |
| 32 | +$transactionContext = TransactionContext::fromParent($transaction); |
| 33 | +$transaction = startTransaction($transactionContext); |
| 34 | + |
| 35 | +public static function fromParent(Transaction $transaction) |
| 36 | +{ |
| 37 | + $context = new self(); |
| 38 | + $context->traceId = $transaction->getTraceId(); |
| 39 | + $context->parentSpanId = $transaction->getParentSpanId(); |
| 40 | + $context->sampled = $transaction->getSampled(); |
| 41 | + $context->getMetadata()->setBaggage($transaction->getBaggage()); |
| 42 | + |
| 43 | + return $context; |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +## Add a third argument to `startTransaction()` |
| 48 | + |
| 49 | +```php |
| 50 | +use Sentry\Tracing\TransactionContext; |
| 51 | +use function Sentry\startTransaction; |
| 52 | + |
| 53 | +$transactionContext = new TransactionContext(); |
| 54 | +$transaction = startTransaction($transactionContext, [], bool $continueTrace = true); |
| 55 | +``` |
| 56 | + |
| 57 | +This would require making the SDKs aware of the current request. |
| 58 | +In PHP, we could rely on `$_SERVER['HTTP_SENTRY_TRACE]` and `$_SERVER['HTTP_BAGGAGE]`, but this is not possible in all languages. |
| 59 | + |
| 60 | +# Unresolved questions |
| 61 | + |
| 62 | +* Can we rely on `SentrySdk::getCurrentHub()->getTransaction()` to fetch the current transaction to be passed into `TransactionContext::fromParent()` ? |
| 63 | +* How would we make `TransactionContext::__construct()` aware of the current request? |
0 commit comments