diff --git a/src/Tracing/TransactionContext.php b/src/Tracing/TransactionContext.php index 13083ed35..dd70441dd 100644 --- a/src/Tracing/TransactionContext.php +++ b/src/Tracing/TransactionContext.php @@ -136,6 +136,17 @@ public static function fromSentryTrace(string $header): self return $context; } + /** + * Returns a context populated with the data of the given environment variables. + * + * @param string $sentryTrace The sentry-trace value from the environment + * @param string $baggage The baggage header value from the environment + */ + public static function fromEnvironment(string $sentryTrace, string $baggage): self + { + return self::parseTraceAndBaggage($sentryTrace, $baggage); + } + /** * Returns a context populated with the data of the given headers. * @@ -143,11 +154,16 @@ public static function fromSentryTrace(string $header): self * @param string $baggageHeader The baggage header from an incoming request */ public static function fromHeaders(string $sentryTraceHeader, string $baggageHeader): self + { + return self::parseTraceAndBaggage($sentryTraceHeader, $baggageHeader); + } + + private static function parseTraceAndBaggage(string $sentryTrace, string $baggage): self { $context = new self(); $hasSentryTrace = false; - if (preg_match(self::TRACEPARENT_HEADER_REGEX, $sentryTraceHeader, $matches)) { + if (preg_match(self::TRACEPARENT_HEADER_REGEX, $sentryTrace, $matches)) { if (!empty($matches['trace_id'])) { $context->traceId = new TraceId($matches['trace_id']); $hasSentryTrace = true; @@ -164,7 +180,7 @@ public static function fromHeaders(string $sentryTraceHeader, string $baggageHea } } - $samplingContext = DynamicSamplingContext::fromHeader($baggageHeader); + $samplingContext = DynamicSamplingContext::fromHeader($baggage); if ($hasSentryTrace && !$samplingContext->hasEntries()) { // The request comes from an old SDK which does not support Dynamic Sampling. diff --git a/tests/Tracing/TransactionContextTest.php b/tests/Tracing/TransactionContextTest.php index 8fdea8a0b..1899ee360 100644 --- a/tests/Tracing/TransactionContextTest.php +++ b/tests/Tracing/TransactionContextTest.php @@ -90,7 +90,7 @@ public function fromSentryTraceDataProvider(): iterable } /** - * @dataProvider fromHeadersDataProvider + * @dataProvider tracingDataProvider */ public function testFromHeaders(string $sentryTraceHeader, string $baggageHeader, ?SpanId $expectedSpanId, ?TraceId $expectedTraceId, ?bool $expectedParentSampled, ?string $expectedDynamicSamplingContextClass, ?bool $expectedDynamicSamplingContextFrozen) { @@ -103,7 +103,21 @@ public function testFromHeaders(string $sentryTraceHeader, string $baggageHeader $this->assertSame($expectedDynamicSamplingContextFrozen, $spanContext->getMetadata()->getDynamicSamplingContext()->isFrozen()); } - public function fromHeadersDataProvider(): iterable + /** + * @dataProvider tracingDataProvider + */ + public function testFromEnvironment(string $sentryTrace, string $baggage, ?SpanId $expectedSpanId, ?TraceId $expectedTraceId, ?bool $expectedParentSampled, ?string $expectedDynamicSamplingContextClass, ?bool $expectedDynamicSamplingContextFrozen) + { + $spanContext = TransactionContext::fromEnvironment($sentryTrace, $baggage); + + $this->assertEquals($expectedSpanId, $spanContext->getParentSpanId()); + $this->assertEquals($expectedTraceId, $spanContext->getTraceId()); + $this->assertSame($expectedParentSampled, $spanContext->getParentSampled()); + $this->assertInstanceOf($expectedDynamicSamplingContextClass, $spanContext->getMetadata()->getDynamicSamplingContext()); + $this->assertSame($expectedDynamicSamplingContextFrozen, $spanContext->getMetadata()->getDynamicSamplingContext()->isFrozen()); + } + + public function tracingDataProvider(): iterable { yield [ '0',