Skip to content

Commit

Permalink
feat: add support for injecting into an array (#23)
Browse files Browse the repository at this point in the history
contributors: @eddycharly
  • Loading branch information
eddycharly authored Mar 13, 2020
1 parent 7e2bc45 commit 10417e6
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ If you do not use Guzzle, you need to inject the trace headers into every outgoi

on the request and use the resulting request with your favorite request client.

If you are using a request that is not PSR-7 compatible, you can inject the headers directly into an array using

```php
Auxmoney\OpentracingBundle\Service\Tracing::injectTracingHeadersIntoCarrier(array $carrier): array
```

passing the array representing the headers of your request.

### Automatic tracing

Out of the box, the bundle will trace some spans automatically:
Expand Down
10 changes: 10 additions & 0 deletions Service/Tracing.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@

namespace Auxmoney\OpentracingBundle\Service;

use OpenTracing\Exceptions\UnsupportedFormat;
use Psr\Http\Message\RequestInterface;

interface Tracing
{
/**
* Injects necessary tracing headers into an array.
* @param array<mixed> $carrier
* @return array<mixed>
*
* @throws UnsupportedFormat when the format is not recognized by the tracer
*/
public function injectTracingHeadersIntoCarrier(array $carrier): array;

/**
* Injects necessary tracing headers into a RequestInterface.
*
Expand Down
40 changes: 33 additions & 7 deletions Service/TracingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Auxmoney\OpentracingBundle\Internal\Opentracing;
use Psr\Http\Message\RequestInterface;
use Psr\Log\LoggerInterface;
use OpenTracing\Exceptions\UnsupportedFormat;
use const OpenTracing\Formats\TEXT_MAP;
use OpenTracing\Span;

final class TracingService implements Tracing
{
Expand All @@ -20,22 +22,33 @@ public function __construct(Opentracing $opentracing, LoggerInterface $logger)
$this->logger = $logger;
}

public function injectTracingHeadersIntoCarrier(array $carrier): array
{
$span = $this->tracer->getActiveSpan();

if (!$span) {
$this->logger->warning(self::class . ': could not inject tracing headers, missing active span');
return $carrier;
}

return $this->doInjectTracingHeadersIntoCarrier($span, $carrier);
}

public function injectTracingHeaders(RequestInterface $request): RequestInterface
{
if (!$this->tracer->getActiveSpan()) {
$span = $this->tracer->getActiveSpan();

if (!$span) {
$this->logger->warning(self::class . ': could not inject tracing headers, missing active span');
return $request;
}

$headers = [];
$this->tracer->inject(
$this->tracer->getActiveSpan()->getContext(),
TEXT_MAP,
$headers
);
$headers = $this->doInjectTracingHeadersIntoCarrier($span, []);

foreach ($headers as $headerKey => $headerValue) {
$request = $request->withHeader($headerKey, $headerValue);
}

return $request;
}

Expand Down Expand Up @@ -78,4 +91,17 @@ public function finishActiveSpan(): void

$this->tracer->getScopeManager()->getActive()->close();
}

/**
* Injects necessary tracing headers into an array.
* @param array<mixed> $carrier
* @return array<mixed>
*
* @throws UnsupportedFormat when the format is not recognized by the tracer
*/
private function doInjectTracingHeadersIntoCarrier(Span $span, array $carrier): array
{
$this->tracer->inject($span->getContext(), TEXT_MAP, $carrier);
return $carrier;
}
}
2 changes: 1 addition & 1 deletion Tests/Mock/MockTracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function startSpan($operationName, $options = [])
*/
public function inject(SpanContext $spanContext, $format, &$carrier)
{
$carrier = ['made_up_header' => '1:2:3:4'];
$carrier['made_up_header'] = '1:2:3:4';
}

public function extract($format, $carrier)
Expand Down
26 changes: 26 additions & 0 deletions Tests/Service/TracingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ public function testInjectTracingHeadersSuccess(): void
self::assertSame($newRequest->reveal(), $injectedRequest);
}

public function testInjectTracingHeadersIntoCarrierSuccess(): void
{
$this->mockTracer->startActiveSpan('test span');

$headers = [ 'abc' => '123' ];

$this->logger->warning(Argument::type('string'))->shouldNotBeCalled();

$newHeaders = $this->subject->injectTracingHeadersIntoCarrier($headers);
self::assertSame([ 'abc' => '123' ], $headers);
self::assertSame([ 'abc' => '123', 'made_up_header' => '1:2:3:4' ], $newHeaders);
}

public function testInjectTracingHeadersNoActiveSpan(): void
{
$originalRequest = $this->prophesize(RequestInterface::class);
Expand All @@ -56,6 +69,19 @@ public function testInjectTracingHeadersNoActiveSpan(): void
self::assertSame($originalRequest->reveal(), $injectedRequest);
}

public function testInjectTracingHeadersIntoCarrierNoActiveSpan(): void
{
$originalRequest = $this->prophesize(RequestInterface::class);

$headers = [ 'abc' => '123' ];

$this->logger->warning(Argument::type('string'))->shouldBeCalled();

$newHeaders = $this->subject->injectTracingHeadersIntoCarrier($headers);
self::assertSame([ 'abc' => '123' ], $headers);
self::assertSame($headers, $newHeaders);
}

public function testStartActiveSpanWithoutOptionsSuccess(): void
{
$this->subject->startActiveSpan('operation name');
Expand Down

0 comments on commit 10417e6

Please sign in to comment.