migrate Clock classes to API (#1281)

* migrate Clock classes to API
- move Clock* + Util into API
- add a deprecated wrapper for ClockFactory and Util
- remove unused StopWatch* classes

* review feedback

* replace ClockFactory with Clock
It's not really a factory, and really only provides access to a system clock. Create a
Clock class which is more clear in its purpose.

* remove time Util class
per review feedback, it's only used in Zipkin. Removed some unused consts from ClockInterface

* Apply suggestions from code review

Co-authored-by: Tobias Bachert <git@b-privat.de>

---------

Co-authored-by: Tobias Bachert <git@b-privat.de>
This commit is contained in:
Brett McBride 2024-04-29 10:28:06 +10:00 committed by GitHub
parent 9fe6aa0cd7
commit fc161a8548
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
59 changed files with 212 additions and 735 deletions

View File

@ -4,11 +4,11 @@ declare(strict_types=1);
namespace OpenTelemetry\Example;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\Contrib\Otlp\MetricExporter;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Export\Stream\StreamTransportFactory;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilter\WithSampledTraceExemplarFilter;
use OpenTelemetry\SDK\Metrics\MeterProvider;
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;
@ -29,7 +29,7 @@ $tracer = (new TracerProvider())->getTracer('io.opentelemetry.contrib.php');
echo get_class($tracer) . PHP_EOL;
//metrics
$clock = ClockFactory::getDefault();
$clock = Clock::getDefault();
$reader = new ExportingReader(new MetricExporter((new StreamTransportFactory())->create(STDOUT, 'application/x-ndjson')));
$views = new CriteriaViewRegistry();
$meterProvider = new MeterProvider(

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace OpenTelemetry\Example;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Logs\EventLogger;
use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\API\Signals;
@ -12,7 +13,6 @@ use OpenTelemetry\Contrib\Otlp\LogsExporter;
use OpenTelemetry\Contrib\Otlp\OtlpUtil;
use Opentelemetry\Proto\Logs\V1\SeverityNumber;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Logs\LoggerProvider;
use OpenTelemetry\SDK\Logs\LogRecordLimitsBuilder;
use OpenTelemetry\SDK\Logs\Processor\BatchLogRecordProcessor;
@ -24,7 +24,7 @@ $exporter = new LogsExporter($transport);
$loggerProvider = new LoggerProvider(
new BatchLogRecordProcessor(
$exporter,
ClockFactory::getDefault()
Clock::getDefault()
),
new InstrumentationScopeFactory(
(new LogRecordLimitsBuilder())->build()->getAttributeFactory()

View File

@ -4,11 +4,11 @@ declare(strict_types=1);
namespace OpenTelemetry\Example;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Logs\EventLogger;
use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Logs\Exporter\ConsoleExporterFactory;
use OpenTelemetry\SDK\Logs\LoggerProvider;
use OpenTelemetry\SDK\Logs\Processor\BatchLogRecordProcessor;
@ -18,7 +18,7 @@ require __DIR__ . '/../../../vendor/autoload.php';
$loggerProvider = new LoggerProvider(
new BatchLogRecordProcessor(
(new ConsoleExporterFactory())->create(),
ClockFactory::getDefault()
Clock::getDefault()
),
new InstrumentationScopeFactory(Attributes::factory())
);

View File

@ -7,11 +7,11 @@ namespace OpenTelemetry\Example;
require_once __DIR__ . '/../../vendor/autoload.php';
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\Contrib\Otlp\MetricExporter;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Export\Stream\StreamTransportFactory;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Metrics\Aggregation\ExplicitBucketHistogramAggregation;
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilter\WithSampledTraceExemplarFilter;
use OpenTelemetry\SDK\Metrics\MeterProvider;
@ -22,7 +22,7 @@ use OpenTelemetry\SDK\Metrics\View\SelectionCriteria\InstrumentNameCriteria;
use OpenTelemetry\SDK\Metrics\View\ViewTemplate;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
$clock = ClockFactory::getDefault();
$clock = Clock::getDefault();
$reader = new ExportingReader(new MetricExporter((new StreamTransportFactory())->create(STDOUT, 'application/x-ndjson'), /*Temporality::CUMULATIVE*/));
// Let's imagine we export the metrics as Histogram, and to simplify the story we will only have one histogram bucket (-Inf, +Inf):

View File

@ -6,14 +6,14 @@ namespace OpenTelemetry\Example;
require __DIR__ . '/../../../vendor/autoload.php';
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Signals;
use OpenTelemetry\Contrib\Grpc\GrpcTransportFactory;
use OpenTelemetry\Contrib\Otlp\MetricExporter;
use OpenTelemetry\Contrib\Otlp\OtlpUtil;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;
$clock = ClockFactory::getDefault();
$clock = Clock::getDefault();
$reader = new ExportingReader(
new MetricExporter(

View File

@ -6,12 +6,12 @@ namespace OpenTelemetry\Example;
require __DIR__ . '/../../../vendor/autoload.php';
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\Contrib\Otlp\MetricExporter;
use OpenTelemetry\SDK\Common\Export\Http\PsrTransportFactory;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;
$clock = ClockFactory::getDefault();
$clock = Clock::getDefault();
$reader = new ExportingReader(
new MetricExporter(
(new PsrTransportFactory())->create('http://collector:4318/v1/metrics', \OpenTelemetry\Contrib\Otlp\ContentTypes::JSON)

View File

@ -6,12 +6,12 @@ namespace OpenTelemetry\Example;
require __DIR__ . '/../../../vendor/autoload.php';
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\Contrib\Otlp\MetricExporter;
use OpenTelemetry\SDK\Common\Export\Stream\StreamTransportFactory;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;
$clock = ClockFactory::getDefault();
$clock = Clock::getDefault();
// @psalm-suppress InternalMethod
$reader = new ExportingReader(
new MetricExporter(

View File

@ -4,10 +4,10 @@ declare(strict_types=1);
namespace OpenTelemetry\Example;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\API\Metrics\ObserverInterface;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilter\WithSampledTraceExemplarFilter;
use OpenTelemetry\SDK\Metrics\MeterProvider;
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;

View File

@ -6,10 +6,10 @@ namespace OpenTelemetry\Example;
require __DIR__ . '/../../../vendor/autoload.php';
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\Contrib\Otlp\ContentTypes;
use OpenTelemetry\Contrib\Otlp\SpanExporter;
use OpenTelemetry\SDK\Common\Export\Stream\StreamTransportFactory;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;
@ -23,7 +23,7 @@ echo 'Starting OTLP example';
$tracerProvider = new TracerProvider(
new BatchSpanProcessor(
$exporter,
ClockFactory::getDefault()
Clock::getDefault()
)
);
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');

View File

@ -6,7 +6,7 @@ namespace OpenTelemetry\Example;
require __DIR__ . '/../../../vendor/autoload.php';
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\SDK\Trace\SpanExporter\ConsoleSpanExporterFactory;
use OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;
@ -19,7 +19,7 @@ echo sprintf('Sending batches every %dms and on shutdown', $delayMillis) . PHP_E
$tracerProvider = new TracerProvider(
new BatchSpanProcessor(
(new ConsoleSpanExporterFactory())->create(),
ClockFactory::getDefault(),
Clock::getDefault(),
2048, //max spans to queue before sending to exporter
$delayMillis, //batch delay milliseconds
)

View File

@ -17,9 +17,9 @@ require __DIR__ . '/../../vendor/autoload.php';
* will be the last log entry
*/
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\Contrib\Zipkin\Exporter as ZipkinExporter;
use OpenTelemetry\SDK\Common\Export\Http\PsrTransportFactory;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Logs\SimplePsrFileLogger;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\SpanExporter\LoggerDecorator;
@ -57,7 +57,7 @@ $decorator = new LoggerDecorator(
* Create the Tracer
*/
$tracerProvider = new TracerProvider(
new BatchSpanProcessor($decorator, ClockFactory::getDefault()),
new BatchSpanProcessor($decorator, Clock::getDefault()),
new AlwaysOnSampler()
);
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');

View File

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\API\Common\Time;
final class Clock
{
private static ?ClockInterface $clock = null;
public static function getDefault(): ClockInterface
{
return self::$clock ??= new SystemClock();
}
public static function setDefault(ClockInterface $clock): void
{
self::$clock = $clock;
}
public static function reset(): void
{
self::$clock = null;
}
}

View File

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\API\Common\Time;
interface ClockInterface
{
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;
}

View File

@ -2,11 +2,14 @@
declare(strict_types=1);
namespace OpenTelemetry\SDK\Common\Time;
namespace OpenTelemetry\API\Common\Time;
use function hrtime;
use function microtime;
/**
* @internal OpenTelemetry
*/
final class SystemClock implements ClockInterface
{
private static int $referenceTime = 0;

View File

@ -2,10 +2,11 @@
declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\SDK\Util;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
namespace OpenTelemetry\API\Common\Time;
/**
* @internal OpenTelemetry
*/
final class TestClock implements ClockInterface
{
public const DEFAULT_START_EPOCH = 1633060331386955008; // Fri Oct 01 2021 03:52:11 UTC
@ -36,9 +37,4 @@ final class TestClock implements ClockInterface
{
return $this->currentEpochNanos;
}
public function nanoTime(): int
{
return $this->currentEpochNanos;
}
}

View File

@ -4,11 +4,11 @@ declare(strict_types=1);
namespace OpenTelemetry\Config\SDK\ComponentProvider\Logs;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\Config\SDK\Configuration\ComponentPlugin;
use OpenTelemetry\Config\SDK\Configuration\ComponentProvider;
use OpenTelemetry\Config\SDK\Configuration\ComponentProviderRegistry;
use OpenTelemetry\Config\SDK\Configuration\Context;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Logs\LogRecordExporterInterface;
use OpenTelemetry\SDK\Logs\LogRecordProcessorInterface;
use OpenTelemetry\SDK\Logs\Processor\BatchLogRecordProcessor;
@ -33,7 +33,7 @@ final class LogRecordProcessorBatch implements ComponentProvider
{
return new BatchLogRecordProcessor(
exporter: $properties['exporter']->create($context),
clock: ClockFactory::getDefault(),
clock: Clock::getDefault(),
maxQueueSize: $properties['max_queue_size'],
scheduledDelayMillis: $properties['schedule_delay'],
exportTimeoutMillis: $properties['export_timeout'],

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace OpenTelemetry\Config\SDK\ComponentProvider;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\Config\SDK\Configuration\ComponentPlugin;
use OpenTelemetry\Config\SDK\Configuration\ComponentProvider;
use OpenTelemetry\Config\SDK\Configuration\ComponentProviderRegistry;
@ -13,7 +14,6 @@ use OpenTelemetry\Context\Propagation\NoopTextMapPropagator;
use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Logs\LoggerProvider;
use OpenTelemetry\SDK\Logs\LogRecordProcessorInterface;
use OpenTelemetry\SDK\Logs\Processor\MultiLogRecordProcessor;
@ -215,7 +215,7 @@ final class OpenTelemetrySdk implements ComponentProvider
$meterProvider = new MeterProvider(
contextStorage: null,
resource: $resource,
clock: ClockFactory::getDefault(),
clock: Clock::getDefault(),
attributesFactory: Attributes::factory(),
instrumentationScopeFactory: new InstrumentationScopeFactory(Attributes::factory()),
metricReaders: $metricReaders, // @phpstan-ignore-line

View File

@ -4,11 +4,11 @@ declare(strict_types=1);
namespace OpenTelemetry\Config\SDK\ComponentProvider\Trace;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\Config\SDK\Configuration\ComponentPlugin;
use OpenTelemetry\Config\SDK\Configuration\ComponentProvider;
use OpenTelemetry\Config\SDK\Configuration\ComponentProviderRegistry;
use OpenTelemetry\Config\SDK\Configuration\Context;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Trace\SpanExporterInterface;
use OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessor;
use OpenTelemetry\SDK\Trace\SpanProcessorInterface;
@ -33,7 +33,7 @@ final class SpanProcessorBatch implements ComponentProvider
{
return new BatchSpanProcessor(
exporter: $properties['exporter']->create($context),
clock: ClockFactory::getDefault(),
clock: Clock::getDefault(),
maxQueueSize: $properties['max_queue_size'],
scheduledDelayMillis: $properties['schedule_delay'],
exportTimeoutMillis: $properties['export_timeout'],

View File

@ -5,10 +5,10 @@ declare(strict_types=1);
namespace OpenTelemetry\Contrib\Zipkin;
use function max;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\Contrib\Zipkin\SpanKind as ZipkinSpanKind;
use OpenTelemetry\SDK\Common\Time\Util as TimeUtil;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SDK\Trace\EventInterface;
use OpenTelemetry\SDK\Trace\SpanConverterInterface;
@ -25,6 +25,8 @@ class SpanConverter implements SpanConverterInterface
const KEY_DROPPED_EVENTS_COUNT = 'otel.dropped_events_count';
const KEY_DROPPED_LINKS_COUNT = 'otel.dropped_links_count';
public const NANOS_PER_MICROSECOND = 1_000;
const REMOTE_ENDPOINT_PREFERRED_ATTRIBUTE_TO_RANK_MAP = [
'peer.service' => 1,
'net.peer.name' => 2,
@ -44,6 +46,12 @@ class SpanConverter implements SpanConverterInterface
$this->defaultServiceName = ResourceInfoFactory::defaultResource()->getAttributes()->get(ResourceAttributes::SERVICE_NAME);
}
/** @psalm-pure */
public static function nanosToMicros(int $nanoseconds): int
{
return intdiv($nanoseconds, ClockInterface::NANOS_PER_MICROSECOND);
}
private function sanitiseTagValue($value): string
{
// Casting false to string makes an empty string
@ -80,8 +88,8 @@ class SpanConverter implements SpanConverterInterface
{
$spanParent = $span->getParentContext();
$startTimestamp = TimeUtil::nanosToMicros($span->getStartEpochNanos());
$endTimestamp = TimeUtil::nanosToMicros($span->getEndEpochNanos());
$startTimestamp = self::nanosToMicros($span->getStartEpochNanos());
$endTimestamp = self::nanosToMicros($span->getEndEpochNanos());
$serviceName = $span->getResource()->getAttributes()->get(ResourceAttributes::SERVICE_NAME)
??
@ -189,7 +197,7 @@ class SpanConverter implements SpanConverterInterface
$value = ($attributesAsJson !== null) ? sprintf('"%s": %s', $eventName, $attributesAsJson) : sprintf('"%s"', $eventName);
$annotation = [
'timestamp' => TimeUtil::nanosToMicros($event->getEpochNanos()),
'timestamp' => self::nanosToMicros($event->getEpochNanos()),
'value' => $value,
];
if ($event->getAttributes()->getDroppedAttributesCount() > 0) {

View File

@ -4,27 +4,26 @@ declare(strict_types=1);
namespace OpenTelemetry\SDK\Common\Time;
final class ClockFactory implements ClockFactoryInterface
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Common\Time\ClockInterface;
/**
* @deprecated Use OpenTelemetry\API\Common\Time\Clock
* @codeCoverageIgnore
*/
class ClockFactory
{
private static ?ClockInterface $default = null;
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();
return Clock::getDefault();
}
public static function setDefault(?ClockInterface $clock): void
{
self::$default = $clock;
if ($clock !== null) {
Clock::setDefault($clock);
} else {
Clock::reset();
}
}
}

View File

@ -1,16 +0,0 @@
<?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;
}

View File

@ -4,16 +4,11 @@ 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;
use OpenTelemetry\API\Common\Time as API;
/**
* Returns the current epoch wall-clock timestamp in nanoseconds
*/
public function now(): int;
/**
* @deprecated Use OpenTelemetry\API\Common\Time\ClockInterface
*/
interface ClockInterface extends API\ClockInterface
{
}

View File

@ -1,116 +0,0 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\SDK\Common\Time;
final class StopWatch implements StopWatchInterface
{
private const INITIAL_ELAPSED_TIME = 0;
private bool $running = false;
private ?int $startTime = null;
private ?int $stopTime = null;
public function __construct(
private readonly ClockInterface $clock,
private ?int $initialStartTime = null,
) {
}
public function isRunning(): bool
{
return $this->running;
}
public function start(): void
{
// resolve start time as early as possible
$startTime = $this->time();
if ($this->isRunning()) {
return;
}
$this->startTime = $startTime;
if (!$this->hasBeenStarted()) {
$this->initialStartTime = $startTime;
}
$this->running = true;
}
public function stop(): void
{
if (!$this->isRunning()) {
return;
}
$this->stopTime = $this->time();
$this->running = false;
}
public function reset(): void
{
$this->startTime = $this->initialStartTime = $this->isRunning() ? $this->time() : null;
}
public function getElapsedTime(): int
{
if (!$this->hasBeenStarted()) {
return self::INITIAL_ELAPSED_TIME;
}
return $this->calculateElapsedTime();
}
public function getLastElapsedTime(): int
{
if (!$this->hasBeenStarted()) {
return self::INITIAL_ELAPSED_TIME;
}
return $this->calculateLastElapsedTime();
}
private function time(): int
{
return $this->clock->now();
}
private function hasBeenStarted(): bool
{
return $this->initialStartTime !== null;
}
private function calculateElapsedTime(): int
{
$referenceTime = $this->isRunning()
? $this->time()
: $this->getStopTime();
return $referenceTime - $this->getInitialStartTime();
}
private function calculateLastElapsedTime(): int
{
$referenceTime = $this->isRunning()
? $this->time()
: $this->getStopTime();
return $referenceTime - $this->getStartTime();
}
private function getInitialStartTime(): ?int
{
return $this->initialStartTime;
}
private function getStartTime(): ?int
{
return $this->startTime;
}
private function getStopTime(): ?int
{
return $this->stopTime;
}
}

View File

@ -1,44 +0,0 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\SDK\Common\Time;
final class StopWatchFactory implements StopWatchFactoryInterface
{
private static ?StopWatchInterface $default = null;
private readonly ClockInterface $clock;
public function __construct(
?ClockInterface $clock = null,
private readonly ?int $initialStartTime = null,
) {
$this->clock = $clock ?? ClockFactory::getDefault();
}
public static function create(?ClockInterface $clock = null, ?int $initialStartTime = null): self
{
return new self($clock, $initialStartTime);
}
public static function fromClockFactory(ClockFactoryInterface $factory, ?int $initialStartTime = null): self
{
return self::create($factory->build(), $initialStartTime);
}
public function build(): StopWatch
{
return new StopWatch($this->clock, $this->initialStartTime);
}
public static function getDefault(): StopWatchInterface
{
return self::$default ?? self::$default = self::create()->build();
}
public static function setDefault(?StopWatchInterface $default): void
{
self::$default = $default;
}
}

View File

@ -1,18 +0,0 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\SDK\Common\Time;
interface StopWatchFactoryInterface
{
public static function create(?ClockInterface $clock = null, ?int $initialStartTime = null): self;
public static function fromClockFactory(ClockFactoryInterface $factory, ?int $initialStartTime = null): self;
public function build(): StopWatchInterface;
public static function getDefault(): StopWatchInterface;
public static function setDefault(?StopWatchInterface $default): void;
}

View File

@ -1,20 +0,0 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\SDK\Common\Time;
interface StopWatchInterface
{
public function isRunning(): bool;
public function start(): void;
public function stop(): void;
public function reset(): void;
public function getElapsedTime(): int;
public function getLastElapsedTime(): int;
}

View File

@ -1,32 +0,0 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\SDK\Common\Time;
class Util
{
/** @psalm-pure */
public static function nanosToMicros(int $nanoseconds): int
{
return intdiv($nanoseconds, ClockInterface::NANOS_PER_MICROSECOND);
}
/** @psalm-pure */
public static function nanosToMillis(int $nanoseconds): int
{
return intdiv($nanoseconds, ClockInterface::NANOS_PER_MILLISECOND);
}
/** @psalm-pure */
public static function secondsToNanos(int $seconds): int
{
return $seconds * ClockInterface::NANOS_PER_SECOND;
}
/** @psalm-pure */
public static function millisToNanos(int $milliSeconds): int
{
return $milliSeconds * ClockInterface::NANOS_PER_MILLISECOND;
}
}

View File

@ -5,12 +5,12 @@ declare(strict_types=1);
namespace OpenTelemetry\SDK\Logs;
use InvalidArgumentException;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Metrics\MeterProviderInterface;
use OpenTelemetry\SDK\Common\Configuration\Configuration;
use OpenTelemetry\SDK\Common\Configuration\KnownValues;
use OpenTelemetry\SDK\Common\Configuration\KnownValues as Values;
use OpenTelemetry\SDK\Common\Configuration\Variables;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Logs\Processor\BatchLogRecordProcessor;
use OpenTelemetry\SDK\Logs\Processor\MultiLogRecordProcessor;
use OpenTelemetry\SDK\Logs\Processor\NoopLogRecordProcessor;
@ -38,7 +38,7 @@ class LogRecordProcessorFactory
return match ($name) {
KnownValues::VALUE_BATCH => new BatchLogRecordProcessor(
$exporter,
ClockFactory::getDefault(),
Clock::getDefault(),
Configuration::getInt(Variables::OTEL_BLRP_MAX_QUEUE_SIZE),
Configuration::getInt(Variables::OTEL_BLRP_SCHEDULE_DELAY),
Configuration::getInt(Variables::OTEL_BLRP_EXPORT_TIMEOUT),

View File

@ -6,12 +6,12 @@ namespace OpenTelemetry\SDK\Logs\Processor;
use InvalidArgumentException;
use OpenTelemetry\API\Behavior\LogsMessagesTrait;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\API\Metrics\MeterProviderInterface;
use OpenTelemetry\API\Metrics\ObserverInterface;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Context\ContextInterface;
use OpenTelemetry\SDK\Common\Future\CancellationInterface;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
use OpenTelemetry\SDK\Logs\LogRecordExporterInterface;
use OpenTelemetry\SDK\Logs\LogRecordProcessorInterface;
use OpenTelemetry\SDK\Logs\ReadWriteLogRecord;

View File

@ -9,6 +9,7 @@ use ArrayAccess;
use function assert;
use function is_callable;
use OpenTelemetry\API\Behavior\LogsMessagesTrait;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\API\Metrics\AsynchronousInstrument;
use OpenTelemetry\API\Metrics\CounterInterface;
use OpenTelemetry\API\Metrics\GaugeInterface;
@ -20,7 +21,6 @@ use OpenTelemetry\API\Metrics\ObservableGaugeInterface;
use OpenTelemetry\API\Metrics\ObservableUpDownCounterInterface;
use OpenTelemetry\API\Metrics\UpDownCounterInterface;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeInterface;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
use function OpenTelemetry\SDK\Common\Util\closure;
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilterInterface;
use OpenTelemetry\SDK\Metrics\MetricRegistration\MultiRegistryRegistration;

View File

@ -5,12 +5,12 @@ declare(strict_types=1);
namespace OpenTelemetry\SDK\Metrics;
use ArrayAccess;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\API\Metrics\MeterInterface;
use OpenTelemetry\API\Metrics\Noop\NoopMeter;
use OpenTelemetry\Context\ContextStorageInterface;
use OpenTelemetry\SDK\Common\Attribute\AttributesFactoryInterface;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactoryInterface;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilterInterface;
use OpenTelemetry\SDK\Metrics\MetricFactory\StreamFactory;
use OpenTelemetry\SDK\Metrics\MetricRegistry\MetricRegistry;

View File

@ -4,9 +4,9 @@ declare(strict_types=1);
namespace OpenTelemetry\SDK\Metrics;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilter\WithSampledTraceExemplarFilter;
use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilterInterface;
use OpenTelemetry\SDK\Metrics\StalenessHandler\NoopStalenessHandlerFactory;
@ -50,7 +50,7 @@ class MeterProviderBuilder
return new MeterProvider(
null,
$this->resource ?? ResourceInfoFactory::emptyResource(),
ClockFactory::getDefault(),
Clock::getDefault(),
Attributes::factory(),
new InstrumentationScopeFactory(Attributes::factory()),
$this->metricReaders,

View File

@ -6,10 +6,10 @@ namespace OpenTelemetry\SDK\Metrics\MetricRegistry;
use function array_key_last;
use Closure;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Context\ContextStorageInterface;
use OpenTelemetry\SDK\Common\Attribute\AttributesFactoryInterface;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
use OpenTelemetry\SDK\Metrics\Instrument;
use OpenTelemetry\SDK\Metrics\Stream\MetricAggregatorFactoryInterface;
use OpenTelemetry\SDK\Metrics\Stream\MetricAggregatorInterface;

View File

@ -5,7 +5,7 @@ declare(strict_types=1);
namespace OpenTelemetry\SDK\Metrics\StalenessHandler;
use Closure;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\SDK\Metrics\ReferenceCounterInterface;
use OpenTelemetry\SDK\Metrics\StalenessHandlerFactoryInterface;
use OpenTelemetry\SDK\Metrics\StalenessHandlerInterface;

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace OpenTelemetry\SDK\Trace;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Trace as API;
use OpenTelemetry\API\Trace\SpanContextInterface;
use OpenTelemetry\Context\ContextInterface;
@ -11,7 +12,6 @@ use OpenTelemetry\SDK\Common\Attribute\AttributesBuilderInterface;
use OpenTelemetry\SDK\Common\Dev\Compatibility\Util as BcUtil;
use OpenTelemetry\SDK\Common\Exception\StackTraceFormatter;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeInterface;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use Throwable;
@ -84,7 +84,7 @@ final class Span extends API\Span implements ReadWriteSpanInterface
$attributesBuilder,
$links,
$totalRecordedLinks,
$startEpochNanos !== 0 ? $startEpochNanos : ClockFactory::getDefault()->now()
$startEpochNanos !== 0 ? $startEpochNanos : Clock::getDefault()->now()
);
// Call onStart here to ensure the span is fully initialized.
@ -176,7 +176,7 @@ final class Span extends API\Span implements ReadWriteSpanInterface
return $this;
}
$timestamp ??= ClockFactory::getDefault()->now();
$timestamp ??= Clock::getDefault()->now();
$eventAttributesBuilder = $this->spanLimits->getEventAttributesFactory()->builder($attributes);
$this->events[] = new Event($name, $timestamp, $eventAttributesBuilder->build());
@ -194,7 +194,7 @@ final class Span extends API\Span implements ReadWriteSpanInterface
return $this;
}
$timestamp ??= ClockFactory::getDefault()->now();
$timestamp ??= Clock::getDefault()->now();
$eventAttributesBuilder = $this->spanLimits->getEventAttributesFactory()->builder([
'exception.type' => $exception::class,
'exception.message' => $exception->getMessage(),
@ -249,7 +249,7 @@ final class Span extends API\Span implements ReadWriteSpanInterface
return;
}
$this->endEpochNanos = $endEpochNanos ?? ClockFactory::getDefault()->now();
$this->endEpochNanos = $endEpochNanos ?? Clock::getDefault()->now();
$this->hasEnded = true;
$this->spanProcessor->onEnd($this);
@ -295,7 +295,7 @@ final class Span extends API\Span implements ReadWriteSpanInterface
/** @inheritDoc */
public function getDuration(): int
{
return ($this->hasEnded ? $this->endEpochNanos : ClockFactory::getDefault()->now()) - $this->startEpochNanos;
return ($this->hasEnded ? $this->endEpochNanos : Clock::getDefault()->now()) - $this->startEpochNanos;
}
/** @inheritDoc */

View File

@ -8,12 +8,12 @@ use function assert;
use function count;
use InvalidArgumentException;
use OpenTelemetry\API\Behavior\LogsMessagesTrait;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\API\Metrics\MeterProviderInterface;
use OpenTelemetry\API\Metrics\ObserverInterface;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Context\ContextInterface;
use OpenTelemetry\SDK\Common\Future\CancellationInterface;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
use OpenTelemetry\SDK\Trace\ReadableSpanInterface;
use OpenTelemetry\SDK\Trace\ReadWriteSpanInterface;
use OpenTelemetry\SDK\Trace\SpanDataInterface;

View File

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace OpenTelemetry\SDK\Trace\SpanProcessor;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\SDK\Metrics\MeterProviderInterface;
use OpenTelemetry\SDK\Trace\SpanExporterInterface;
@ -27,7 +27,7 @@ class BatchSpanProcessorBuilder
{
return new BatchSpanProcessor(
$this->exporter,
ClockFactory::getDefault(),
Clock::getDefault(),
BatchSpanProcessor::DEFAULT_MAX_QUEUE_SIZE,
BatchSpanProcessor::DEFAULT_SCHEDULE_DELAY,
BatchSpanProcessor::DEFAULT_EXPORT_TIMEOUT,

View File

@ -5,10 +5,10 @@ declare(strict_types=1);
namespace OpenTelemetry\SDK\Trace;
use InvalidArgumentException;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\SDK\Common\Configuration\Configuration;
use OpenTelemetry\SDK\Common\Configuration\KnownValues as Values;
use OpenTelemetry\SDK\Common\Configuration\Variables as Env;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Metrics\MeterProviderInterface;
use OpenTelemetry\SDK\Metrics\NoopMeterProvider;
use OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessor;
@ -28,7 +28,7 @@ class SpanProcessorFactory
return match ($name) {
Values::VALUE_BATCH => new BatchSpanProcessor(
$exporter,
ClockFactory::getDefault(),
Clock::getDefault(),
Configuration::getInt(Env::OTEL_BSP_MAX_QUEUE_SIZE, BatchSpanProcessor::DEFAULT_MAX_QUEUE_SIZE),
Configuration::getInt(Env::OTEL_BSP_SCHEDULE_DELAY, BatchSpanProcessor::DEFAULT_SCHEDULE_DELAY),
Configuration::getInt(Env::OTEL_BSP_EXPORT_TIMEOUT, BatchSpanProcessor::DEFAULT_EXPORT_TIMEOUT),

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace OpenTelemetry\Tests\Benchmark;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Trace\TracerInterface;
use OpenTelemetry\Contrib\Otlp\SpanExporter;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
@ -11,7 +12,6 @@ use OpenTelemetry\SDK\Common\Export\TransportInterface;
use OpenTelemetry\SDK\Common\Future\CancellationInterface;
use OpenTelemetry\SDK\Common\Future\CompletedFuture;
use OpenTelemetry\SDK\Common\Future\FutureInterface;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\SamplerInterface;
@ -95,7 +95,7 @@ class OtlpBench
$exporter = new SpanExporter($transport);
$processor = $params[1] === self::SIMPLE
? new SimpleSpanProcessor($exporter)
: new BatchSpanProcessor($exporter, ClockFactory::getDefault());
: new BatchSpanProcessor($exporter, Clock::getDefault());
$provider = new TracerProvider($processor, $this->sampler, $this->resource);
$this->tracer = $provider->getTracer('io.opentelemetry.contrib.php');
}

View File

@ -5,13 +5,14 @@ declare(strict_types=1);
namespace OpenTelemetry\Tests\Integration\SDK;
use AssertWell\PHPUnitGlobalState\EnvironmentVariables;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\API\Common\Time\TestClock;
use OpenTelemetry\API\Metrics as API;
use OpenTelemetry\API\Metrics\Noop\NoopMeter;
use OpenTelemetry\API\Metrics\ObserverInterface;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScope;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
use OpenTelemetry\SDK\Metrics\Data\Metric;
use OpenTelemetry\SDK\Metrics\Data\NumberDataPoint;
use OpenTelemetry\SDK\Metrics\Data\Sum;
@ -27,7 +28,6 @@ use OpenTelemetry\SDK\Metrics\MetricSourceRegistryInterface;
use OpenTelemetry\SDK\Metrics\StalenessHandler\ImmediateStalenessHandlerFactory;
use OpenTelemetry\SDK\Metrics\View\CriteriaViewRegistry;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\Tests\Unit\SDK\Util\TestClock;
use PHPUnit\Framework\TestCase;
/**

View File

@ -6,9 +6,9 @@ require __DIR__ . '/trace-context-handler.php';
use Nyholm\Psr7\Request;
use Nyholm\Psr7\Response;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\Contrib\Zipkin\Exporter as ZipkinExporter;
use OpenTelemetry\SDK\Common\Export\Http\PsrTransportFactory;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;
use Psr\Http\Client\ClientExceptionInterface;
@ -28,7 +28,7 @@ function main(): void
new ZipkinExporter(
(new PsrTransportFactory())->create('http://zipkin:9412/api/v2/spans', 'application/json')
),
ClockFactory::getDefault()
Clock::getDefault()
),
],
))->getTracer('W3C Trace-Context Test Service');

View File

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\API\Common\Time;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\API\Common\Time\SystemClock;
use PHPUnit\Framework\TestCase;
/**
* @covers \OpenTelemetry\API\Common\Time\Clock
*/
class ClockTest extends TestCase
{
public function test_default_is_system_clock(): void
{
$this->assertInstanceOf(SystemClock::class, Clock::getDefault());
}
public function test_default_is_settable(): void
{
$clock = $this->createMock(ClockInterface::class);
Clock::setDefault($clock);
$this->assertSame($clock, Clock::getDefault());
}
public function test_default_is_resettable(): void
{
$clock = $this->createMock(ClockInterface::class);
Clock::setDefault(
$clock
);
Clock::reset();
$this->assertNotSame($clock, Clock::getDefault());
}
}

View File

@ -2,14 +2,14 @@
declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\SDK\Common\Time;
namespace OpenTelemetry\Tests\Unit\API\Common\Time;
use DateTime;
use OpenTelemetry\SDK\Common\Time\SystemClock;
use OpenTelemetry\API\Common\Time\SystemClock;
use PHPUnit\Framework\TestCase;
/**
* @covers \OpenTelemetry\SDK\Common\Time\SystemClock
* @covers \OpenTelemetry\API\Common\Time\SystemClock
*/
class SystemClockTest extends TestCase
{

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\API\Trace;
use OpenTelemetry\API\Common\Time\TestClock;
use OpenTelemetry\API\Trace\NonRecordingSpan;
use OpenTelemetry\API\Trace\NoopSpanBuilder;
use OpenTelemetry\API\Trace\SpanContextInterface;
@ -11,7 +12,6 @@ use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Context\ContextKeys;
use OpenTelemetry\Context\ContextStorageInterface;
use OpenTelemetry\Tests\Unit\SDK\Util\TestClock;
use PHPUnit\Framework\TestCase;
/**

View File

@ -1,45 +0,0 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\SDK\Common\Time;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
use OpenTelemetry\SDK\Common\Time\SystemClock;
use PHPUnit\Framework\TestCase;
/**
* @covers \OpenTelemetry\SDK\Common\Time\ClockFactory
*/
class ClockFactoryTest extends TestCase
{
public function test_build(): void
{
$this->assertInstanceOf(SystemClock::class, ClockFactory::create()->build());
}
public function test_default_is_system_clock(): void
{
$this->assertInstanceOf(SystemClock::class, ClockFactory::getDefault());
}
public function test_default_is_settable(): void
{
$clock = $this->createMock(ClockInterface::class);
ClockFactory::setDefault($clock);
$this->assertSame($clock, ClockFactory::getDefault());
}
public function test_default_is_resettable(): void
{
$clock = $this->createMock(ClockInterface::class);
ClockFactory::setDefault(
$clock
);
ClockFactory::setDefault(null);
$this->assertNotSame($clock, ClockFactory::getDefault());
}
}

View File

@ -1,49 +0,0 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\SDK\Common\Time;
use OpenTelemetry\SDK\Common\Time\ClockFactoryInterface;
use OpenTelemetry\SDK\Common\Time\StopWatch;
use OpenTelemetry\SDK\Common\Time\StopWatchFactory;
use OpenTelemetry\SDK\Common\Time\StopWatchInterface;
use PHPUnit\Framework\TestCase;
/**
* @covers \OpenTelemetry\SDK\Common\Time\StopWatchFactory
*/
class StopWatchFactoryTest extends TestCase
{
public function test_from_clock_factory(): void
{
$clockFactory = $this->createMock(ClockFactoryInterface::class);
$clockFactory->expects($this->once())->method('build');
StopWatchFactory::fromClockFactory($clockFactory);
}
public function test_default_is_system_clock(): void
{
$this->assertInstanceOf(StopWatch::class, StopWatchFactory::getDefault());
}
public function test_default_is_settable(): void
{
$stopwatch = $this->createMock(StopWatchInterface::class);
StopWatchFactory::setDefault($stopwatch);
$this->assertSame($stopwatch, StopWatchFactory::getDefault());
}
public function test_default_is_resettable(): void
{
StopWatchFactory::setDefault(
$this->createMock(StopWatchInterface::class)
);
StopWatchFactory::setDefault(null);
$this->assertInstanceOf(StopWatch::class, StopWatchFactory::getDefault());
}
}

View File

@ -1,231 +0,0 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\SDK\Common\Time;
use OpenTelemetry\SDK\Common\Time\StopWatch;
use OpenTelemetry\Tests\Unit\SDK\Util\TestClock;
use PHPUnit\Framework\TestCase;
/**
* @covers \OpenTelemetry\SDK\Common\Time\StopWatch
*/
class StopWatchTest extends TestCase
{
private StopWatch $stopwatch;
private TestClock $testClock;
public function setUp(): void
{
$this->init();
}
private function init(?int $initialStartTime = null): void
{
$this->stopwatch = new StopWatch(
$this->testClock = new TestClock(),
$initialStartTime
);
}
public function test_is_not_running_initially(): void
{
$this->assertFalse($this->stopwatch->isRunning());
}
public function test_start(): void
{
$this->stopwatch->start();
$this->assertTrue($this->stopwatch->isRunning());
}
public function test_restart(): void
{
$this->stopwatch->start();
$this->stopwatch->stop();
$this->stopwatch->start();
$this->assertTrue($this->stopwatch->isRunning());
}
public function test_stop(): void
{
$this->stopwatch->start();
$this->stopwatch->stop();
$this->assertFalse($this->stopwatch->isRunning());
}
public function test_stop_without_start(): void
{
$this->stopwatch->stop();
$this->assertFalse($this->stopwatch->isRunning());
}
public function test_get_elapsed_time_initially(): void
{
$this->assertSame(0, $this->stopwatch->getElapsedTime());
}
public function test_get_elapsed_time_started(): void
{
$elapsed = 500;
$this->stopwatch->start();
$this->testClock->advance($elapsed);
$this->assertSame($elapsed, $this->stopwatch->getElapsedTime());
}
public function test_get_elapsed_time_started_twice(): void
{
$elapsed = 500;
$this->stopwatch->start();
$this->testClock->advance($elapsed);
$this->stopwatch->start();
$this->testClock->advance($elapsed);
$this->assertSame($elapsed * 2, $this->stopwatch->getElapsedTime());
}
public function test_get_elapsed_time_stopped(): void
{
$elapsed = 500;
$this->stopwatch->start();
$this->testClock->advance($elapsed);
$this->stopwatch->stop();
$this->testClock->advance($elapsed);
$this->assertSame($elapsed, $this->stopwatch->getElapsedTime());
}
public function test_get_elapsed_time_stopped_twice(): void
{
$elapsed = 500;
$this->stopwatch->start();
$this->testClock->advance($elapsed);
$this->stopwatch->stop();
$this->testClock->advance($elapsed);
$this->stopwatch->stop();
$this->assertSame($elapsed, $this->stopwatch->getElapsedTime());
}
public function test_get_elapsed_time_with_initial_start_time(): void
{
$elapsed = 500;
$this->init(TestClock::DEFAULT_START_EPOCH - $elapsed);
$this->stopwatch->start();
$this->testClock->advance($elapsed);
$this->assertSame($elapsed * 2, $this->stopwatch->getElapsedTime());
}
public function test_get_last_elapsed_time_initially(): void
{
$this->assertSame(0, $this->stopwatch->getLastElapsedTime());
}
public function test_get_last_elapsed_time_started(): void
{
$elapsed = 500;
$this->stopwatch->start();
$this->testClock->advance($elapsed);
$this->assertSame($elapsed, $this->stopwatch->getLastElapsedTime());
}
public function test_get_last_elapsed_time_started_twice(): void
{
$elapsed = 500;
$this->stopwatch->start();
$this->testClock->advance($elapsed);
$this->stopwatch->start();
$this->testClock->advance($elapsed);
$this->assertSame($elapsed * 2, $this->stopwatch->getLastElapsedTime());
}
public function test_get_last_elapsed_time_restarted(): void
{
$elapsed = 500;
$this->stopwatch->start();
$this->testClock->advance($elapsed);
$this->stopwatch->stop();
$this->testClock->advance($elapsed);
$this->stopwatch->start();
$this->testClock->advance($elapsed);
$this->assertSame($elapsed, $this->stopwatch->getLastElapsedTime());
}
public function test_get_last_elapsed_time_stopped(): void
{
$elapsed = 500;
$this->stopwatch->start();
$this->testClock->advance($elapsed);
$this->stopwatch->stop();
$this->testClock->advance($elapsed);
$this->assertSame($elapsed, $this->stopwatch->getLastElapsedTime());
}
public function test_get_last_elapsed_time_stopped_twice(): void
{
$elapsed = 500;
$this->stopwatch->start();
$this->testClock->advance($elapsed);
$this->stopwatch->stop();
$this->testClock->advance($elapsed);
$this->stopwatch->stop();
$this->assertSame($elapsed, $this->stopwatch->getLastElapsedTime());
}
public function test_reset_initially(): void
{
$this->stopwatch->reset();
$this->testClock->advance(500);
$this->assertSame(0, $this->stopwatch->getElapsedTime());
$this->assertSame(0, $this->stopwatch->getLastElapsedTime());
}
public function test_reset_started(): void
{
$elapsed = 500;
$this->stopwatch->start();
$this->testClock->advance($elapsed);
$this->stopwatch->reset();
$this->testClock->advance($elapsed);
$this->assertSame($elapsed, $this->stopwatch->getElapsedTime());
$this->assertSame($elapsed, $this->stopwatch->getLastElapsedTime());
}
public function test_reset_stopped(): void
{
$elapsed = 500;
$this->stopwatch->start();
$this->testClock->advance($elapsed);
$this->stopwatch->stop();
$this->stopwatch->reset();
$this->testClock->advance($elapsed);
$this->assertSame(0, $this->stopwatch->getElapsedTime());
$this->assertSame(0, $this->stopwatch->getLastElapsedTime());
}
}

View File

@ -1,34 +0,0 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\SDK\Common\Time;
use OpenTelemetry\SDK\Common\Time\Util;
use PHPUnit\Framework\TestCase;
/**
* @covers \OpenTelemetry\SDK\Common\Time\Util
*/
class UtilTest extends TestCase
{
public function test_nanos_to_micro(): void
{
$this->assertEquals(1, Util::nanosToMicros((int) 1e3));
}
public function test_nanos_to_milli(): void
{
$this->assertEquals(1, Util::nanosToMillis((int) 1e6));
}
public function test_seconds_to_nanos(): void
{
$this->assertEquals((int) 1e9, Util::secondsToNanos(1));
}
public function test_millis_to_nanos(): void
{
$this->assertEquals((int) 1e6, Util::millisToNanos(1));
}
}

View File

@ -10,12 +10,13 @@ use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use OpenTelemetry\API\Behavior\Internal\Logging;
use OpenTelemetry\API\Behavior\Internal\LogWriter\LogWriterInterface;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\API\Common\Time\TestClock;
use OpenTelemetry\Context\Context;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Future\CompletedFuture;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
use OpenTelemetry\SDK\Logs\LogRecordExporterInterface;
use OpenTelemetry\SDK\Logs\LogRecordProcessorInterface;
use OpenTelemetry\SDK\Logs\Processor\BatchLogRecordProcessor;
@ -26,7 +27,6 @@ use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;
use OpenTelemetry\SDK\Metrics\StalenessHandler\ImmediateStalenessHandlerFactory;
use OpenTelemetry\SDK\Metrics\View\CriteriaViewRegistry;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\Tests\Unit\SDK\Util\TestClock;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LogLevel;
@ -45,12 +45,12 @@ class BatchLogRecordProcessorTest extends MockeryTestCase
Logging::setLogWriter($this->logWriter);
$this->testClock = new TestClock();
ClockFactory::setDefault($this->testClock);
Clock::setDefault($this->testClock);
}
protected function tearDown(): void
{
ClockFactory::setDefault(null);
Clock::reset();
Logging::reset();
}
@ -427,7 +427,7 @@ class BatchLogRecordProcessorTest extends MockeryTestCase
$processor = new BatchLogRecordProcessor(
$exporter,
ClockFactory::getDefault(),
Clock::getDefault(),
2048,
5000,
30000,

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\SDK\Metrics;
use OpenTelemetry\API\Common\Time\TestClock;
use OpenTelemetry\API\Metrics\ObserverInterface;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Metrics\Aggregation\ExplicitBucketHistogramAggregation;
@ -25,7 +26,6 @@ use OpenTelemetry\SDK\Metrics\Stream\MetricAggregator;
use OpenTelemetry\SDK\Metrics\Stream\MetricAggregatorFactory;
use OpenTelemetry\SDK\Metrics\Stream\SynchronousMetricStream;
use OpenTelemetry\SDK\Metrics\UpDownCounter;
use OpenTelemetry\Tests\Unit\SDK\Util\TestClock;
use PHPUnit\Framework\TestCase;
use stdClass;
use WeakMap;

View File

@ -4,12 +4,12 @@ declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\SDK\Metrics;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Metrics\Noop\NoopMeter;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScope;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactoryInterface;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Metrics\DefaultAggregationProviderInterface;
use OpenTelemetry\SDK\Metrics\MeterProvider;
use OpenTelemetry\SDK\Metrics\MetricReaderInterface;
@ -39,7 +39,7 @@ final class MeterProviderTest extends TestCase
$meterProvider = new MeterProvider(
null,
ResourceInfoFactory::emptyResource(),
ClockFactory::getDefault(),
Clock::getDefault(),
Attributes::factory(),
$instrumentationScopeFactory,
[],
@ -55,7 +55,7 @@ final class MeterProviderTest extends TestCase
$meterProvider = new MeterProvider(
null,
ResourceInfoFactory::emptyResource(),
ClockFactory::getDefault(),
Clock::getDefault(),
Attributes::factory(),
new InstrumentationScopeFactory(Attributes::factory()),
[],
@ -76,7 +76,7 @@ final class MeterProviderTest extends TestCase
$meterProvider = new MeterProvider(
null,
ResourceInfoFactory::emptyResource(),
ClockFactory::getDefault(),
Clock::getDefault(),
Attributes::factory(),
new InstrumentationScopeFactory(Attributes::factory()),
[$metricReader],
@ -95,7 +95,7 @@ final class MeterProviderTest extends TestCase
$meterProvider = new MeterProvider(
null,
ResourceInfoFactory::emptyResource(),
ClockFactory::getDefault(),
Clock::getDefault(),
Attributes::factory(),
new InstrumentationScopeFactory(Attributes::factory()),
[$metricReader],

View File

@ -5,10 +5,10 @@ declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\SDK\Metrics;
use function func_get_arg;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScope;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Metrics\AggregationInterface;
use OpenTelemetry\SDK\Metrics\DefaultAggregationProviderInterface;
use OpenTelemetry\SDK\Metrics\Instrument;
@ -381,7 +381,7 @@ final class MeterTest extends TestCase
return new MeterProvider(
null,
ResourceInfoFactory::emptyResource(),
ClockFactory::getDefault(),
Clock::getDefault(),
Attributes::factory(),
new InstrumentationScopeFactory(Attributes::factory()),
$metricReaders,

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\SDK\Metrics\MetricFactory;
use function func_get_args;
use OpenTelemetry\API\Common\Time\TestClock;
use OpenTelemetry\API\Metrics\ObserverInterface;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScope;
@ -26,7 +27,6 @@ use OpenTelemetry\SDK\Metrics\StalenessHandler\NoopStalenessHandler;
use OpenTelemetry\SDK\Metrics\StalenessHandlerInterface;
use OpenTelemetry\SDK\Metrics\ViewProjection;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\Tests\Unit\SDK\Util\TestClock;
use PHPUnit\Framework\TestCase;
/**

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\SDK\Metrics\MetricRegistry;
use OpenTelemetry\API\Common\Time\TestClock;
use OpenTelemetry\API\Metrics\ObserverInterface;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Metrics\Aggregation\SumAggregation;
@ -17,7 +18,6 @@ use OpenTelemetry\SDK\Metrics\Stream\AsynchronousMetricStream;
use OpenTelemetry\SDK\Metrics\Stream\MetricAggregator;
use OpenTelemetry\SDK\Metrics\Stream\MetricAggregatorFactory;
use OpenTelemetry\SDK\Metrics\Stream\SynchronousMetricStream;
use OpenTelemetry\Tests\Unit\SDK\Util\TestClock;
use PHPUnit\Framework\TestCase;
use function printf;

View File

@ -4,8 +4,8 @@ declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\SDK\Metrics\StalenessHandler;
use OpenTelemetry\API\Common\Time\TestClock;
use OpenTelemetry\SDK\Metrics\StalenessHandler\DelayedStalenessHandlerFactory;
use OpenTelemetry\Tests\Unit\SDK\Util\TestClock;
use PHPUnit\Framework\TestCase;
use stdClass;
use WeakReference;

View File

@ -4,9 +4,9 @@ declare(strict_types=1);
namespace OpenTelemetry\Tests\Unit\SDK\Trace;
use OpenTelemetry\API\Common\Time\TestClock;
use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;
use OpenTelemetry\SDK\Trace\Event;
use OpenTelemetry\Tests\Unit\SDK\Util\TestClock;
use PHPUnit\Framework\TestCase;
/**

View File

@ -11,13 +11,14 @@ use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use OpenTelemetry\API\Behavior\Internal\Logging;
use OpenTelemetry\API\Behavior\Internal\LogWriter\LogWriterInterface;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\API\Common\Time\TestClock;
use OpenTelemetry\API\Trace as API;
use OpenTelemetry\Context\Context;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Future\CompletedFuture;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
use OpenTelemetry\SDK\Metrics\MeterProvider;
use OpenTelemetry\SDK\Metrics\MetricExporter\InMemoryExporter;
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;
@ -30,7 +31,6 @@ use OpenTelemetry\SDK\Trace\SpanExporterInterface;
use OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessor;
use OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessorBuilder;
use OpenTelemetry\SDK\Trace\SpanProcessorInterface;
use OpenTelemetry\Tests\Unit\SDK\Util\TestClock;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LogLevel;
@ -49,12 +49,12 @@ class BatchSpanProcessorTest extends MockeryTestCase
Logging::setLogWriter($this->logWriter);
$this->testClock = new TestClock();
ClockFactory::setDefault($this->testClock);
Clock::setDefault($this->testClock);
}
protected function tearDown(): void
{
ClockFactory::setDefault(null);
Clock::reset();
Logging::reset();
}
@ -485,7 +485,7 @@ class BatchSpanProcessorTest extends MockeryTestCase
$processor = new BatchSpanProcessor(
$exporter,
ClockFactory::getDefault(),
Clock::getDefault(),
2048,
5000,
30000,

View File

@ -11,6 +11,9 @@ use Mockery\Adapter\Phpunit\MockeryTestCase;
use Mockery\MockInterface;
use OpenTelemetry\API\Behavior\Internal\Logging;
use OpenTelemetry\API\Behavior\Internal\LogWriter\LogWriterInterface;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\API\Common\Time\TestClock;
use OpenTelemetry\API\Trace as API;
use OpenTelemetry\API\Trace\NonRecordingSpan;
use OpenTelemetry\API\Trace\SpanContext;
@ -21,9 +24,6 @@ use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;
use OpenTelemetry\SDK\Common\Exception\StackTraceFormatter;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScope;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Common\Time\ClockInterface;
use OpenTelemetry\SDK\Common\Time\Util as TimeUtil;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SDK\Trace\Event;
@ -38,7 +38,6 @@ use OpenTelemetry\SDK\Trace\SpanLimits;
use OpenTelemetry\SDK\Trace\SpanLimitsBuilder;
use OpenTelemetry\SDK\Trace\SpanProcessorInterface;
use OpenTelemetry\SDK\Trace\StatusData;
use OpenTelemetry\Tests\Unit\SDK\Util\TestClock;
use PHPUnit\Framework\MockObject\MockObject;
use function range;
use function str_repeat;
@ -99,14 +98,14 @@ class SpanTest extends MockeryTestCase
)
);
ClockFactory::setDefault($this->testClock);
Clock::setDefault($this->testClock);
$this->logWriter = $this->createMock(LogWriterInterface::class);
Logging::setLogWriter($this->logWriter);
}
protected function tearDown(): void
{
ClockFactory::setDefault(null);
Clock::reset();
Logging::reset();
// LoggerHolder::unset();
}
@ -552,7 +551,7 @@ class SpanTest extends MockeryTestCase
$span = $this->createTestRootSpan();
$span->addEvent('event1');
$span->addEvent('event2', ['key1' => 1]);
$span->addEvent('event3', [], TimeUtil::secondsToNanos(10));
$span->addEvent('event3', [], 10*ClockInterface::NANOS_PER_SECOND);
$span->end();
@ -562,7 +561,7 @@ class SpanTest extends MockeryTestCase
$this->assertEvent($events[$idx++], 'event1', Attributes::create([]), self::START_EPOCH);
$this->assertEvent($events[$idx++], 'event2', Attributes::create(['key1' => 1]), self::START_EPOCH);
$this->assertEvent($events[$idx], 'event3', Attributes::create([]), TimeUtil::secondsToNanos(10));
$this->assertEvent($events[$idx], 'event3', Attributes::create([]), 10*ClockInterface::NANOS_PER_SECOND);
}
public function test_add_event_attribute_length(): void

View File

@ -6,12 +6,12 @@ namespace OpenTelemetry\Tests\Unit\SDK\Util;
use function count;
use function max;
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Trace as API;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Attribute\AttributesBuilderInterface;
use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScope;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SDK\Trace as SDK;
@ -105,7 +105,7 @@ class SpanData implements SDK\SpanDataInterface
public function addEvent(string $name, AttributesInterface $attributes, int $timestamp = null): self
{
$this->events[] = new SDK\Event($name, $timestamp ?? ClockFactory::getDefault()->now(), $attributes);
$this->events[] = new SDK\Event($name, $timestamp ?? Clock::getDefault()->now(), $attributes);
return $this;
}