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

Commit

Permalink
Add types in Scope, RequestHandler and Tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenad Stojanovikj committed Mar 28, 2019
1 parent 707710b commit 1911445
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 55 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
36 changes: 16 additions & 20 deletions src/Trace/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'],
Expand Down Expand Up @@ -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());

Expand All @@ -145,7 +143,7 @@ public function onExit()
*
* @return TracerInterface
*/
public function tracer()
public function tracer(): TracerInterface
{
return $this->tracer;
}
Expand All @@ -159,6 +157,7 @@ public function tracer()
* @param array $spanOptions Options for the span. See
* <a href="Span.html#method___construct">OpenCensus\Trace\Span::__construct()</a>
* @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 = [])
Expand All @@ -174,7 +173,7 @@ public function inSpan(array $spanOptions, callable $callable, array $arguments
* <a href="Span.html#method___construct">OpenCensus\Trace\Span::__construct()</a>
* @return Span
*/
public function startSpan(array $spanOptions = [])
public function startSpan(array $spanOptions = []): Span
{
return $this->tracer->startSpan($spanOptions);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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");
Expand All @@ -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'];
Expand All @@ -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)) {
Expand Down
56 changes: 22 additions & 34 deletions src/Trace/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -187,12 +186,9 @@ public static function inSpan(array $spanOptions, callable $callable, array $arg
* <a href="Span.html#method___construct">OpenCensus\Trace\Span::__construct()</a>
* @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();
}

/**
Expand All @@ -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 () {
}
);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -293,24 +285,20 @@ 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);
}

/**
* Returns the current span context.
*
* @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);
}
}

0 comments on commit 1911445

Please sign in to comment.