This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
Add B3 multiple headers propagation #224
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
This file contains 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
This file contains 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,58 @@ | ||
<?php | ||
|
||
namespace OpenCensus\Trace\Propagator; | ||
|
||
use OpenCensus\Trace\SpanContext; | ||
|
||
/** | ||
* @see https://github.com/openzipkin/b3-propagation | ||
*/ | ||
class B3HeadersPropagator implements PropagatorInterface | ||
{ | ||
private const X_B3_TRACE_ID = 'X-B3-TraceId'; | ||
private const X_B3_SPAN_ID = 'X-B3-SpanId'; | ||
private const X_B3_SAMPLED = 'X-B3-Sampled'; | ||
private const X_B3_FLAGS = 'X-B3-Flags'; | ||
|
||
/** | ||
* Extract the SpanContext from some container | ||
* | ||
* @param mixed $container | ||
* @return SpanContext | ||
*/ | ||
public function extract($container) | ||
{ | ||
$traceId = $container[self::X_B3_TRACE_ID] ?? null; | ||
$spanId = $container[self::X_B3_SPAN_ID] ?? null; | ||
$sampled = $container[self::X_B3_SAMPLED] ?? null; | ||
$flags = $container[self::X_B3_FLAGS] ?? null; | ||
|
||
$enabled = null; | ||
|
||
if ($sampled !== null) { | ||
$enabled = $sampled === '1'; | ||
} | ||
|
||
if ($flags === '1') { | ||
$enabled = true; | ||
} | ||
|
||
return new SpanContext($traceId, $spanId, $enabled, true); | ||
} | ||
|
||
/** | ||
* Inject the SpanContext back into the response | ||
* | ||
* @param SpanContext $context | ||
* @param mixed $container | ||
* @return array | ||
*/ | ||
public function inject(SpanContext $context, $container) | ||
{ | ||
return [ | ||
self::X_B3_TRACE_ID => $context->traceId(), | ||
self::X_B3_SPAN_ID => $context->spanId(), | ||
self::X_B3_SAMPLED => $context->enabled() ? 1 : 0, | ||
] + $container; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -25,7 +25,7 @@ | |
*/ | ||
class HttpHeaderPropagator implements PropagatorInterface | ||
{ | ||
const DEFAULT_HEADER = 'HTTP_X_CLOUD_TRACE_CONTEXT'; | ||
const DEFAULT_HEADER = 'X-Cloud-Trace-Context'; | ||
|
||
/** | ||
* @var FormatterInterface | ||
|
@@ -44,7 +44,7 @@ class HttpHeaderPropagator implements PropagatorInterface | |
* deserialize SpanContext. **Defaults to** a new | ||
* CloudTraceFormatter. | ||
* @param string $key [optional] The header key to store/retrieve the | ||
* encoded SpanContext. **Defaults to** `HTTP_X_CLOUD_TRACE_CONTEXT` | ||
* encoded SpanContext. **Defaults to** `X-Cloud-Trace-Context` | ||
*/ | ||
public function __construct(FormatterInterface $formatter = null, $header = null) | ||
{ | ||
|
@@ -75,33 +75,11 @@ public function extract($headers) | |
*/ | ||
public function inject(SpanContext $context, $container) | ||
{ | ||
$header = $this->key(); | ||
$header = $this->header; | ||
$value = $this->formatter->serialize($context); | ||
if (!headers_sent()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i am not sure if i should keep it, it shouldn't cause any side effects IMHO There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like a backwards incompatible change if you remove this code. If someone was relying on having the headers written with this library, we'd remove that functionality. However, since the package is in |
||
header("$header: $value"); | ||
} | ||
return [ | ||
$header => $value | ||
]; | ||
} | ||
|
||
/** | ||
* Returns the current formatter | ||
* | ||
* @return FormatterInterface | ||
*/ | ||
public function formatter() | ||
{ | ||
return $this->formatter; | ||
} | ||
$container[$header] = $value; | ||
|
||
/** | ||
* Return the key used to propagate the SpanContext | ||
* | ||
* @return string | ||
*/ | ||
public function key() | ||
{ | ||
return str_replace('_', '-', preg_replace('/^HTTP_/', '', $this->header)); | ||
return $container; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,80 @@ | ||
<?php | ||
|
||
namespace OpenCensus\Tests\Unit\Trace\Propagator; | ||
|
||
use OpenCensus\Trace\Propagator\B3HeadersPropagator; | ||
use OpenCensus\Trace\Propagator\HttpHeaderPropagator; | ||
use OpenCensus\Trace\SpanContext; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @group trace | ||
*/ | ||
class B3HeadersPropagatorTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider traceMetadata | ||
*/ | ||
public function testExtract($traceId, $spanId, $enabled, $headers) | ||
{ | ||
$propagator = new B3HeadersPropagator(); | ||
$context = $propagator->extract($headers); | ||
$this->assertEquals($traceId, $context->traceId()); | ||
$this->assertEquals($spanId, $context->spanId()); | ||
$this->assertEquals($enabled, $context->enabled()); | ||
$this->assertTrue($context->fromHeader()); | ||
} | ||
|
||
/** | ||
* @dataProvider traceMetadata | ||
*/ | ||
public function testInject($traceId, $spanId, $enabled, $headers) | ||
{ | ||
$propagator = new B3HeadersPropagator(); | ||
$context = new SpanContext($traceId, $spanId, $enabled); | ||
$output = $propagator->inject($context, []); | ||
|
||
if (array_key_exists('X-B3-Flags', $headers)) { | ||
$headers['X-B3-Sampled'] = $headers['X-B3-Flags']; | ||
unset($headers['X-B3-Flags']); | ||
} | ||
|
||
$this->assertEquals($headers, $output); | ||
} | ||
|
||
public function traceMetadata() | ||
{ | ||
return [ | ||
[ | ||
'463ac35c9f6413ad48485a3953bb6124', | ||
'a2fb4a1d1a96d312', | ||
true, | ||
[ | ||
'X-B3-TraceId' => '463ac35c9f6413ad48485a3953bb6124', | ||
'X-B3-SpanId' => 'a2fb4a1d1a96d312', | ||
'X-B3-Sampled' => '1', | ||
], | ||
], | ||
[ | ||
'463ac35c9f6413ad48485a3953bb6124', | ||
'a2fb4a1d1a96d312', | ||
false, | ||
[ | ||
'X-B3-TraceId' => '463ac35c9f6413ad48485a3953bb6124', | ||
'X-B3-SpanId' => 'a2fb4a1d1a96d312', | ||
'X-B3-Sampled' => '0', | ||
], | ||
], | ||
[ | ||
'463ac35c9f6413ad48485a3953bb6124', | ||
'a2fb4a1d1a96d312', | ||
true, | ||
[ | ||
'X-B3-TraceId' => '463ac35c9f6413ad48485a3953bb6124', | ||
'X-B3-SpanId' => 'a2fb4a1d1a96d312', | ||
'X-B3-Flags' => '1', | ||
], | ||
], | ||
]; | ||
} | ||
} |
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is possible some propagators pass
true
aswell according to https://github.com/openzipkin/b3-propagation#sampling-state-1