Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Tracing/GuzzleTracingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ public static function trace(?HubInterface $hub = null): \Closure
}

if (self::shouldAttachTracingHeaders($sdkOptions, $request)) {
$traceParent = getTraceparent();
$traceParent = getTraceparent($hub);
if ($traceParent !== '') {
$request = $request->withHeader('sentry-trace', $traceParent);
}

$baggage = getBaggage();
$baggage = getBaggage($hub);
if ($baggage !== '') {
$request = $request->withHeader('baggage', $baggage);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Tracing/PropagationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sentry\Tracing;

use Sentry\SentrySdk;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Sentry\Tracing\Traits\TraceHeaderParserTrait;

Expand Down Expand Up @@ -91,10 +92,10 @@ public function toW3CTraceparent(): string
/**
* Returns a string that can be used for the `baggage` header & meta tag.
*/
public function toBaggage(): string
public function toBaggage(?HubInterface $hub = null): string
{
if ($this->dynamicSamplingContext === null) {
$hub = SentrySdk::getCurrentHub();
$hub = $hub ?? SentrySdk::getCurrentHub();
$client = $hub->getClient();

if ($client !== null) {
Expand Down
16 changes: 8 additions & 8 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,16 +383,16 @@ function getOtlpTracesEndpointUrl(): ?string
* This function is context aware, as in it either returns the traceparent based
* on the current span, or the scope's propagation context.
*/
function getTraceparent(): string
function getTraceparent(?HubInterface $hub = null): string
{
$hub = SentrySdk::getCurrentHub();
$hub = $hub ?? SentrySdk::getCurrentHub();
$client = $hub->getClient();

if ($client !== null) {
$options = $client->getOptions();

if ($options->isTracingEnabled()) {
$span = SentrySdk::getCurrentHub()->getSpan();
$span = $hub->getSpan();
if ($span !== null) {
return $span->toTraceparent();
}
Expand Down Expand Up @@ -430,29 +430,29 @@ function getW3CTraceparent(): string
* This function is context aware, as in it either returns the baggage based
* on the current span or the scope's propagation context.
*/
function getBaggage(): string
function getBaggage(?HubInterface $hub = null): string
{
$hub = SentrySdk::getCurrentHub();
$hub = $hub ?? SentrySdk::getCurrentHub();
$client = $hub->getClient();

if ($client !== null) {
$options = $client->getOptions();

if ($options->isTracingEnabled()) {
$span = SentrySdk::getCurrentHub()->getSpan();
$span = $hub->getSpan();
Comment thread
cursor[bot] marked this conversation as resolved.
if ($span !== null) {
return $span->toBaggage();
}
}
}

$baggage = '';
$hub->configureScope(static function (Scope $scope) use (&$baggage) {
$hub->configureScope(static function (Scope $scope) use (&$baggage, $hub) {
if ($scope->hasExternalPropagationContext()) {
return;
}

$baggage = $scope->getPropagationContext()->toBaggage();
$baggage = $scope->getPropagationContext()->toBaggage($hub);
});

return $baggage;
Expand Down
39 changes: 39 additions & 0 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,45 @@ public function testBaggageWithTracingEnabled(): void
$this->assertSame('sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-sample_rate=1,sentry-transaction=Test,sentry-release=1.0.0,sentry-environment=development,sentry-sampled=true,sentry-sample_rand=0.25', $baggage);
}

public function testTraceHeadersUseTheGivenHub(): void
{
$currentHubClient = $this->createMock(ClientInterface::class);
$currentHubClient->method('getOptions')->willReturn(new Options(['traces_sample_rate' => 1.0]));

$currentHub = new Hub($currentHubClient);
$currentHub->setSpan($currentHub->startTransaction(new TransactionContext()));

SentrySdk::setCurrentHub($currentHub);

$client = $this->createMock(ClientInterface::class);
$client->method('getOptions')->willReturn(new Options(['traces_sample_rate' => 1.0]));

$hub = new Hub($client);
$transaction = $hub->startTransaction(new TransactionContext());
$hub->setSpan($transaction);

$this->assertSame($transaction->toTraceparent(), getTraceparent($hub));
$this->assertSame($transaction->toBaggage(), getBaggage($hub));
}

public function testBaggageUsesTheGivenHubWithoutASpan(): void
{
$currentHubClient = $this->createMock(ClientInterface::class);
$currentHubClient->method('getOptions')->willReturn(new Options(['release' => 'current-hub-release']));

SentrySdk::setCurrentHub(new Hub($currentHubClient));

$client = $this->createMock(ClientInterface::class);
$client->method('getOptions')->willReturn(new Options(['release' => 'given-hub-release']));

$hub = new Hub($client);

$baggage = getBaggage($hub);

$this->assertStringContainsString('sentry-release=given-hub-release', $baggage);
$this->assertStringNotContainsString('current-hub-release', $baggage);
}

public function testGetOtlpTracesEndpointUrlFallsBackToDsn(): void
{
$client = $this->createMock(ClientInterface::class);
Expand Down
30 changes: 30 additions & 0 deletions tests/Tracing/GuzzleTracingMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,36 @@ public function testTraceConcurrentRequests(): void
$this->assertSame($transaction, $hub->getSpan());
}

public function testTraceHeadersUseTheSpanOfTheGivenHub(): void
{
$currentHubClient = $this->createMock(ClientInterface::class);
$currentHubClient->method('getOptions')->willReturn(new Options(['traces_sample_rate' => 1]));
$currentHub = new Hub($currentHubClient);
SentrySdk::setCurrentHub($currentHub);
$currentHub->setSpan($currentHub->startTransaction(new TransactionContext()));

$client = $this->createMock(ClientInterface::class);
$client->method('getOptions')->willReturn(new Options(['traces_sample_rate' => 1]));
$hub = new Hub($client);
$transaction = $hub->startTransaction(new TransactionContext());
$hub->setSpan($transaction);

$sentRequests = [];
$function = (GuzzleTracingMiddleware::trace($hub))(static function (Request $request) use ($hub, &$sentRequests): PromiseInterface {
$sentRequests[] = [$request, $hub->getSpan()];

return new FulfilledPromise(new Response());
});

$function(new Request('GET', 'https://www.example.com'), []);

[$request, $span] = $sentRequests[0];

$this->assertNotSame($transaction, $span);
$this->assertSame($span->toTraceparent(), $request->getHeaderLine('sentry-trace'));
$this->assertSame($span->toBaggage(), $request->getHeaderLine('baggage'));
}

public function testTraceRestoresTheCurrentSpanWhenTheHandlerThrows(): void
{
$client = $this->createMock(ClientInterface::class);
Expand Down
Loading