From fc161a854813176e373887f1bddfbc52afa8ef38 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Mon, 29 Apr 2024 10:28:06 +1000 Subject: [PATCH] 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 --------- Co-authored-by: Tobias Bachert --- examples/disabling_sdk.php | 4 +- examples/logs/exporters/otlp_grpc.php | 4 +- examples/logs/features/batch_exporting.php | 4 +- examples/metrics/basic.php | 4 +- examples/metrics/exporters/otlp_grpc.php | 4 +- examples/metrics/exporters/otlp_http.php | 4 +- examples/metrics/exporters/stream.php | 4 +- examples/src/ExampleMetricsGenerator.php | 2 +- examples/traces/exporters/otlp_file.php | 4 +- examples/traces/features/batch_exporting.php | 4 +- .../troubleshooting/logging_of_span_data.php | 4 +- src/API/Common/Time/Clock.php | 25 ++ src/API/Common/Time/ClockInterface.php | 17 ++ src/{SDK => API}/Common/Time/SystemClock.php | 5 +- .../API/Common/Time}/TestClock.php | 12 +- .../Logs/LogRecordProcessorBatch.php | 4 +- .../ComponentProvider/OpenTelemetrySdk.php | 4 +- .../Trace/SpanProcessorBatch.php | 4 +- src/Contrib/Zipkin/SpanConverter.php | 16 +- src/SDK/Common/Time/ClockFactory.php | 29 ++- src/SDK/Common/Time/ClockFactoryInterface.php | 16 -- src/SDK/Common/Time/ClockInterface.php | 17 +- src/SDK/Common/Time/StopWatch.php | 116 --------- src/SDK/Common/Time/StopWatchFactory.php | 44 ---- .../Common/Time/StopWatchFactoryInterface.php | 18 -- src/SDK/Common/Time/StopWatchInterface.php | 20 -- src/SDK/Common/Time/Util.php | 32 --- src/SDK/Logs/LogRecordProcessorFactory.php | 4 +- .../Processor/BatchLogRecordProcessor.php | 2 +- src/SDK/Metrics/Meter.php | 2 +- src/SDK/Metrics/MeterProvider.php | 2 +- src/SDK/Metrics/MeterProviderBuilder.php | 4 +- .../Metrics/MetricRegistry/MetricRegistry.php | 2 +- .../DelayedStalenessHandlerFactory.php | 2 +- src/SDK/Trace/Span.php | 12 +- .../SpanProcessor/BatchSpanProcessor.php | 2 +- .../BatchSpanProcessorBuilder.php | 4 +- src/SDK/Trace/SpanProcessorFactory.php | 4 +- tests/Benchmark/OtlpBench.php | 4 +- tests/Integration/SDK/MeterProviderTest.php | 4 +- tests/TraceContext/W3CTestService/index.php | 4 +- tests/Unit/API/Common/Time/ClockTest.php | 40 +++ .../Common/Time/SystemClockTest.php | 6 +- tests/Unit/API/Trace/NoopSpanBuilderTest.php | 2 +- .../Unit/SDK/Common/Time/ClockFactoryTest.php | 45 ---- .../SDK/Common/Time/StopWatchFactoryTest.php | 49 ---- tests/Unit/SDK/Common/Time/StopWatchTest.php | 231 ------------------ tests/Unit/SDK/Common/Time/UtilTest.php | 34 --- .../Processor/BatchLogRecordProcessorTest.php | 12 +- tests/Unit/SDK/Metrics/InstrumentTest.php | 2 +- tests/Unit/SDK/Metrics/MeterProviderTest.php | 10 +- tests/Unit/SDK/Metrics/MeterTest.php | 4 +- .../MetricFactory/StreamFactoryTest.php | 2 +- .../MetricRegistry/MetricRegistryTest.php | 2 +- .../DelayedStalenessHandlerTest.php | 2 +- tests/Unit/SDK/Trace/EventTest.php | 2 +- .../SpanProcessor/BatchSpanProcessorTest.php | 12 +- tests/Unit/SDK/Trace/SpanTest.php | 15 +- tests/Unit/SDK/Util/SpanData.php | 4 +- 59 files changed, 212 insertions(+), 735 deletions(-) create mode 100644 src/API/Common/Time/Clock.php create mode 100644 src/API/Common/Time/ClockInterface.php rename src/{SDK => API}/Common/Time/SystemClock.php (93%) rename {tests/Unit/SDK/Util => src/API/Common/Time}/TestClock.php (81%) delete mode 100644 src/SDK/Common/Time/ClockFactoryInterface.php delete mode 100644 src/SDK/Common/Time/StopWatch.php delete mode 100644 src/SDK/Common/Time/StopWatchFactory.php delete mode 100644 src/SDK/Common/Time/StopWatchFactoryInterface.php delete mode 100644 src/SDK/Common/Time/StopWatchInterface.php delete mode 100644 src/SDK/Common/Time/Util.php create mode 100644 tests/Unit/API/Common/Time/ClockTest.php rename tests/Unit/{SDK => API}/Common/Time/SystemClockTest.php (88%) delete mode 100644 tests/Unit/SDK/Common/Time/ClockFactoryTest.php delete mode 100644 tests/Unit/SDK/Common/Time/StopWatchFactoryTest.php delete mode 100644 tests/Unit/SDK/Common/Time/StopWatchTest.php delete mode 100644 tests/Unit/SDK/Common/Time/UtilTest.php diff --git a/examples/disabling_sdk.php b/examples/disabling_sdk.php index ae5ec5f1e..0eb4ff54c 100644 --- a/examples/disabling_sdk.php +++ b/examples/disabling_sdk.php @@ -4,11 +4,11 @@ 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 @@ 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( diff --git a/examples/logs/exporters/otlp_grpc.php b/examples/logs/exporters/otlp_grpc.php index d084ac372..93f76c885 100644 --- a/examples/logs/exporters/otlp_grpc.php +++ b/examples/logs/exporters/otlp_grpc.php @@ -4,6 +4,7 @@ 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\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 @@ $loggerProvider = new LoggerProvider( new BatchLogRecordProcessor( $exporter, - ClockFactory::getDefault() + Clock::getDefault() ), new InstrumentationScopeFactory( (new LogRecordLimitsBuilder())->build()->getAttributeFactory() diff --git a/examples/logs/features/batch_exporting.php b/examples/logs/features/batch_exporting.php index 80befaf47..831c628d0 100644 --- a/examples/logs/features/batch_exporting.php +++ b/examples/logs/features/batch_exporting.php @@ -4,11 +4,11 @@ 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 @@ $loggerProvider = new LoggerProvider( new BatchLogRecordProcessor( (new ConsoleExporterFactory())->create(), - ClockFactory::getDefault() + Clock::getDefault() ), new InstrumentationScopeFactory(Attributes::factory()) ); diff --git a/examples/metrics/basic.php b/examples/metrics/basic.php index ccf37a45f..33127c3f0 100644 --- a/examples/metrics/basic.php +++ b/examples/metrics/basic.php @@ -7,11 +7,11 @@ 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\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): diff --git a/examples/metrics/exporters/otlp_grpc.php b/examples/metrics/exporters/otlp_grpc.php index bb71a712e..f295d2cb3 100644 --- a/examples/metrics/exporters/otlp_grpc.php +++ b/examples/metrics/exporters/otlp_grpc.php @@ -6,14 +6,14 @@ 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( diff --git a/examples/metrics/exporters/otlp_http.php b/examples/metrics/exporters/otlp_http.php index 618a6abfc..03f381ce6 100644 --- a/examples/metrics/exporters/otlp_http.php +++ b/examples/metrics/exporters/otlp_http.php @@ -6,12 +6,12 @@ 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) diff --git a/examples/metrics/exporters/stream.php b/examples/metrics/exporters/stream.php index 06b33b771..7717063d4 100644 --- a/examples/metrics/exporters/stream.php +++ b/examples/metrics/exporters/stream.php @@ -6,12 +6,12 @@ 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( diff --git a/examples/src/ExampleMetricsGenerator.php b/examples/src/ExampleMetricsGenerator.php index 54b034d53..6eb530318 100644 --- a/examples/src/ExampleMetricsGenerator.php +++ b/examples/src/ExampleMetricsGenerator.php @@ -4,10 +4,10 @@ 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; diff --git a/examples/traces/exporters/otlp_file.php b/examples/traces/exporters/otlp_file.php index e668e44ce..770a3e3a5 100644 --- a/examples/traces/exporters/otlp_file.php +++ b/examples/traces/exporters/otlp_file.php @@ -6,10 +6,10 @@ 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 @@ $tracerProvider = new TracerProvider( new BatchSpanProcessor( $exporter, - ClockFactory::getDefault() + Clock::getDefault() ) ); $tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php'); diff --git a/examples/traces/features/batch_exporting.php b/examples/traces/features/batch_exporting.php index f92d991e5..a1153e09f 100644 --- a/examples/traces/features/batch_exporting.php +++ b/examples/traces/features/batch_exporting.php @@ -6,7 +6,7 @@ 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 @@ $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 ) diff --git a/examples/troubleshooting/logging_of_span_data.php b/examples/troubleshooting/logging_of_span_data.php index a1e42b8e9..4b71a6d9f 100644 --- a/examples/troubleshooting/logging_of_span_data.php +++ b/examples/troubleshooting/logging_of_span_data.php @@ -17,9 +17,9 @@ * 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 @@ * 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'); diff --git a/src/API/Common/Time/Clock.php b/src/API/Common/Time/Clock.php new file mode 100644 index 000000000..8ddac6481 --- /dev/null +++ b/src/API/Common/Time/Clock.php @@ -0,0 +1,25 @@ +currentEpochNanos; } - - public function nanoTime(): int - { - return $this->currentEpochNanos; - } } diff --git a/src/Config/SDK/ComponentProvider/Logs/LogRecordProcessorBatch.php b/src/Config/SDK/ComponentProvider/Logs/LogRecordProcessorBatch.php index e0b4f2ad5..780018284 100644 --- a/src/Config/SDK/ComponentProvider/Logs/LogRecordProcessorBatch.php +++ b/src/Config/SDK/ComponentProvider/Logs/LogRecordProcessorBatch.php @@ -4,11 +4,11 @@ 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 @@ public function createPlugin(array $properties, Context $context): LogRecordProc { 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'], diff --git a/src/Config/SDK/ComponentProvider/OpenTelemetrySdk.php b/src/Config/SDK/ComponentProvider/OpenTelemetrySdk.php index 7929bee03..45e35aed2 100644 --- a/src/Config/SDK/ComponentProvider/OpenTelemetrySdk.php +++ b/src/Config/SDK/ComponentProvider/OpenTelemetrySdk.php @@ -4,6 +4,7 @@ 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\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 @@ public function createPlugin(array $properties, Context $context): SdkBuilder $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 diff --git a/src/Config/SDK/ComponentProvider/Trace/SpanProcessorBatch.php b/src/Config/SDK/ComponentProvider/Trace/SpanProcessorBatch.php index f4da8f556..287192ab9 100644 --- a/src/Config/SDK/ComponentProvider/Trace/SpanProcessorBatch.php +++ b/src/Config/SDK/ComponentProvider/Trace/SpanProcessorBatch.php @@ -4,11 +4,11 @@ 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 @@ public function createPlugin(array $properties, Context $context): SpanProcessor { 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'], diff --git a/src/Contrib/Zipkin/SpanConverter.php b/src/Contrib/Zipkin/SpanConverter.php index d46cfc78d..1248a1007 100644 --- a/src/Contrib/Zipkin/SpanConverter.php +++ b/src/Contrib/Zipkin/SpanConverter.php @@ -5,10 +5,10 @@ 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 @@ public function __construct() $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 @@ private function convertSpan(SpanDataInterface $span): array { $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 @@ private static function toAnnotation(EventInterface $event): array $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) { diff --git a/src/SDK/Common/Time/ClockFactory.php b/src/SDK/Common/Time/ClockFactory.php index 33f4364f6..715c5ff39 100644 --- a/src/SDK/Common/Time/ClockFactory.php +++ b/src/SDK/Common/Time/ClockFactory.php @@ -4,27 +4,26 @@ namespace OpenTelemetry\SDK\Common\Time; -final class ClockFactory implements ClockFactoryInterface -{ - private static ?ClockInterface $default = null; - - public static function create(): self - { - return new self(); - } - - public function build(): ClockInterface - { - return new SystemClock(); - } +use OpenTelemetry\API\Common\Time\Clock; +use OpenTelemetry\API\Common\Time\ClockInterface; +/** + * @deprecated Use OpenTelemetry\API\Common\Time\Clock + * @codeCoverageIgnore + */ +class ClockFactory +{ 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(); + } } } diff --git a/src/SDK/Common/Time/ClockFactoryInterface.php b/src/SDK/Common/Time/ClockFactoryInterface.php deleted file mode 100644 index 6d9afde91..000000000 --- a/src/SDK/Common/Time/ClockFactoryInterface.php +++ /dev/null @@ -1,16 +0,0 @@ -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; - } -} diff --git a/src/SDK/Common/Time/StopWatchFactory.php b/src/SDK/Common/Time/StopWatchFactory.php deleted file mode 100644 index 6d944136a..000000000 --- a/src/SDK/Common/Time/StopWatchFactory.php +++ /dev/null @@ -1,44 +0,0 @@ -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; - } -} diff --git a/src/SDK/Common/Time/StopWatchFactoryInterface.php b/src/SDK/Common/Time/StopWatchFactoryInterface.php deleted file mode 100644 index 9750f5769..000000000 --- a/src/SDK/Common/Time/StopWatchFactoryInterface.php +++ /dev/null @@ -1,18 +0,0 @@ - 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), diff --git a/src/SDK/Logs/Processor/BatchLogRecordProcessor.php b/src/SDK/Logs/Processor/BatchLogRecordProcessor.php index 57d0944a7..8de2d0f30 100644 --- a/src/SDK/Logs/Processor/BatchLogRecordProcessor.php +++ b/src/SDK/Logs/Processor/BatchLogRecordProcessor.php @@ -6,12 +6,12 @@ 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; diff --git a/src/SDK/Metrics/Meter.php b/src/SDK/Metrics/Meter.php index 69c13566e..f13544f51 100644 --- a/src/SDK/Metrics/Meter.php +++ b/src/SDK/Metrics/Meter.php @@ -9,6 +9,7 @@ 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\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; diff --git a/src/SDK/Metrics/MeterProvider.php b/src/SDK/Metrics/MeterProvider.php index db3d19e04..4fc307f41 100644 --- a/src/SDK/Metrics/MeterProvider.php +++ b/src/SDK/Metrics/MeterProvider.php @@ -5,12 +5,12 @@ 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; diff --git a/src/SDK/Metrics/MeterProviderBuilder.php b/src/SDK/Metrics/MeterProviderBuilder.php index 17f0be895..940621bcb 100644 --- a/src/SDK/Metrics/MeterProviderBuilder.php +++ b/src/SDK/Metrics/MeterProviderBuilder.php @@ -4,9 +4,9 @@ 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 @@ public function build(): MeterProviderInterface return new MeterProvider( null, $this->resource ?? ResourceInfoFactory::emptyResource(), - ClockFactory::getDefault(), + Clock::getDefault(), Attributes::factory(), new InstrumentationScopeFactory(Attributes::factory()), $this->metricReaders, diff --git a/src/SDK/Metrics/MetricRegistry/MetricRegistry.php b/src/SDK/Metrics/MetricRegistry/MetricRegistry.php index c5da81e26..55e287be7 100644 --- a/src/SDK/Metrics/MetricRegistry/MetricRegistry.php +++ b/src/SDK/Metrics/MetricRegistry/MetricRegistry.php @@ -6,10 +6,10 @@ 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; diff --git a/src/SDK/Metrics/StalenessHandler/DelayedStalenessHandlerFactory.php b/src/SDK/Metrics/StalenessHandler/DelayedStalenessHandlerFactory.php index 08e9b181c..6e59649e9 100644 --- a/src/SDK/Metrics/StalenessHandler/DelayedStalenessHandlerFactory.php +++ b/src/SDK/Metrics/StalenessHandler/DelayedStalenessHandlerFactory.php @@ -5,7 +5,7 @@ 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; diff --git a/src/SDK/Trace/Span.php b/src/SDK/Trace/Span.php index 75d274fc9..9ef5315fa 100644 --- a/src/SDK/Trace/Span.php +++ b/src/SDK/Trace/Span.php @@ -4,6 +4,7 @@ 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\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 @@ public static function startSpan( $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 @@ public function addEvent(string $name, iterable $attributes = [], ?int $timestam 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 @@ public function recordException(Throwable $exception, iterable $attributes = [], 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 @@ public function end(int $endEpochNanos = null): void return; } - $this->endEpochNanos = $endEpochNanos ?? ClockFactory::getDefault()->now(); + $this->endEpochNanos = $endEpochNanos ?? Clock::getDefault()->now(); $this->hasEnded = true; $this->spanProcessor->onEnd($this); @@ -295,7 +295,7 @@ public function toSpanData(): SpanDataInterface /** @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 */ diff --git a/src/SDK/Trace/SpanProcessor/BatchSpanProcessor.php b/src/SDK/Trace/SpanProcessor/BatchSpanProcessor.php index c697c4238..e1a3ff438 100644 --- a/src/SDK/Trace/SpanProcessor/BatchSpanProcessor.php +++ b/src/SDK/Trace/SpanProcessor/BatchSpanProcessor.php @@ -8,12 +8,12 @@ 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; diff --git a/src/SDK/Trace/SpanProcessor/BatchSpanProcessorBuilder.php b/src/SDK/Trace/SpanProcessor/BatchSpanProcessorBuilder.php index c3229548f..5e225c35e 100644 --- a/src/SDK/Trace/SpanProcessor/BatchSpanProcessorBuilder.php +++ b/src/SDK/Trace/SpanProcessor/BatchSpanProcessorBuilder.php @@ -4,7 +4,7 @@ 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 @@ public function build(): BatchSpanProcessor { return new BatchSpanProcessor( $this->exporter, - ClockFactory::getDefault(), + Clock::getDefault(), BatchSpanProcessor::DEFAULT_MAX_QUEUE_SIZE, BatchSpanProcessor::DEFAULT_SCHEDULE_DELAY, BatchSpanProcessor::DEFAULT_EXPORT_TIMEOUT, diff --git a/src/SDK/Trace/SpanProcessorFactory.php b/src/SDK/Trace/SpanProcessorFactory.php index 3279c56ee..9b60f110a 100644 --- a/src/SDK/Trace/SpanProcessorFactory.php +++ b/src/SDK/Trace/SpanProcessorFactory.php @@ -5,10 +5,10 @@ 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 @@ public function create(?SpanExporterInterface $exporter = null, ?MeterProviderIn 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), diff --git a/tests/Benchmark/OtlpBench.php b/tests/Benchmark/OtlpBench.php index 5250ee34b..1f4c31809 100644 --- a/tests/Benchmark/OtlpBench.php +++ b/tests/Benchmark/OtlpBench.php @@ -4,6 +4,7 @@ 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\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 @@ public function setUpOtlpExporter(array $params): void $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'); } diff --git a/tests/Integration/SDK/MeterProviderTest.php b/tests/Integration/SDK/MeterProviderTest.php index 12e71836d..9e573da43 100644 --- a/tests/Integration/SDK/MeterProviderTest.php +++ b/tests/Integration/SDK/MeterProviderTest.php @@ -5,13 +5,14 @@ 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\StalenessHandler\ImmediateStalenessHandlerFactory; use OpenTelemetry\SDK\Metrics\View\CriteriaViewRegistry; use OpenTelemetry\SDK\Resource\ResourceInfoFactory; -use OpenTelemetry\Tests\Unit\SDK\Util\TestClock; use PHPUnit\Framework\TestCase; /** diff --git a/tests/TraceContext/W3CTestService/index.php b/tests/TraceContext/W3CTestService/index.php index d3cfd4be0..642180aa0 100644 --- a/tests/TraceContext/W3CTestService/index.php +++ b/tests/TraceContext/W3CTestService/index.php @@ -6,9 +6,9 @@ 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'); diff --git a/tests/Unit/API/Common/Time/ClockTest.php b/tests/Unit/API/Common/Time/ClockTest.php new file mode 100644 index 000000000..81b2c6c36 --- /dev/null +++ b/tests/Unit/API/Common/Time/ClockTest.php @@ -0,0 +1,40 @@ +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()); + } +} diff --git a/tests/Unit/SDK/Common/Time/SystemClockTest.php b/tests/Unit/API/Common/Time/SystemClockTest.php similarity index 88% rename from tests/Unit/SDK/Common/Time/SystemClockTest.php rename to tests/Unit/API/Common/Time/SystemClockTest.php index 05c3665b2..652a63203 100644 --- a/tests/Unit/SDK/Common/Time/SystemClockTest.php +++ b/tests/Unit/API/Common/Time/SystemClockTest.php @@ -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 { diff --git a/tests/Unit/API/Trace/NoopSpanBuilderTest.php b/tests/Unit/API/Trace/NoopSpanBuilderTest.php index f5c15dc66..a7ae8a35c 100644 --- a/tests/Unit/API/Trace/NoopSpanBuilderTest.php +++ b/tests/Unit/API/Trace/NoopSpanBuilderTest.php @@ -4,6 +4,7 @@ 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\Context\Context; use OpenTelemetry\Context\ContextKeys; use OpenTelemetry\Context\ContextStorageInterface; -use OpenTelemetry\Tests\Unit\SDK\Util\TestClock; use PHPUnit\Framework\TestCase; /** diff --git a/tests/Unit/SDK/Common/Time/ClockFactoryTest.php b/tests/Unit/SDK/Common/Time/ClockFactoryTest.php deleted file mode 100644 index 030d15edf..000000000 --- a/tests/Unit/SDK/Common/Time/ClockFactoryTest.php +++ /dev/null @@ -1,45 +0,0 @@ -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()); - } -} diff --git a/tests/Unit/SDK/Common/Time/StopWatchFactoryTest.php b/tests/Unit/SDK/Common/Time/StopWatchFactoryTest.php deleted file mode 100644 index ce2e53784..000000000 --- a/tests/Unit/SDK/Common/Time/StopWatchFactoryTest.php +++ /dev/null @@ -1,49 +0,0 @@ -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()); - } -} diff --git a/tests/Unit/SDK/Common/Time/StopWatchTest.php b/tests/Unit/SDK/Common/Time/StopWatchTest.php deleted file mode 100644 index e771bb28c..000000000 --- a/tests/Unit/SDK/Common/Time/StopWatchTest.php +++ /dev/null @@ -1,231 +0,0 @@ -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()); - } -} diff --git a/tests/Unit/SDK/Common/Time/UtilTest.php b/tests/Unit/SDK/Common/Time/UtilTest.php deleted file mode 100644 index 74776e2dd..000000000 --- a/tests/Unit/SDK/Common/Time/UtilTest.php +++ /dev/null @@ -1,34 +0,0 @@ -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)); - } -} diff --git a/tests/Unit/SDK/Logs/Processor/BatchLogRecordProcessorTest.php b/tests/Unit/SDK/Logs/Processor/BatchLogRecordProcessorTest.php index 44ad41eb4..57e74c4c8 100644 --- a/tests/Unit/SDK/Logs/Processor/BatchLogRecordProcessorTest.php +++ b/tests/Unit/SDK/Logs/Processor/BatchLogRecordProcessorTest.php @@ -10,12 +10,13 @@ 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\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 @@ protected function setUp(): void 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 @@ public function test_self_diagnostics(): void $processor = new BatchLogRecordProcessor( $exporter, - ClockFactory::getDefault(), + Clock::getDefault(), 2048, 5000, 30000, diff --git a/tests/Unit/SDK/Metrics/InstrumentTest.php b/tests/Unit/SDK/Metrics/InstrumentTest.php index 37ecb3806..cca7e8ad5 100644 --- a/tests/Unit/SDK/Metrics/InstrumentTest.php +++ b/tests/Unit/SDK/Metrics/InstrumentTest.php @@ -4,6 +4,7 @@ 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\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; diff --git a/tests/Unit/SDK/Metrics/MeterProviderTest.php b/tests/Unit/SDK/Metrics/MeterProviderTest.php index f23eccfd8..30d4fd9f3 100644 --- a/tests/Unit/SDK/Metrics/MeterProviderTest.php +++ b/tests/Unit/SDK/Metrics/MeterProviderTest.php @@ -4,12 +4,12 @@ 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 @@ public function test_get_meter_creates_instrumentation_scope_with_given_argument $meterProvider = new MeterProvider( null, ResourceInfoFactory::emptyResource(), - ClockFactory::getDefault(), + Clock::getDefault(), Attributes::factory(), $instrumentationScopeFactory, [], @@ -55,7 +55,7 @@ public function test_get_meter_returns_noop_meter_after_shutdown(): void $meterProvider = new MeterProvider( null, ResourceInfoFactory::emptyResource(), - ClockFactory::getDefault(), + Clock::getDefault(), Attributes::factory(), new InstrumentationScopeFactory(Attributes::factory()), [], @@ -76,7 +76,7 @@ public function test_shutdown_calls_metric_reader_shutdown(): void $meterProvider = new MeterProvider( null, ResourceInfoFactory::emptyResource(), - ClockFactory::getDefault(), + Clock::getDefault(), Attributes::factory(), new InstrumentationScopeFactory(Attributes::factory()), [$metricReader], @@ -95,7 +95,7 @@ public function test_force_flush_calls_metric_reader_force_flush(): void $meterProvider = new MeterProvider( null, ResourceInfoFactory::emptyResource(), - ClockFactory::getDefault(), + Clock::getDefault(), Attributes::factory(), new InstrumentationScopeFactory(Attributes::factory()), [$metricReader], diff --git a/tests/Unit/SDK/Metrics/MeterTest.php b/tests/Unit/SDK/Metrics/MeterTest.php index 166446c7f..37340de94 100644 --- a/tests/Unit/SDK/Metrics/MeterTest.php +++ b/tests/Unit/SDK/Metrics/MeterTest.php @@ -5,10 +5,10 @@ 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 @@ private function createMeterProviderForMetricFactory(MetricFactoryInterface $met return new MeterProvider( null, ResourceInfoFactory::emptyResource(), - ClockFactory::getDefault(), + Clock::getDefault(), Attributes::factory(), new InstrumentationScopeFactory(Attributes::factory()), $metricReaders, diff --git a/tests/Unit/SDK/Metrics/MetricFactory/StreamFactoryTest.php b/tests/Unit/SDK/Metrics/MetricFactory/StreamFactoryTest.php index bcac1f0fb..c1cf3e7fc 100644 --- a/tests/Unit/SDK/Metrics/MetricFactory/StreamFactoryTest.php +++ b/tests/Unit/SDK/Metrics/MetricFactory/StreamFactoryTest.php @@ -5,6 +5,7 @@ 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\StalenessHandlerInterface; use OpenTelemetry\SDK\Metrics\ViewProjection; use OpenTelemetry\SDK\Resource\ResourceInfoFactory; -use OpenTelemetry\Tests\Unit\SDK\Util\TestClock; use PHPUnit\Framework\TestCase; /** diff --git a/tests/Unit/SDK/Metrics/MetricRegistry/MetricRegistryTest.php b/tests/Unit/SDK/Metrics/MetricRegistry/MetricRegistryTest.php index e3fb5404e..788209d35 100644 --- a/tests/Unit/SDK/Metrics/MetricRegistry/MetricRegistryTest.php +++ b/tests/Unit/SDK/Metrics/MetricRegistry/MetricRegistryTest.php @@ -4,6 +4,7 @@ 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\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; diff --git a/tests/Unit/SDK/Metrics/StalenessHandler/DelayedStalenessHandlerTest.php b/tests/Unit/SDK/Metrics/StalenessHandler/DelayedStalenessHandlerTest.php index f8f3fc28f..46817b70e 100644 --- a/tests/Unit/SDK/Metrics/StalenessHandler/DelayedStalenessHandlerTest.php +++ b/tests/Unit/SDK/Metrics/StalenessHandler/DelayedStalenessHandlerTest.php @@ -4,8 +4,8 @@ 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; diff --git a/tests/Unit/SDK/Trace/EventTest.php b/tests/Unit/SDK/Trace/EventTest.php index dc25b7488..02eb1acf8 100644 --- a/tests/Unit/SDK/Trace/EventTest.php +++ b/tests/Unit/SDK/Trace/EventTest.php @@ -4,9 +4,9 @@ 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; /** diff --git a/tests/Unit/SDK/Trace/SpanProcessor/BatchSpanProcessorTest.php b/tests/Unit/SDK/Trace/SpanProcessor/BatchSpanProcessorTest.php index 9e0dc0fc4..7e6190708 100644 --- a/tests/Unit/SDK/Trace/SpanProcessor/BatchSpanProcessorTest.php +++ b/tests/Unit/SDK/Trace/SpanProcessor/BatchSpanProcessorTest.php @@ -11,13 +11,14 @@ 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\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 @@ protected function setUp(): void 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 @@ public function test_self_diagnostics(): void $processor = new BatchSpanProcessor( $exporter, - ClockFactory::getDefault(), + Clock::getDefault(), 2048, 5000, 30000, diff --git a/tests/Unit/SDK/Trace/SpanTest.php b/tests/Unit/SDK/Trace/SpanTest.php index 27cd1ddce..17beec2e3 100644 --- a/tests/Unit/SDK/Trace/SpanTest.php +++ b/tests/Unit/SDK/Trace/SpanTest.php @@ -11,6 +11,9 @@ 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\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\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 @@ protected function setUp():void ) ); - 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 @@ public function test_add_event(): void $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 @@ public function test_add_event(): void $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 diff --git a/tests/Unit/SDK/Util/SpanData.php b/tests/Unit/SDK/Util/SpanData.php index 1efb0db61..fcba7b218 100644 --- a/tests/Unit/SDK/Util/SpanData.php +++ b/tests/Unit/SDK/Util/SpanData.php @@ -6,12 +6,12 @@ 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 @@ public function setEvents(array $events): self 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; }