From 4b638c316283cb7f849248ca444753df76625c26 Mon Sep 17 00:00:00 2001 From: Nenad Stojanovikj Date: Wed, 27 Mar 2019 08:58:48 +0100 Subject: [PATCH 1/6] Add implementation for getting and setting headers in propagator Similar to NodeJS OpenCensus implementation --- src/Trace/Propagator/ArrayHeaders.php | 59 +++++++++++++++++++++++++++ src/Trace/Propagator/HeaderGetter.php | 12 ++++++ src/Trace/Propagator/HeaderSetter.php | 12 ++++++ 3 files changed, 83 insertions(+) create mode 100644 src/Trace/Propagator/ArrayHeaders.php create mode 100644 src/Trace/Propagator/HeaderGetter.php create mode 100644 src/Trace/Propagator/HeaderSetter.php diff --git a/src/Trace/Propagator/ArrayHeaders.php b/src/Trace/Propagator/ArrayHeaders.php new file mode 100644 index 000000000..e9665b67e --- /dev/null +++ b/src/Trace/Propagator/ArrayHeaders.php @@ -0,0 +1,59 @@ +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]); + } +} diff --git a/src/Trace/Propagator/HeaderGetter.php b/src/Trace/Propagator/HeaderGetter.php new file mode 100644 index 000000000..d00a7ad79 --- /dev/null +++ b/src/Trace/Propagator/HeaderGetter.php @@ -0,0 +1,12 @@ + Date: Wed, 27 Mar 2019 09:35:09 +0100 Subject: [PATCH 2/6] Use HeaderSetter and HeaderGetter in PropagatorInterface And the implementations --- src/Trace/Propagator/B3HeadersPropagator.php | 18 ++++++------- .../Propagator/GrpcMetadataPropagator.php | 22 +++++++-------- src/Trace/Propagator/HttpHeaderPropagator.php | 27 +++++++++---------- src/Trace/Propagator/PropagatorInterface.php | 10 +++---- .../Propagator/B3HeadersPropagatorTest.php | 10 ++++--- .../Propagator/GrpcMetadataPropagatorTest.php | 6 ++--- .../Propagator/HttpHeaderPropagatorTest.php | 19 +++++++------ 7 files changed, 56 insertions(+), 56 deletions(-) diff --git a/src/Trace/Propagator/B3HeadersPropagator.php b/src/Trace/Propagator/B3HeadersPropagator.php index da5d9ad48..88a7094f4 100644 --- a/src/Trace/Propagator/B3HeadersPropagator.php +++ b/src/Trace/Propagator/B3HeadersPropagator.php @@ -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(); @@ -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); } } diff --git a/src/Trace/Propagator/GrpcMetadataPropagator.php b/src/Trace/Propagator/GrpcMetadataPropagator.php index 818a5bbcc..553141a72 100644 --- a/src/Trace/Propagator/GrpcMetadataPropagator.php +++ b/src/Trace/Propagator/GrpcMetadataPropagator.php @@ -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 @@ -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)); } } diff --git a/src/Trace/Propagator/HttpHeaderPropagator.php b/src/Trace/Propagator/HttpHeaderPropagator.php index 3712f7ecb..7b8b8e2c8 100644 --- a/src/Trace/Propagator/HttpHeaderPropagator.php +++ b/src/Trace/Propagator/HttpHeaderPropagator.php @@ -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 @@ -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); } } diff --git a/src/Trace/Propagator/PropagatorInterface.php b/src/Trace/Propagator/PropagatorInterface.php index dbd3006d5..7b4b33aea 100644 --- a/src/Trace/Propagator/PropagatorInterface.php +++ b/src/Trace/Propagator/PropagatorInterface.php @@ -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; } diff --git a/tests/unit/Trace/Propagator/B3HeadersPropagatorTest.php b/tests/unit/Trace/Propagator/B3HeadersPropagatorTest.php index aa232a9cd..4f90cb10d 100644 --- a/tests/unit/Trace/Propagator/B3HeadersPropagatorTest.php +++ b/tests/unit/Trace/Propagator/B3HeadersPropagatorTest.php @@ -2,8 +2,8 @@ namespace OpenCensus\Tests\Unit\Trace\Propagator; +use OpenCensus\Trace\Propagator\ArrayHeaders; use OpenCensus\Trace\Propagator\B3HeadersPropagator; -use OpenCensus\Trace\Propagator\HttpHeaderPropagator; use OpenCensus\Trace\SpanContext; use PHPUnit\Framework\TestCase; @@ -18,7 +18,7 @@ class B3HeadersPropagatorTest extends TestCase public function testExtract($traceId, $spanId, $enabled, $headers) { $propagator = new B3HeadersPropagator(); - $context = $propagator->extract($headers); + $context = $propagator->extract(new ArrayHeaders($headers)); $this->assertEquals($traceId, $context->traceId()); $this->assertEquals($spanId, $context->spanId()); $this->assertEquals($enabled, $context->enabled()); @@ -28,7 +28,7 @@ public function testExtract($traceId, $spanId, $enabled, $headers) public function testExtractWithoutHeaders() { $propagator = new B3HeadersPropagator(); - $context = $propagator->extract([]); + $context = $propagator->extract(new ArrayHeaders()); $this->assertNull($context->enabled()); $this->assertFalse($context->fromHeader()); } @@ -40,7 +40,9 @@ public function testInject($traceId, $spanId, $enabled, $headers) { $propagator = new B3HeadersPropagator(); $context = new SpanContext($traceId, $spanId, $enabled); - $propagator->inject($context, $output); + $headers = new ArrayHeaders(); + $propagator->inject($context, $headers); + $output = $headers->toArray(); $this->assertArrayHasKey('X-B3-TraceId', $output); $this->assertArrayHasKey('X-B3-SpanId', $output); diff --git a/tests/unit/Trace/Propagator/GrpcMetadataPropagatorTest.php b/tests/unit/Trace/Propagator/GrpcMetadataPropagatorTest.php index 2e16dc6e6..7a1f9001c 100644 --- a/tests/unit/Trace/Propagator/GrpcMetadataPropagatorTest.php +++ b/tests/unit/Trace/Propagator/GrpcMetadataPropagatorTest.php @@ -17,7 +17,7 @@ namespace OpenCensus\Tests\Unit\Trace\Propagator; -use OpenCensus\Trace\SpanContext; +use OpenCensus\Trace\Propagator\ArrayHeaders; use OpenCensus\Trace\Propagator\BinaryFormatter; use OpenCensus\Trace\Propagator\GrpcMetadataPropagator; use PHPUnit\Framework\TestCase; @@ -33,7 +33,7 @@ class GrpcMetadataPropagatorTest extends TestCase public function testExtract($traceId, $spanId, $enabled, $hex) { $propagator = new GrpcMetadataPropagator(); - $context = $propagator->extract(['grpc-trace-bin' => hex2bin($hex)]); + $context = $propagator->extract(new ArrayHeaders(['grpc-trace-bin' => hex2bin($hex)])); $this->assertEquals($traceId, $context->traceId()); $this->assertEquals($spanId, $context->spanId()); $this->assertEquals($enabled, $context->enabled()); @@ -46,7 +46,7 @@ public function testExtract($traceId, $spanId, $enabled, $hex) public function testExtractCustomKey($traceId, $spanId, $enabled, $hex) { $propagator = new GrpcMetadataPropagator(new BinaryFormatter(), 'foobar'); - $context = $propagator->extract(['foobar' => hex2bin($hex)]); + $context = $propagator->extract(new ArrayHeaders(['foobar' => hex2bin($hex)])); $this->assertEquals($traceId, $context->traceId()); $this->assertEquals($spanId, $context->spanId()); $this->assertEquals($enabled, $context->enabled()); diff --git a/tests/unit/Trace/Propagator/HttpHeaderPropagatorTest.php b/tests/unit/Trace/Propagator/HttpHeaderPropagatorTest.php index 650ece8cb..5dbbed169 100644 --- a/tests/unit/Trace/Propagator/HttpHeaderPropagatorTest.php +++ b/tests/unit/Trace/Propagator/HttpHeaderPropagatorTest.php @@ -17,6 +17,7 @@ namespace OpenCensus\Tests\Unit\Trace\Propagator; +use OpenCensus\Trace\Propagator\ArrayHeaders; use OpenCensus\Trace\SpanContext; use OpenCensus\Trace\Propagator\CloudTraceFormatter; use OpenCensus\Trace\Propagator\HttpHeaderPropagator; @@ -33,7 +34,7 @@ class HttpHeaderPropagatorTest extends TestCase public function testExtract($traceId, $spanId, $enabled, $header) { $propagator = new HttpHeaderPropagator(); - $context = $propagator->extract(['X-Cloud-Trace-Context' => $header]); + $context = $propagator->extract(new ArrayHeaders(['X-Cloud-Trace-Context' => $header])); $this->assertEquals($traceId, $context->traceId()); $this->assertEquals($spanId, $context->spanId()); $this->assertEquals($enabled, $context->enabled()); @@ -46,7 +47,7 @@ public function testExtract($traceId, $spanId, $enabled, $header) public function testExtractCustomKey($traceId, $spanId, $enabled, $header) { $propagator = new HttpHeaderPropagator(new CloudTraceFormatter(), 'Trace-Context'); - $context = $propagator->extract(['Trace-Context' => $header]); + $context = $propagator->extract(new ArrayHeaders(['Trace-Context' => $header])); $this->assertEquals($traceId, $context->traceId()); $this->assertEquals($spanId, $context->spanId()); $this->assertEquals($enabled, $context->enabled()); @@ -60,9 +61,10 @@ public function testInject($traceId, $spanId, $enabled, $header) { $propagator = new HttpHeaderPropagator(); $context = new SpanContext($traceId, $spanId, $enabled); - $propagator->inject($context, $output); - $this->assertArrayHasKey('X-Cloud-Trace-Context', $output); - $this->assertEquals($header, $output['X-Cloud-Trace-Context']); + $headers = new ArrayHeaders(); + $propagator->inject($context, $headers); + $this->assertArrayHasKey('X-Cloud-Trace-Context', $headers); + $this->assertEquals($header, $headers->get('X-Cloud-Trace-Context')); } /** @@ -72,9 +74,10 @@ public function testInjectCustomKey($traceId, $spanId, $enabled, $header) { $propagator = new HttpHeaderPropagator(new CloudTraceFormatter(), 'Trace-Context'); $context = new SpanContext($traceId, $spanId, $enabled); - $propagator->inject($context, $output); - $this->assertArrayHasKey('Trace-Context', $output); - $this->assertEquals($header, $output['Trace-Context']); + $headers = new ArrayHeaders(); + $propagator->inject($context, $headers); + $this->assertArrayHasKey('Trace-Context', $headers); + $this->assertEquals($header, $headers->get('Trace-Context')); } /** From 7ba1f2fcdc9ac2105ea83edf7d522840989e08f2 Mon Sep 17 00:00:00 2001 From: Nenad Stojanovikj Date: Wed, 27 Mar 2019 09:36:06 +0100 Subject: [PATCH 3/6] Use HeaderSetter and HeaderGetter in GuzzleMiddleware --- src/Trace/Integrations/Guzzle/Middleware.php | 3 ++- tests/unit/Trace/Integrations/Guzzle/MiddlewareTest.php | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Trace/Integrations/Guzzle/Middleware.php b/src/Trace/Integrations/Guzzle/Middleware.php index dd41b629c..154090d76 100644 --- a/src/Trace/Integrations/Guzzle/Middleware.php +++ b/src/Trace/Integrations/Guzzle/Middleware.php @@ -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; @@ -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); } diff --git a/tests/unit/Trace/Integrations/Guzzle/MiddlewareTest.php b/tests/unit/Trace/Integrations/Guzzle/MiddlewareTest.php index 693ef906e..9a5982d64 100644 --- a/tests/unit/Trace/Integrations/Guzzle/MiddlewareTest.php +++ b/tests/unit/Trace/Integrations/Guzzle/MiddlewareTest.php @@ -22,6 +22,7 @@ use OpenCensus\Trace\Exporter\ExporterInterface; use OpenCensus\Trace\Integrations\Guzzle\Middleware; use Prophecy\Argument; +use Prophecy\Prophecy\ObjectProphecy; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use PHPUnit\Framework\TestCase; @@ -31,6 +32,9 @@ */ class MiddlewareTest extends TestCase { + /** + * @var ExporterInterface|ObjectProphecy + */ private $exporter; public function setUp() From 90f478db4fb223b96d3883de493cc19ea3a05be5 Mon Sep 17 00:00:00 2001 From: Nenad Stojanovikj Date: Wed, 27 Mar 2019 09:36:20 +0100 Subject: [PATCH 4/6] Use ArrayHeaders in EventSubscriber --- src/Trace/Integrations/Guzzle/EventSubscriber.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Trace/Integrations/Guzzle/EventSubscriber.php b/src/Trace/Integrations/Guzzle/EventSubscriber.php index b85ad9681..1e701abbe 100644 --- a/src/Trace/Integrations/Guzzle/EventSubscriber.php +++ b/src/Trace/Integrations/Guzzle/EventSubscriber.php @@ -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; @@ -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([ From 707710b90f2485e42ec560bc3b9f6a6e9ab913af Mon Sep 17 00:00:00 2001 From: Nenad Stojanovikj Date: Wed, 27 Mar 2019 09:38:40 +0100 Subject: [PATCH 5/6] Use ArrayHeaders in RequestHandler --- src/Trace/RequestHandler.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Trace/RequestHandler.php b/src/Trace/RequestHandler.php index 40c5583cf..1a969949a 100644 --- a/src/Trace/RequestHandler.php +++ b/src/Trace/RequestHandler.php @@ -20,6 +20,7 @@ use OpenCensus\Core\Context; use OpenCensus\Core\Scope; use OpenCensus\Trace\Exporter\ExporterInterface; +use OpenCensus\Trace\Propagator\ArrayHeaders; use OpenCensus\Trace\Sampler\SamplerInterface; use OpenCensus\Trace\Span; use OpenCensus\Trace\Tracer\ContextTracer; @@ -65,7 +66,7 @@ class RequestHandler private $scope; /** - * @var array Replacement $_SERVER variables + * @var ArrayHeaders Keeps the provided headers and has a fallback to $_SERVER if none were given. */ private $headers; @@ -91,9 +92,7 @@ public function __construct( array $options = [] ) { $this->exporter = $exporter; - $this->headers = array_key_exists('headers', $options) - ? $options['headers'] - : $_SERVER; + $this->headers = new ArrayHeaders($options['headers'] ?? $_SERVER); $spanContext = $propagator->extract($this->headers); @@ -113,8 +112,8 @@ public function __construct( : new NullTracer(); $spanOptions = $options + [ - 'startTime' => $this->startTimeFromHeaders($this->headers), - 'name' => $this->nameFromHeaders($this->headers), + 'startTime' => $this->startTimeFromHeaders($this->headers->toArray()), + 'name' => $this->nameFromHeaders($this->headers->toArray()), 'attributes' => [], 'kind' => Span::KIND_SERVER, 'sameProcessAsParentSpan' => false @@ -134,7 +133,7 @@ public function __construct( */ public function onExit() { - $this->addCommonRequestAttributes($this->headers); + $this->addCommonRequestAttributes($this->headers->toArray()); $this->scope->close(); From 1911445609e6098010364e60b54a852ef54424d1 Mon Sep 17 00:00:00 2001 From: Nenad Stojanovikj Date: Wed, 27 Mar 2019 09:45:06 +0100 Subject: [PATCH 6/6] Add types in Scope, RequestHandler and Tracer --- src/Core/Scope.php | 2 +- src/Trace/RequestHandler.php | 36 +++++++++++------------ src/Trace/Tracer.php | 56 ++++++++++++++---------------------- 3 files changed, 39 insertions(+), 55 deletions(-) diff --git a/src/Core/Scope.php b/src/Core/Scope.php index a1c0c2b86..9eb244f9a 100644 --- a/src/Core/Scope.php +++ b/src/Core/Scope.php @@ -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); } diff --git a/src/Trace/RequestHandler.php b/src/Trace/RequestHandler.php index 1a969949a..19ff3dd77 100644 --- a/src/Trace/RequestHandler.php +++ b/src/Trace/RequestHandler.php @@ -17,12 +17,10 @@ namespace OpenCensus\Trace; -use OpenCensus\Core\Context; use OpenCensus\Core\Scope; use OpenCensus\Trace\Exporter\ExporterInterface; use OpenCensus\Trace\Propagator\ArrayHeaders; use OpenCensus\Trace\Sampler\SamplerInterface; -use OpenCensus\Trace\Span; use OpenCensus\Trace\Tracer\ContextTracer; use OpenCensus\Trace\Tracer\ExtensionTracer; use OpenCensus\Trace\Tracer\NullTracer; @@ -36,8 +34,8 @@ */ class RequestHandler { - const DEFAULT_ROOT_SPAN_NAME = 'main'; - const ATTRIBUTE_MAP = [ + private const DEFAULT_ROOT_SPAN_NAME = 'main'; + private const ATTRIBUTE_MAP = [ Span::ATTRIBUTE_HOST => ['HTTP_HOST', 'SERVER_NAME'], Span::ATTRIBUTE_PORT => ['SERVER_PORT'], Span::ATTRIBUTE_METHOD => ['REQUEST_METHOD'], @@ -131,7 +129,7 @@ public function __construct( * reports using the provided ExporterInterface. Adds additional attributes to * the root span detected from the response. */ - public function onExit() + public function onExit(): void { $this->addCommonRequestAttributes($this->headers->toArray()); @@ -145,7 +143,7 @@ public function onExit() * * @return TracerInterface */ - public function tracer() + public function tracer(): TracerInterface { return $this->tracer; } @@ -159,6 +157,7 @@ public function tracer() * @param array $spanOptions Options for the span. See * OpenCensus\Trace\Span::__construct() * @param callable $callable The callable to instrument. + * @param array $arguments * @return mixed Returns whatever the callable returns */ public function inSpan(array $spanOptions, callable $callable, array $arguments = []) @@ -174,7 +173,7 @@ public function inSpan(array $spanOptions, callable $callable, array $arguments * OpenCensus\Trace\Span::__construct() * @return Span */ - public function startSpan(array $spanOptions = []) + public function startSpan(array $spanOptions = []): Span { return $this->tracer->startSpan($spanOptions); } @@ -186,7 +185,7 @@ public function startSpan(array $spanOptions = []) * @param Span $span * @return Scope */ - public function withSpan(Span $span) + public function withSpan(Span $span): Scope { return $this->tracer->withSpan($span); } @@ -200,7 +199,7 @@ public function withSpan(Span $span) * * @type Span $span The span to add the attribute to. */ - public function addAttribute($attribute, $value, $options = []) + public function addAttribute($attribute, $value, $options = []): void { $this->tracer->addAttribute($attribute, $value, $options); } @@ -215,7 +214,7 @@ public function addAttribute($attribute, $value, $options = []) * @type array $attributes Attributes for this annotation. * @type \DateTimeInterface|int|float $time The time of this event. */ - public function addAnnotation($description, $options = []) + public function addAnnotation($description, $options = []): void { $this->tracer->addAnnotation($description, $options); } @@ -233,7 +232,7 @@ public function addAnnotation($description, $options = []) * @type array $attributes Attributes for this annotation. * @type \DateTimeInterface|int|float $time The time of this event. */ - public function addLink($traceId, $spanId, $options = []) + public function addLink($traceId, $spanId, $options = []): void { $this->tracer->addLink($traceId, $spanId, $options); } @@ -253,12 +252,12 @@ public function addLink($traceId, $spanId, $options = []) * uncompressed. * @type \DateTimeInterface|int|float $time The time of this event. */ - public function addMessageEvent($type, $id, $options) + public function addMessageEvent($type, $id, $options): void { $this->tracer->addMessageEvent($type, $id, $options); } - public function addCommonRequestAttributes(array $headers) + private function addCommonRequestAttributes(array $headers): void { if ($responseCode = http_response_code()) { $this->rootSpan->setStatus($responseCode, "HTTP status code: $responseCode"); @@ -275,7 +274,7 @@ public function addCommonRequestAttributes(array $headers) } } - private function startTimeFromHeaders(array $headers) + private function startTimeFromHeaders(array $headers): ?string { if (array_key_exists('REQUEST_TIME_FLOAT', $headers)) { return $headers['REQUEST_TIME_FLOAT']; @@ -286,15 +285,12 @@ private function startTimeFromHeaders(array $headers) return null; } - private function nameFromHeaders(array $headers) + private function nameFromHeaders(array $headers): string { - if (array_key_exists('REQUEST_URI', $headers)) { - return $headers['REQUEST_URI']; - } - return self::DEFAULT_ROOT_SPAN_NAME; + return $headers['REQUEST_URI'] ?? self::DEFAULT_ROOT_SPAN_NAME; } - private function detectKey(array $keys, array $array) + private function detectKey(array $keys, array $array): ?string { foreach ($keys as $key) { if (array_key_exists($key, $array)) { diff --git a/src/Trace/Tracer.php b/src/Trace/Tracer.php index 2361b2bb4..0e1d1b5cb 100644 --- a/src/Trace/Tracer.php +++ b/src/Trace/Tracer.php @@ -18,7 +18,6 @@ namespace OpenCensus\Trace; use OpenCensus\Core\Scope; -use OpenCensus\Trace\Span; use OpenCensus\Trace\Sampler\AlwaysSampleSampler; use OpenCensus\Trace\Sampler\SamplerInterface; use OpenCensus\Trace\Exporter\ExporterInterface; @@ -117,7 +116,7 @@ class Tracer * @type array $headers Optional array of headers to use in place of $_SERVER * @return RequestHandler */ - public static function start(ExporterInterface $reporter, array $options = []) + public static function start(ExporterInterface $reporter, array $options = []): RequestHandler { $sampler = array_key_exists('sampler', $options) ? $options['sampler'] @@ -187,12 +186,9 @@ public static function inSpan(array $spanOptions, callable $callable, array $arg * OpenCensus\Trace\Span::__construct() * @return Span */ - public static function startSpan(array $spanOptions = []) + public static function startSpan(array $spanOptions = []): Span { - if (!isset(self::$instance)) { - return new Span(); - } - return self::$instance->startSpan($spanOptions); + return isset(self::$instance) ? self::$instance->startSpan($spanOptions) : new Span(); } /** @@ -213,13 +209,12 @@ public static function startSpan(array $spanOptions = []) * @param Span $span * @return Scope */ - public static function withSpan(Span $span) + public static function withSpan(Span $span): Scope { - if (!isset(self::$instance)) { - return new Scope(function () { - }); - } - return self::$instance->withSpan($span); + return isset(self::$instance) ? self::$instance->withSpan($span) : new Scope( + function () { + } + ); } /** @@ -231,12 +226,11 @@ public static function withSpan(Span $span) * * @type Span $span The span to add the attribute to. */ - public static function addAttribute($attribute, $value, $options = []) + public static function addAttribute($attribute, $value, $options = []): void { - if (!isset(self::$instance)) { - return; + if (isset(self::$instance)) { + self::$instance->addAttribute($attribute, $value, $options); } - return self::$instance->addAttribute($attribute, $value, $options); } /** @@ -249,12 +243,11 @@ public static function addAttribute($attribute, $value, $options = []) * @type array $attributes Attributes for this annotation. * @type \DateTimeInterface|int|float $time The time of this event. */ - public static function addAnnotation($description, $options = []) + public static function addAnnotation($description, $options = []): void { - if (!isset(self::$instance)) { - return; + if (isset(self::$instance)) { + self::$instance->addAnnotation($description, $options); } - return self::$instance->addAnnotation($description, $options); } /** @@ -270,12 +263,11 @@ public static function addAnnotation($description, $options = []) * @type array $attributes Attributes for this annotation. * @type \DateTimeInterface|int|float $time The time of this event. */ - public static function addLink($traceId, $spanId, $options = []) + public static function addLink($traceId, $spanId, $options = []): void { - if (!isset(self::$instance)) { - return; + if (isset(self::$instance)) { + self::$instance->addLink($traceId, $spanId, $options); } - return self::$instance->addLink($traceId, $spanId, $options); } /** @@ -293,12 +285,11 @@ public static function addLink($traceId, $spanId, $options = []) * uncompressed. * @type \DateTimeInterface|int|float $time The time of this event. */ - public static function addMessageEvent($type, $id, $options = []) + public static function addMessageEvent($type, $id, $options = []): void { - if (!isset(self::$instance)) { - return; + if (isset(self::$instance)) { + self::$instance->addMessageEvent($type, $id, $options); } - return self::$instance->addMessageEvent($type, $id, $options); } /** @@ -306,11 +297,8 @@ public static function addMessageEvent($type, $id, $options = []) * * @return SpanContext */ - public static function spanContext() + public static function spanContext(): SpanContext { - if (!isset(self::$instance)) { - return new SpanContext(null, null, false); - } - return self::$instance->tracer()->spanContext(); + return isset(self::$instance) ? self::$instance->tracer()->spanContext() : new SpanContext(null, null, false); } }