Skip to content
This repository has been archived by the owner on May 25, 2021. It is now read-only.

Commit

Permalink
Merge pull request #11 from nenadstojanovikj/minor/update-propagator-…
Browse files Browse the repository at this point in the history
…interface

Update PropagatorInterface
  • Loading branch information
Nenad Stojanovikj authored Mar 28, 2019
2 parents 4f76b19 + 1911445 commit 4cea3aa
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 120 deletions.
2 changes: 1 addition & 1 deletion src/Core/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct(callable $callback, $args = [])
/**
* Close and clean up the scope. Runs the initial callback provided.
*/
public function close()
public function close(): void
{
call_user_func_array($this->callback, $this->args);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Trace/Integrations/Guzzle/EventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace OpenCensus\Trace\Integrations\Guzzle;

use OpenCensus\Core\Scope;
use OpenCensus\Trace\Propagator\ArrayHeaders;
use OpenCensus\Trace\Span;
use OpenCensus\Trace\Tracer;
use OpenCensus\Trace\Propagator\HttpHeaderPropagator;
Expand Down Expand Up @@ -88,8 +89,9 @@ public function onBefore(BeforeEvent $event)
$request = $event->getRequest();
$context = Tracer::spanContext();
if ($context->enabled()) {
$headers = new ArrayHeaders();
$this->propagator->inject($context, $headers);
$request->setHeaders($headers);
$request->setHeaders($headers->toArray());
}

$span = Tracer::startSpan([
Expand Down
3 changes: 2 additions & 1 deletion src/Trace/Integrations/Guzzle/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace OpenCensus\Trace\Integrations\Guzzle;

use OpenCensus\Trace\Propagator\ArrayHeaders;
use OpenCensus\Trace\Span;
use OpenCensus\Trace\Tracer;
use OpenCensus\Trace\Propagator\HttpHeaderPropagator;
Expand Down Expand Up @@ -70,8 +71,8 @@ public function __invoke(callable $handler)
return function (RequestInterface $request, $options) use ($handler) {
$context = Tracer::spanContext();
if ($context->enabled()) {
$headers = new ArrayHeaders();
$this->propagator->inject($context, $headers) ;

foreach ($headers as $headerName => $headerValue) {
$request = $request->withHeader($headerName, $headerValue);
}
Expand Down
59 changes: 59 additions & 0 deletions src/Trace/Propagator/ArrayHeaders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php declare(strict_types=1);

namespace OpenCensus\Trace\Propagator;

class ArrayHeaders implements HeaderSetter, HeaderGetter, \IteratorAggregate, \ArrayAccess
{
/**
* @var string[]
*/
private $headers;

/**
* @param string[] $headers An associative array with header name as key
*/
public function __construct(array $headers = [])
{
$this->headers = $headers;
}

public function get(string $header): ?string
{
return $this->headers[$header] ?? null;
}

public function set(string $header, string $value): void
{
$this->headers[$header] = $value;
}

public function toArray(): array
{
return $this->headers;
}

public function getIterator()
{
return new \ArrayIterator($this->headers);
}

public function offsetExists($offset)
{
return isset($this->headers[$offset]);
}

public function offsetGet($offset)
{
return $this->get($offset);
}

public function offsetSet($offset, $value)
{
$this->set($offset, $value);
}

public function offsetUnset($offset)
{
unset($this->headers[$offset]);
}
}
18 changes: 9 additions & 9 deletions src/Trace/Propagator/B3HeadersPropagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class B3HeadersPropagator implements PropagatorInterface
private const X_B3_SAMPLED = 'X-B3-Sampled';
private const X_B3_FLAGS = 'X-B3-Flags';

public function extract($container): SpanContext
public function extract(HeaderGetter $headers): SpanContext
{
$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;
$traceId = $headers->get(self::X_B3_TRACE_ID);
$spanId = $headers->get(self::X_B3_SPAN_ID);
$sampled = $headers->get(self::X_B3_SAMPLED);
$flags = $headers->get(self::X_B3_FLAGS);

if (!$traceId || !$spanId) {
return new SpanContext();
Expand All @@ -38,10 +38,10 @@ public function extract($container): SpanContext
return new SpanContext($traceId, $spanId, $enabled, true);
}

public function inject(SpanContext $context, &$container): void
public function inject(SpanContext $context, HeaderSetter $headers): void
{
$container[self::X_B3_TRACE_ID] = $context->traceId();
$container[self::X_B3_SPAN_ID] = $context->spanId();
$container[self::X_B3_SAMPLED] = $context->enabled() ? 1 : 0;
$headers->set(self::X_B3_TRACE_ID, $context->traceId());
$headers->set(self::X_B3_SPAN_ID, $context->spanId());
$headers->set(self::X_B3_SAMPLED, $context->enabled() ? 1 : 0);
}
}
22 changes: 10 additions & 12 deletions src/Trace/Propagator/GrpcMetadataPropagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
class GrpcMetadataPropagator implements PropagatorInterface
{
const DEFAULT_METADATA_KEY = 'grpc-trace-bin';
private const DEFAULT_METADATA_KEY = 'grpc-trace-bin';

/**
* @var FormatterInterface
Expand All @@ -43,25 +43,23 @@ class GrpcMetadataPropagator implements PropagatorInterface
*
* @param FormatterInterface $formatter [optional] The formatter used to serialize/deserialize SpanContext
* **Defaults to** a new BinaryFormatter.
* @param string $key [optional] The grpc metadata key to store/retrieve the encoded SpanContext.
* **Defaults to** `grpc-trace-bin`
* @param string $key The grpc metadata key to store/retrieve the encoded SpanContext.
*/
public function __construct(FormatterInterface $formatter = null, $key = null)
public function __construct(FormatterInterface $formatter = null, string $key = self::DEFAULT_METADATA_KEY)
{
$this->formatter = $formatter ?: new BinaryFormatter();
$this->key = $key ?: self::DEFAULT_METADATA_KEY;
$this->key = $key;
}

public function extract($metadata): SpanContext
public function extract(HeaderGetter $metadata): SpanContext
{
if (array_key_exists($this->key, $metadata)) {
return $this->formatter->deserialize($metadata[$this->key]);
}
return new SpanContext();
$data = $metadata->get($this->key);

return $data ? $this->formatter->deserialize($data) : new SpanContext();
}

public function inject(SpanContext $context, &$metadata): void
public function inject(SpanContext $context, HeaderSetter $metadata): void
{
$metadata[$this->key] = $this->formatter->serialize($context);
$metadata->set($this->key, $this->formatter->serialize($context));
}
}
12 changes: 12 additions & 0 deletions src/Trace/Propagator/HeaderGetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace OpenCensus\Trace\Propagator;

interface HeaderGetter
{
/**
* @param string $header Header name
* @return string|null
*/
public function get(string $header): ?string;
}
12 changes: 12 additions & 0 deletions src/Trace/Propagator/HeaderSetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace OpenCensus\Trace\Propagator;

interface HeaderSetter
{
/**
* @param string $header Header name
* @param string $value Header value
*/
public function set(string $header, string $value): void;
}
27 changes: 12 additions & 15 deletions src/Trace/Propagator/HttpHeaderPropagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
class HttpHeaderPropagator implements PropagatorInterface
{
const DEFAULT_HEADER = 'X-Cloud-Trace-Context';
private const DEFAULT_HEADER = 'X-Cloud-Trace-Context';

/**
* @var FormatterInterface
Expand All @@ -41,32 +41,29 @@ class HttpHeaderPropagator implements PropagatorInterface
* Create a new HttpHeaderPropagator
*
* @param FormatterInterface $formatter The formatter used to serialize and
* deserialize SpanContext. **Defaults to** a new
* CloudTraceFormatter.
* @param string|null $header
* deserialize SpanContext. **Defaults to** a new CloudTraceFormatter.
* @param string $header
*/
public function __construct(FormatterInterface $formatter = null, $header = null)
public function __construct(FormatterInterface $formatter = null, string $header = self::DEFAULT_HEADER)
{
$this->formatter = $formatter ?: new CloudTraceFormatter();
$this->header = $header ?: self::DEFAULT_HEADER;
$this->header = $header;
}

public function extract($headers): SpanContext
public function extract(HeaderGetter $headers): SpanContext
{
if (array_key_exists($this->header, $headers)) {
return $this->formatter->deserialize($headers[$this->header]);
}
return new SpanContext();
$data = $headers->get($this->header);

return $data ? $this->formatter->deserialize($data) : new SpanContext();
}

public function inject(SpanContext $context, &$container): void
public function inject(SpanContext $context, HeaderSetter $setter): void
{
$header = $this->header;
$value = $this->formatter->serialize($context);

if (!headers_sent()) {
header("$header: $value");
header("$this->header: $value");
}
$container[$header] = $value;
$setter->set($this->header, $value);
}
}
10 changes: 5 additions & 5 deletions src/Trace/Propagator/PropagatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ interface PropagatorInterface
/**
* Extract the SpanContext from some container
*
* @param mixed $container
* @param HeaderGetter $getter
* @return SpanContext
*/
public function extract($container): SpanContext;
public function extract(HeaderGetter $getter): SpanContext;

/**
* Inject the SpanContext back into the response
* Inject the SpanContext back into the container
*
* @param SpanContext $context
* @param mixed $container An object in which the SpanContext data will be injected.
* @param HeaderSetter $setter
*/
public function inject(SpanContext $context, &$container): void;
public function inject(SpanContext $context, HeaderSetter $setter): void;
}
Loading

0 comments on commit 4cea3aa

Please sign in to comment.