Skip to content

Commit

Permalink
Move and refactor clock components (#633)
Browse files Browse the repository at this point in the history
* Move Clock classes to SDK\Common\Time namespace

* Move conversion methods to Util class

* Implement StopWatch

* Implement ClockFactory

* Fix StopwatchInterface

* Finalize ClockFactory

* Implement StopWatchFactory

* Deprecate AbstractClock

* Add BC alias

* Add initial start time to StopWatch

* Unify and optimize SystemClock methods

* Use ClockFactory in Span

* Use ClockFactory in BatchSpanProcessor

* Use ClockInterface in BatchSpanProcessorTest

* Use ClockFactory in SpanData

* Use ClockFactory in examples

* Add millisToNanos Util method

* Use StopWatch in BatchSpanProcessor

* Make Phan happy

* Make Psalm happy

* Make PHPUnit happy

* Remove sllh/composer-versions-check from allowed composer plugins

* Use correct assertion

* Use correct assertion

* Assert difference
  • Loading branch information
tidal authored Mar 25, 2022
1 parent 2b72c92 commit 8e1e912
Show file tree
Hide file tree
Showing 40 changed files with 902 additions and 183 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@
"Jaeger\\Thrift\\": "thrift/jaeger/"
},
"files": [
"src/Context/fiber/initialize_fiber_handler.php"
"src/Context/fiber/initialize_fiber_handler.php",
"src/SDK/Common/Time/ClockInterface.php",
"src/SDK/Common/Time/AbstractClock.php",
"src/SDK/Common/Time/SystemClock.php"
]
},
"autoload-dev": {
Expand Down
1 change: 0 additions & 1 deletion docs/laravel-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ $kernel->terminate($request, $response);
```php
use OpenTelemetry\Contrib\Jaeger\Exporter as JaegerExporter;
use OpenTelemetry\Contrib\Zipkin\Exporter as ZipkinExporter;
use OpenTelemetry\SDK\AbstractClock;
use OpenTelemetry\Context\Context;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\SamplingResult;
Expand Down
1 change: 0 additions & 1 deletion docs/symfony-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ To use open-telemetry specific classes we have to import them at the top of our
use App\Kernel;
use OpenTelemetry\Contrib\Jaeger\Exporter as JaegerExporter;
use OpenTelemetry\Contrib\Zipkin\Exporter as ZipkinExporter;
use OpenTelemetry\SDK\AbstractClock;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\SamplingResult;
use OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessor;
Expand Down
4 changes: 2 additions & 2 deletions examples/AlwaysOffTraceExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

use OpenTelemetry\API\Trace as API;
use OpenTelemetry\Context\Context;
use OpenTelemetry\SDK\AbstractClock;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOffSampler;
use OpenTelemetry\SDK\Trace\SamplingResult;
use OpenTelemetry\SDK\Trace\TracerProvider;
Expand All @@ -28,7 +28,7 @@
$span->setAttribute('remote_ip', '1.2.3.4');
$span->setAttribute('country', 'USA');

$timestamp = AbstractClock::getDefault()->timestamp();
$timestamp = ClockFactory::getDefault()->timestamp();
$span->addEvent('found_login', new Attributes([
'id' => 12345,
'username' => 'otuser',
Expand Down
4 changes: 2 additions & 2 deletions examples/AlwaysOnNewrelicExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use OpenTelemetry\API\Trace as API;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Contrib\Newrelic\Exporter as NewrelicExporter;
use OpenTelemetry\SDK\AbstractClock;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\SamplingResult;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
Expand Down Expand Up @@ -67,7 +67,7 @@

for ($i = 0; $i < 5; $i++) {
// start a span, register some events
$timestamp = AbstractClock::getDefault()->timestamp();
$timestamp = ClockFactory::getDefault()->now();
$span = $tracer->startAndActivateSpan('session.generate.span.' . microtime(true));

$spanParent = $span->getParentContext();
Expand Down
4 changes: 2 additions & 2 deletions examples/AlwaysOnOTLPGrpcExample1.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use OpenTelemetry\API\Trace as API;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Contrib\OtlpGrpc\Exporter as OTLPExporter;
use OpenTelemetry\SDK\AbstractClock;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\SamplingResult;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
Expand All @@ -31,7 +31,7 @@

for ($i = 0; $i < 5; $i++) {
// start a span, register some events
$timestamp = AbstractClock::getDefault()->timestamp();
$timestamp = ClockFactory::getDefault()->now();
$span = $tracer->startAndActivateSpan('session.generate.span' . microtime(true));
//startAndActivateSpan('session.generate.span.' . microtime(true));

Expand Down
4 changes: 2 additions & 2 deletions examples/AlwaysOnOTLPGrpcExample2.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use OpenTelemetry\API\Trace as API;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Contrib\OtlpGrpc\Exporter as OTLPExporter;
use OpenTelemetry\SDK\AbstractClock;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\SamplingResult;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
Expand Down Expand Up @@ -35,7 +35,7 @@

$rootSpan->setAttribute('remote_ip', '1.2.3.4')
->setAttribute('country', 'USA');
$timestamp = AbstractClock::getDefault()->timestamp();
$timestamp = ClockFactory::getDefault()->now();
$rootSpan->addEvent('found_login', new Attributes([
'id' => 1,
'username' => 'otuser',
Expand Down
4 changes: 2 additions & 2 deletions examples/AlwaysOnZipkinToNewrelicExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use OpenTelemetry\API\Trace as API;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Contrib\ZipkinToNewrelic\Exporter as ZipkinToNewrelicExporter;
use OpenTelemetry\SDK\AbstractClock;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\SamplingResult;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
Expand Down Expand Up @@ -67,7 +67,7 @@

for ($i = 0; $i < 5; $i++) {
// start a span, register some events
$timestamp = AbstractClock::getDefault()->timestamp();
$timestamp = ClockFactory::getDefault()->now();
$span = $tracer->startAndActivateSpan('session.generate.span.' . microtime(true));

$spanParent = $span->getParentContext();
Expand Down
2 changes: 1 addition & 1 deletion examples/BatchExporting.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
$tracerProvider = new TracerProvider(
new BatchSpanProcessor(
new ConsoleSpanExporter(),
\OpenTelemetry\SDK\SystemClock::getDefault(),
\OpenTelemetry\SDK\Common\Time\SystemClock::getDefault(),
2048, //max spans to queue before sending to exporter
$delayMillis, //batch delay milliseconds
)
Expand Down
3 changes: 1 addition & 2 deletions examples/LoggingOfSpanData.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use OpenTelemetry\Contrib\Jaeger\Exporter as JaegerExporter;
use OpenTelemetry\SDK\AbstractClock;
use OpenTelemetry\SDK\Logs\SimplePsrFileLogger;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\SpanExporter\LoggerDecorator;
Expand Down Expand Up @@ -59,7 +58,7 @@
* Create the Tracer
*/
$tracerProvider = new TracerProvider(
new BatchSpanProcessor($decorator, AbstractClock::getDefault()),
new BatchSpanProcessor($decorator),
new AlwaysOnSampler()
);
$tracer = $tracerProvider->getTracer();
Expand Down
8 changes: 4 additions & 4 deletions src/Contrib/Jaeger/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Jaeger\Thrift\TagType;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\SDK\AbstractClock;
use OpenTelemetry\SDK\Common\Time\Util as TimeUtil;
use OpenTelemetry\SDK\Trace\EventInterface;
use OpenTelemetry\SDK\Trace\LinkInterface;
use OpenTelemetry\SDK\Trace\SpanConverterInterface;
Expand Down Expand Up @@ -69,8 +69,8 @@ private function convertSpan(SpanDataInterface $span): JTSpan
'parentSpanId' => $parentSpanId,
] = self::convertOtelToJaegerIds($span);

$startTime = AbstractClock::nanosToMicro($span->getStartEpochNanos());
$duration = AbstractClock::nanosToMicro($span->getEndEpochNanos() - $span->getStartEpochNanos());
$startTime = TimeUtil::nanosToMicros($span->getStartEpochNanos());
$duration = TimeUtil::nanosToMicros($span->getEndEpochNanos() - $span->getStartEpochNanos());

$tags = self::convertOtelSpanDataToJaegerTags($span);

Expand Down Expand Up @@ -292,7 +292,7 @@ function ($event) {

private static function convertSingleOtelEventToJaegerLog(EventInterface $event): Log
{
$timestamp = AbstractClock::nanosToMicro($event->getEpochNanos());
$timestamp = TimeUtil::nanosToMicros($event->getEpochNanos());

$eventValue = $event->getAttributes()->get(self::EVENT_ATTRIBUTE_KEY_NAMED_EVENT) ?? $event->getName();
$attributes = $event->getAttributes()->toArray();
Expand Down
6 changes: 3 additions & 3 deletions src/Contrib/Newrelic/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OpenTelemetry\Contrib\Newrelic;

use OpenTelemetry\SDK\AbstractClock;
use OpenTelemetry\SDK\Common\Time\Util as TimeUtil;
use OpenTelemetry\SDK\Trace\SpanConverterInterface;
use OpenTelemetry\SDK\Trace\SpanDataInterface;

Expand Down Expand Up @@ -37,8 +37,8 @@ private function convertSpan(SpanDataInterface $span): array
{
$spanParent = $span->getParentContext();

$startTimestamp = AbstractClock::nanosToMilli($span->getStartEpochNanos());
$endTimestamp = AbstractClock::nanosToMilli($span->getEndEpochNanos());
$startTimestamp = TimeUtil::nanosToMillis($span->getStartEpochNanos());
$endTimestamp = TimeUtil::nanosToMillis($span->getEndEpochNanos());

$row = [
'id' => $span->getSpanId(),
Expand Down
8 changes: 4 additions & 4 deletions src/Contrib/Zipkin/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\Contrib\Zipkin\SpanKind as ZipkinSpanKind;
use OpenTelemetry\SDK\AbstractClock;
use OpenTelemetry\SDK\Common\Time\Util as TimeUtil;
use OpenTelemetry\SDK\Trace\EventInterface;
use OpenTelemetry\SDK\Trace\SpanConverterInterface;
use OpenTelemetry\SDK\Trace\SpanDataInterface;
Expand Down Expand Up @@ -75,8 +75,8 @@ private function convertSpan(SpanDataInterface $span): array
{
$spanParent = $span->getParentContext();

$startTimestamp = AbstractClock::nanosToMicro($span->getStartEpochNanos());
$endTimestamp = AbstractClock::nanosToMicro($span->getEndEpochNanos());
$startTimestamp = TimeUtil::nanosToMicros($span->getStartEpochNanos());
$endTimestamp = TimeUtil::nanosToMicros($span->getEndEpochNanos());

$row = [
'id' => $span->getSpanId(),
Expand Down Expand Up @@ -167,7 +167,7 @@ private static function toAnnotation(EventInterface $event): array
$value = ($attributesAsJson !== null) ? sprintf('"%s": %s', $eventName, $attributesAsJson) : sprintf('"%s"', $eventName);

$annotation = [
'timestamp' => AbstractClock::nanosToMicro($event->getEpochNanos()),
'timestamp' => TimeUtil::nanosToMicros($event->getEpochNanos()),
'value' => $value,
];

Expand Down
8 changes: 4 additions & 4 deletions src/Contrib/ZipkinToNewrelic/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace OpenTelemetry\Contrib\ZipkinToNewrelic;

use function max;
use OpenTelemetry\SDK\AbstractClock;
use OpenTelemetry\SDK\Common\Time\Util as TimeUtil;
use OpenTelemetry\SDK\Trace\SpanConverterInterface;
use OpenTelemetry\SDK\Trace\SpanDataInterface;

Expand Down Expand Up @@ -56,8 +56,8 @@ private function convertSpan(SpanDataInterface $span): array
{
$spanParent = $span->getParentContext();

$startTimestamp = AbstractClock::nanosToMicro($span->getStartEpochNanos());
$endTimestamp = AbstractClock::nanosToMicro($span->getEndEpochNanos());
$startTimestamp = TimeUtil::nanosToMicros($span->getStartEpochNanos());
$endTimestamp = TimeUtil::nanosToMicros($span->getEndEpochNanos());

$row = [
'id' => $span->getSpanId(),
Expand Down Expand Up @@ -88,7 +88,7 @@ private function convertSpan(SpanDataInterface $span): array
$row['annotations'] = [];
}
$row['annotations'][] = [
'timestamp' => AbstractClock::nanosToMicro($event->getEpochNanos()),
'timestamp' => TimeUtil::nanosToMicros($event->getEpochNanos()),
'value' => $event->getName(),
];
}
Expand Down
24 changes: 0 additions & 24 deletions src/SDK/ClockInterface.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

declare(strict_types=1);

namespace OpenTelemetry\SDK;

use function intdiv;
namespace OpenTelemetry\SDK\Common\Time;

/**
* @deprecated
* Use {@see \OpenTelemetry\SDK\Common\Time\ClockFactory} and {@see \OpenTelemetry\SDK\Common\Time\Util} instead.
*/
abstract class AbstractClock implements ClockInterface
{
private static ?ClockInterface $testClock;

public static function getDefault(): ClockInterface
{
return self::$testClock ?? SystemClock::getInstance();
return self::$testClock ?? SystemClock::create();
}

/**
Expand Down Expand Up @@ -42,3 +44,9 @@ public static function secondsToNanos(int $seconds): int
return $seconds * self::NANOS_PER_SECOND;
}
}

/**
* BC class alias
* @todo: remove in future release. Also in composer.json autoload/files.
*/
class_alias(AbstractClock::class, 'OpenTelemetry\SDK\AbstractClock');
30 changes: 30 additions & 0 deletions src/SDK/Common/Time/ClockFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Time;

final class ClockFactory implements ClockFactoryInterface
{
private static ?ClockInterface $default;

public static function create(): self
{
return new self();
}

public function build(): ClockInterface
{
return new SystemClock();
}

public static function getDefault(): ClockInterface
{
return self::$default ?? self::$default = self::create()->build();
}

public static function setDefault(?ClockInterface $clock): void
{
self::$default = $clock;
}
}
16 changes: 16 additions & 0 deletions src/SDK/Common/Time/ClockFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Time;

interface ClockFactoryInterface
{
public static function create(): self;

public function build(): ClockInterface;

public static function getDefault(): ClockInterface;

public static function setDefault(?ClockInterface $clock): void;
}
30 changes: 30 additions & 0 deletions src/SDK/Common/Time/ClockInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Time;

interface ClockInterface
{
public const MILLIS_PER_SECOND = 1_000;
public const MICROS_PER_SECOND = 1_000_000;
public const NANOS_PER_SECOND = 1_000_000_000;
public const NANOS_PER_MILLISECOND = 1_000_000;
public const NANOS_PER_MICROSECOND = 1_000;

/**
* Returns the current epoch wall-clock timestamp in nanoseconds
*/
public function now(): int;

/**
* @deprecated
*/
public function nanoTime(): int;
}

/**
* BC class alias
* @todo: remove in future release. Also in composer.json autoload/files.
*/
class_alias(ClockInterface::class, 'OpenTelemetry\SDK\ClockInterface');
Loading

0 comments on commit 8e1e912

Please sign in to comment.