Skip to content

Commit

Permalink
Merge pull request #81 from cawolf/extract-null-span-context
Browse files Browse the repository at this point in the history
Extract null span context
  • Loading branch information
jky-yy authored Feb 1, 2020
2 parents 7ddfbcf + 034945f commit 39b3e83
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 18 deletions.
6 changes: 4 additions & 2 deletions src/Jaeger/Jaeger.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace Jaeger;

use Jaeger\Sampler\Sampler;
use OpenTracing\Exceptions\UnsupportedFormat;
use OpenTracing\SpanContext;
use OpenTracing\Formats;
use OpenTracing\Tracer;
Expand Down Expand Up @@ -44,6 +45,7 @@ class Jaeger implements Tracer{

public $processThrift = '';

/** @var Propagator|null */
public $propagator = null;

public function __construct($serverName = '', Reporter $reporter, Sampler $sampler,
Expand Down Expand Up @@ -136,7 +138,7 @@ public function inject(SpanContext $spanContext, $format, &$carrier){
if($format == Formats\TEXT_MAP){
$this->propagator->inject($spanContext, $format, $carrier);
}else{
throw new \Exception("not support format $format");
throw UnsupportedFormat::forFormat($format);
}
}

Expand All @@ -150,7 +152,7 @@ public function extract($format, $carrier){
if($format == Formats\TEXT_MAP){
return $this->propagator->extract($format, $carrier);
}else{
throw new \Exception("not support format $format");
throw UnsupportedFormat::forFormat($format);
}
}

Expand Down
20 changes: 13 additions & 7 deletions src/Jaeger/Propagator/JaegerPropagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,23 @@ public function inject(SpanContext $spanContext, $format, &$carrier){
}


/**
* 提取
* @param string $format
* @param $carrier
*/
public function extract($format, $carrier){
$spanContext = new SpanContext(0, 0, 0, null, 0);

$spanContext = null;
$carrier = array_change_key_case($carrier, CASE_LOWER);

foreach ($carrier as $k => $v){

if(!in_array($k, [Constants\Tracer_State_Header_Name,
Constants\Jaeger_Debug_Header, Constants\Jaeger_Baggage_Header]) &&
stripos($k, Constants\Trace_Baggage_Header_Prefix) === false){
continue;
}

if($spanContext === null){
$spanContext = new SpanContext(0, 0, 0, null, 0);
}

if(is_array($v)){
$v = urldecode(current($v));
}else {
Expand Down
3 changes: 2 additions & 1 deletion src/Jaeger/Propagator/Propagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function inject(SpanContext $spanContext, $format, &$carrier);
* 提取
* @param string $format
* @param $carrier
* @return SpanContext|null
*/
public function extract($format, $carrier);

}
}
16 changes: 14 additions & 2 deletions src/Jaeger/Propagator/ZipkinPropagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ public function inject(SpanContext $spanContext, $format, &$carrier){


public function extract($format, $carrier){
$spanContext = new SpanContext(0, 0, 0, null, 0);
$spanContext = null;

foreach ($carrier as $k => $val) {
if (in_array($k, [Constants\X_B3_TRACEID,
Constants\X_B3_PARENT_SPANID, Constants\X_B3_SPANID, Constants\X_B3_SAMPLED])
) {
if($spanContext === null){
$spanContext = new SpanContext(0, 0, 0, null, 0);
}
continue;
}
}

if(isset($carrier[Constants\X_B3_TRACEID]) && $carrier[Constants\X_B3_TRACEID]){
$spanContext->traceIdToString($carrier[Constants\X_B3_TRACEID]);
}
Expand All @@ -49,4 +61,4 @@ public function extract($format, $carrier){

return $spanContext;
}
}
}
6 changes: 3 additions & 3 deletions tests/JaegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function testInjectUnSupportFormat(){
$Jaeger->setPropagator(new JaegerPropagator());

$context = new SpanContext(1, 1, 1, null, 1);
$this->expectExceptionMessage('not support format http_headers');
$this->expectExceptionMessage('The format \'http_headers\' is not supported.');

$Jaeger->inject($context, Formats\HTTP_HEADERS, $_SERVER);
}
Expand All @@ -106,7 +106,7 @@ public function testExtractUnSupportFormat(){
$Jaeger->setPropagator(new JaegerPropagator());

$_SERVER[strtoupper(Constants\Tracer_State_Header_Name)] = '1:1:1:1';
$this->expectExceptionMessage('not support format http_headers');
$this->expectExceptionMessage('The format \'http_headers\' is not supported.');

$Jaeger->extract(Formats\HTTP_HEADERS, $_SERVER);
}
Expand Down Expand Up @@ -217,4 +217,4 @@ public function testNestedSpanBaggage(){

$this->assertEquals($parent->getBaggageItem('key'), $child->getBaggageItem('key'));
}
}
}
15 changes: 13 additions & 2 deletions tests/Propagator/JaegerPropagatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testExtract(){
public function testExtractDebugId(){

$jaeger = new JaegerPropagator();
$carrier = [];
$carrier[Constants\Trace_Baggage_Header_Prefix . 'baggage'] = 2;

$context = $jaeger->extract(Formats\TEXT_MAP, $carrier);
$this->assertTrue($context->debugId == 0);
Expand All @@ -85,6 +85,7 @@ public function testExtractUberctx(){
$jaeger = new JaegerPropagator();

$carrier[Constants\Trace_Baggage_Header_Prefix] = '2.0.0';
$carrier[Constants\Jaeger_Debug_Header] = true;
$context = $jaeger->extract(Formats\TEXT_MAP, $carrier);
$this->assertTrue($context->baggage == null);

Expand Down Expand Up @@ -113,6 +114,7 @@ public function testExtractBadBaggageHeader(){
$carrier = [];

$carrier[Constants\Jaeger_Baggage_Header] = 'version';
$carrier[Constants\Jaeger_Debug_Header] = true;
$context = $jaeger->extract(Formats\TEXT_MAP, $carrier);
$this->assertTrue($context->baggage == null);
}
Expand Down Expand Up @@ -146,4 +148,13 @@ public function testExtractPsr7(){
$this->assertTrue($context->spanId == 1562237095801441413);
$this->assertTrue($context->flags == 1);
}
}


public function testExtractReturnsNull(){
$jaeger = new JaegerPropagator();
$carrier = [];

$context = $jaeger->extract(Formats\TEXT_MAP, $carrier);
$this->assertNull($context);
}
}
11 changes: 10 additions & 1 deletion tests/Propagator/ZipkinPropagatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,13 @@ public function testExtract128Bit(){
$this->assertTrue($context->spanId == 1562289663898779811);
$this->assertTrue($context->flags == 1);
}
}


public function testExtractReturnsNull(){
$jaeger = new ZipkinPropagator();
$carrier = [];

$context = $jaeger->extract(Formats\TEXT_MAP, $carrier);
$this->assertNull($context);
}
}

0 comments on commit 39b3e83

Please sign in to comment.