From 1911445609e6098010364e60b54a852ef54424d1 Mon Sep 17 00:00:00 2001 From: Nenad Stojanovikj Date: Wed, 27 Mar 2019 09:45:06 +0100 Subject: [PATCH] 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); } }