Skip to content

Commit 5c03130

Browse files
authored
feat: add trace id header capability (#13)
contributors: @mmross
1 parent 2fbfebb commit 5c03130

File tree

7 files changed

+108
-10
lines changed

7 files changed

+108
-10
lines changed

.codacy.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
exclude_paths:
2-
- Tests/**/*
2+
- Tests/*
3+
- Tests/**/*

Internal/ZipkinTracingId.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Auxmoney\OpentracingBundle\Internal;
6+
7+
use Auxmoney\OpentracingBundle\Service\Tracing;
8+
use Zipkin\Propagation\B3;
9+
10+
class ZipkinTracingId implements TracingId
11+
{
12+
private $tracing;
13+
14+
public function __construct(Tracing $tracing)
15+
{
16+
$this->tracing = $tracing;
17+
}
18+
19+
public function getAsString(): string
20+
{
21+
$context = $this->tracing->injectTracingHeadersIntoCarrier([]);
22+
$traceHeaderName = strtolower(B3::TRACE_ID_NAME);
23+
return $context[$traceHeaderName] ?? 'none';
24+
}
25+
}

Resources/config/services.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ services:
1414

1515
Auxmoney\OpentracingBundle\Factory\SamplerFactory:
1616
class: Auxmoney\OpentracingBundle\Factory\ZipkinSamplerFactory
17+
18+
Auxmoney\OpentracingBundle\Internal\TracingId:
19+
class: Auxmoney\OpentracingBundle\Internal\ZipkinTracingId

Tests/Functional/TestProjectFiles/default/src/Command/TestCommand.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,25 @@
44

55
namespace App\Command;
66

7-
use Auxmoney\OpentracingBundle\Internal\Opentracing;
7+
use Auxmoney\OpentracingBundle\Internal\TracingId;
88
use Symfony\Component\Console\Command\Command;
99
use Symfony\Component\Console\Input\InputInterface;
1010
use Symfony\Component\Console\Output\OutputInterface;
11-
use const OpenTracing\Formats\TEXT_MAP;
1211

1312
class TestCommand extends Command
1413
{
15-
private $opentracing;
14+
private $tracingId;
1615

17-
public function __construct(Opentracing $opentracing)
16+
public function __construct(TracingId $tracingId)
1817
{
1918
parent::__construct('test:zipkin');
2019
$this->setDescription('some fancy command description');
21-
$this->opentracing = $opentracing;
20+
$this->tracingId = $tracingId;
2221
}
2322

2423
protected function execute(InputInterface $input, OutputInterface $output)
2524
{
26-
$carrier = [];
27-
$this->opentracing->getTracerInstance()->inject($this->opentracing->getTracerInstance()->getActiveSpan()->getContext(), TEXT_MAP, $carrier);
28-
$output->writeln(current($carrier));
25+
$output->writeln($this->tracingId->getAsString());
2926
return 0;
3027
}
3128
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Auxmoney\OpentracingBundle\Tests\Internal;
6+
7+
use Auxmoney\OpentracingBundle\Internal\ZipkinTracingId;
8+
use Auxmoney\OpentracingBundle\Service\Tracing;
9+
use PHPUnit\Framework\TestCase;
10+
use Prophecy\Prophecy\ObjectProphecy;
11+
12+
class ZipkinTracingIdTest extends TestCase
13+
{
14+
/** @var Tracing|ObjectProphecy */
15+
private $tracing;
16+
/** @var ZipkinTracingId */
17+
private $subject;
18+
19+
public function setUp()
20+
{
21+
parent::setUp();
22+
$this->tracing = $this->prophesize(Tracing::class);
23+
24+
$this->subject = new ZipkinTracingId($this->tracing->reveal());
25+
}
26+
27+
public function testGetAsStringSuccess(): void
28+
{
29+
$this->tracing->injectTracingHeadersIntoCarrier([])->shouldBeCalled()->willReturn(['x-b3-traceid' => 'abc']);
30+
31+
self::assertSame('abc', $this->subject->getAsString());
32+
}
33+
34+
public function testGetAsStringNoHeader(): void
35+
{
36+
$this->tracing->injectTracingHeadersIntoCarrier([])->shouldBeCalled()->willReturn([]);
37+
38+
self::assertSame('none', $this->subject->getAsString());
39+
}
40+
}

Tests/OpentracingBundleTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Auxmoney\OpentracingBundle\Tests;
6+
7+
use Auxmoney\OpentracingBundle\DependencyInjection\PSR18CompilerPass;
8+
use Auxmoney\OpentracingBundle\OpentracingBundle;
9+
use PHPUnit\Framework\TestCase;
10+
use Prophecy\Argument;
11+
use Symfony\Component\DependencyInjection\ContainerBuilder;
12+
13+
class OpentracingBundleTest extends TestCase
14+
{
15+
/** @var OpentracingBundle */
16+
private $subject;
17+
18+
public function setUp()
19+
{
20+
parent::setUp();
21+
22+
$this->subject = new OpentracingBundle();
23+
}
24+
25+
public function testBuild(): void
26+
{
27+
$containerBuilder = $this->prophesize(ContainerBuilder::class);
28+
$containerBuilder->addCompilerPass(Argument::type(PSR18CompilerPass::class))->shouldBeCalled();
29+
30+
$this->subject->build($containerBuilder->reveal());
31+
}
32+
}

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"require": {
2020
"php": "^7.1.33",
2121
"ext-json": "*",
22-
"auxmoney/opentracing-bundle-core": "^0.6",
22+
"auxmoney/opentracing-bundle-core": "^0.6.6",
2323
"opentracing/opentracing": "1.0.0-beta5@beta",
2424
"jcchavezs/zipkin-opentracing": "^0.1"
2525
},

0 commit comments

Comments
 (0)