From 851049b1973fe24bdee005ef6c4f2b79b2b18179 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Mon, 13 Jun 2022 10:29:02 +1000 Subject: [PATCH 01/14] adding events per spec, "SDK implementations MUST allow end users to change the library's default error handling behavior for relevant errors" --- composer.json | 1 + examples/EventsExample.php | 68 ++++++++++++++ src/Contrib/Jaeger/JaegerTransport.php | 7 +- src/Contrib/OtlpGrpc/Exporter.php | 8 +- src/SDK/Common/Event/Dispatcher.php | 39 ++++++++ src/SDK/Common/Event/Event/DebugEvent.php | 20 ++++ src/SDK/Common/Event/Event/ErrorEvent.php | 33 +++++++ src/SDK/Common/Event/Event/WarningEvent.php | 9 ++ src/SDK/Common/Event/EventType.php | 16 ++++ .../Event/Handler/DebugEventHandler.php | 18 ++++ .../Event/Handler/ErrorEventHandler.php | 18 ++++ .../Event/Handler/WarningEventHandler.php | 18 ++++ src/SDK/Common/Event/SimpleDispatcher.php | 62 +++++++++++++ .../Common/Event/SimpleListenerProvider.php | 47 ++++++++++ src/SDK/Common/Event/StoppableEventTrait.php | 20 ++++ .../Trace/Behavior/HttpSpanExporterTrait.php | 6 ++ .../SpanProcessor/SimpleSpanProcessor.php | 5 +- src/SDK/Trace/TracerProviderFactory.php | 8 +- .../Unit/SDK/Common/Event/DispatcherTest.php | 47 ++++++++++ .../SDK/Common/Event/SimpleDispatcherTest.php | 93 +++++++++++++++++++ .../Event/SimpleListenerProviderTest.php | 83 +++++++++++++++++ .../Common/Event/StoppableEventTraitTest.php | 33 +++++++ .../SpanProcessor/MultiSpanProcessorTest.php | 2 +- .../SDK/Trace/TracerProviderFactoryTest.php | 18 ++-- 24 files changed, 662 insertions(+), 17 deletions(-) create mode 100644 examples/EventsExample.php create mode 100644 src/SDK/Common/Event/Dispatcher.php create mode 100644 src/SDK/Common/Event/Event/DebugEvent.php create mode 100644 src/SDK/Common/Event/Event/ErrorEvent.php create mode 100644 src/SDK/Common/Event/Event/WarningEvent.php create mode 100644 src/SDK/Common/Event/EventType.php create mode 100644 src/SDK/Common/Event/Handler/DebugEventHandler.php create mode 100644 src/SDK/Common/Event/Handler/ErrorEventHandler.php create mode 100644 src/SDK/Common/Event/Handler/WarningEventHandler.php create mode 100644 src/SDK/Common/Event/SimpleDispatcher.php create mode 100644 src/SDK/Common/Event/SimpleListenerProvider.php create mode 100644 src/SDK/Common/Event/StoppableEventTrait.php create mode 100644 tests/Unit/SDK/Common/Event/DispatcherTest.php create mode 100644 tests/Unit/SDK/Common/Event/SimpleDispatcherTest.php create mode 100644 tests/Unit/SDK/Common/Event/SimpleListenerProviderTest.php create mode 100644 tests/Unit/SDK/Common/Event/StoppableEventTraitTest.php diff --git a/composer.json b/composer.json index 21376c4e8..3f50bf034 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "php-http/async-client-implementation": "^1.0", "php-http/discovery": "^1.14", "promphp/prometheus_client_php": "^2.2.1", + "psr/event-dispatcher": "^1", "psr/http-factory-implementation": "^1.0", "psr/log": "^1.1|^2.0|^3.0", "symfony/polyfill-mbstring": "^1.23" diff --git a/examples/EventsExample.php b/examples/EventsExample.php new file mode 100644 index 000000000..9ff316f37 --- /dev/null +++ b/examples/EventsExample.php @@ -0,0 +1,68 @@ +setFormatter(new JsonFormatter())]) +); + +/** + * Register event listeners - this only works for SimpleEventDispatcher. For other PSR-14 implementations, you will +/* need to register all events that you are interested in, @see {SDK\Event\EventTypes} + */ +$listenerProvider = new SimpleListenerProvider(); +$listenerProvider->listen(EventType::ERROR, function (ErrorEvent $event) { + echo 'Custom handling of an error event: ' . $event->getError()->getMessage() . PHP_EOL; +}, -10); //runs before built-in handler +$listenerProvider->listen(EventType::ERROR, function (ErrorEvent $event) { + echo 'Another custom handling of an error event: ' . $event->getMessage() . PHP_EOL; + echo json_encode($event->getError()->getTrace()) . PHP_EOL; + echo 'Stopping event propagation...' . PHP_EOL; + $event->stopPropagation(); +}, 5); //runs after build-in handler +$listenerProvider->listen(EventType::ERROR, function (ErrorEvent $event) { + echo 'This will not be executed, because a high priority handler stopped event propagation.' . PHP_EOL; +}, 10); + +Dispatcher::setInstance(new SimpleDispatcher($listenerProvider)); + +//or, provide your own PSR-14 event dispatcher. This must be done before getting a tracer: +//$listenerProvider = new \Any\Psr14\ListenerProvider(); +//$listenerProvider->listen(EventType::ERROR, function(ErrorEvent $event){...}); +//DispatcherHolder::setInstance(\Any\Psr14\EventDispatcherInterface($listenerProvider)); + +$tracerProvider = new TracerProvider( + new SimpleSpanProcessor( + ZipkinExporter::fromConnectionString('http://invalid-host:9999', 'zipkin-exporter') + ) +); +$tracer = $tracerProvider->getTracer(); + +$span = $tracer->spanBuilder('root')->startSpan(); +$span->end(); diff --git a/src/Contrib/Jaeger/JaegerTransport.php b/src/Contrib/Jaeger/JaegerTransport.php index 786d20ae3..3ec31f5f7 100644 --- a/src/Contrib/Jaeger/JaegerTransport.php +++ b/src/Contrib/Jaeger/JaegerTransport.php @@ -8,14 +8,13 @@ use Jaeger\Thrift\Batch; use Jaeger\Thrift\Process; use Jaeger\Thrift\Span; -use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; +use OpenTelemetry\SDK\Common\Event\Dispatcher; +use OpenTelemetry\SDK\Common\Event\Event\ErrorEvent; use Thrift\Exception\TTransportException; use Thrift\Protocol\TCompactProtocol; final class JaegerTransport implements TransportInterface { - use LogsMessagesTrait; - // DEFAULT_BUFFER_SIZE indicates the default maximum buffer size, or the size threshold // at which the buffer will be flushed to the agent. const DEFAULT_BUFFER_SIZE = 1; @@ -93,7 +92,7 @@ public function flush($force = false): int // reset the process tag $this->process = null; } catch (TTransportException $e) { - self::logError('jaeger: transport failure: ' . $e->getMessage()); + Dispatcher::getInstance()->dispatch(new ErrorEvent('jaeger transport failure', $e)); return 0; } diff --git a/src/Contrib/OtlpGrpc/Exporter.php b/src/Contrib/OtlpGrpc/Exporter.php index 6687d2ae2..ac24c482c 100644 --- a/src/Contrib/OtlpGrpc/Exporter.php +++ b/src/Contrib/OtlpGrpc/Exporter.php @@ -4,6 +4,7 @@ namespace OpenTelemetry\Contrib\OtlpGrpc; +use Exception; use grpc; use Grpc\ChannelCredentials; use OpenTelemetry\Contrib\Otlp\ExporterTrait; @@ -12,6 +13,9 @@ use Opentelemetry\Proto\Collector\Trace\V1\TraceServiceClient; use OpenTelemetry\SDK\Common\Environment\KnownValues as Values; use OpenTelemetry\SDK\Common\Environment\Variables as Env; +use OpenTelemetry\SDK\Common\Event\Dispatcher; +use OpenTelemetry\SDK\Common\Event\Event\ErrorEvent; +use OpenTelemetry\SDK\Common\Event\Event\WarningEvent; use OpenTelemetry\SDK\Trace\Behavior\SpanExporterTrait; use OpenTelemetry\SDK\Trace\SpanExporterInterface; @@ -140,12 +144,12 @@ protected function doExport(iterable $spans): int \Grpc\STATUS_DATA_LOSS, \Grpc\STATUS_UNAUTHENTICATED, ], true)) { - self::logWarning('Retryable error exporting grpc span', ['error' => $error]); + Dispatcher::getInstance()->dispatch(new WarningEvent('Retryable error exporting grpc span', new Exception($error['error'], $error['code']))); return self::STATUS_FAILED_RETRYABLE; } - self::logError('Error exporting grpc span', ['error' => $error]); + Dispatcher::getInstance()->dispatch(new ErrorEvent('Error exporting grpc span', new Exception($error['error']))); return self::STATUS_FAILED_NOT_RETRYABLE; } diff --git a/src/SDK/Common/Event/Dispatcher.php b/src/SDK/Common/Event/Dispatcher.php new file mode 100644 index 000000000..816fb5b84 --- /dev/null +++ b/src/SDK/Common/Event/Dispatcher.php @@ -0,0 +1,39 @@ +listen(EventType::ERROR, new ErrorEventHandler()); + $dispatcher->listen(EventType::WARNING, new WarningEventHandler()); + $dispatcher->listen(EventType::DEBUG, new DebugEventHandler()); + + self::$instance = $dispatcher; + } + + return self::$instance; + } + + public static function setInstance(EventDispatcherInterface $dispatcher): void + { + self::$instance = $dispatcher; + } + + public static function unset(): void + { + self::$instance = null; + } +} diff --git a/src/SDK/Common/Event/Event/DebugEvent.php b/src/SDK/Common/Event/Event/DebugEvent.php new file mode 100644 index 000000000..d90923577 --- /dev/null +++ b/src/SDK/Common/Event/Event/DebugEvent.php @@ -0,0 +1,20 @@ +message = $message; + } + + public function getMessage(): string + { + return $this->message; + } +} diff --git a/src/SDK/Common/Event/Event/ErrorEvent.php b/src/SDK/Common/Event/Event/ErrorEvent.php new file mode 100644 index 000000000..9392c992e --- /dev/null +++ b/src/SDK/Common/Event/Event/ErrorEvent.php @@ -0,0 +1,33 @@ +message = $message; + $this->error = $error; + } + + public function getError(): Throwable + { + return $this->error; + } + + public function getMessage(): string + { + return $this->message; + } +} diff --git a/src/SDK/Common/Event/Event/WarningEvent.php b/src/SDK/Common/Event/Event/WarningEvent.php new file mode 100644 index 000000000..30b0e6e16 --- /dev/null +++ b/src/SDK/Common/Event/Event/WarningEvent.php @@ -0,0 +1,9 @@ +getMessage()); + } +} diff --git a/src/SDK/Common/Event/Handler/ErrorEventHandler.php b/src/SDK/Common/Event/Handler/ErrorEventHandler.php new file mode 100644 index 000000000..46453d404 --- /dev/null +++ b/src/SDK/Common/Event/Handler/ErrorEventHandler.php @@ -0,0 +1,18 @@ +getError()->getMessage(), ['error' => $event->getError()]); + } +} diff --git a/src/SDK/Common/Event/Handler/WarningEventHandler.php b/src/SDK/Common/Event/Handler/WarningEventHandler.php new file mode 100644 index 000000000..97fc518fe --- /dev/null +++ b/src/SDK/Common/Event/Handler/WarningEventHandler.php @@ -0,0 +1,18 @@ +getMessage(), ['error' => $event->getError()]); + } +} diff --git a/src/SDK/Common/Event/SimpleDispatcher.php b/src/SDK/Common/Event/SimpleDispatcher.php new file mode 100644 index 000000000..27ea91e8b --- /dev/null +++ b/src/SDK/Common/Event/SimpleDispatcher.php @@ -0,0 +1,62 @@ +listenerProvider = $listenerProvider; + } + + public function getListenerProvider(): ListenerProviderInterface + { + return $this->listenerProvider; + } + + public function listen(string $event, callable $listener, int $priority = 0): void + { + if (is_a($this->listenerProvider, SimpleListenerProvider::class)) { + $this->listenerProvider->listen($event, $listener, $priority); + } + /* there is no standard interface to register listeners, nor access a listener provider. Using a different listener provider + requires also setting up all required listeners for that provider */ + } + + public function dispatch(object $event): object + { + $listeners = $this->listenerProvider->getListenersForEvent($event); + + $event instanceof StoppableEventInterface + ? $this->dispatchStoppableEvent($listeners, $event) + : $this->dispatchEvent($listeners, $event); + + return $event; + } + + private function dispatchStoppableEvent(iterable $listeners, StoppableEventInterface $event): void + { + foreach ($listeners as $listener) { + if ($event->isPropagationStopped()) { + break; + } + + $listener($event); + } + } + + private function dispatchEvent(iterable $listeners, object $event): void + { + foreach ($listeners as $listener) { + $listener($event); + } + } +} diff --git a/src/SDK/Common/Event/SimpleListenerProvider.php b/src/SDK/Common/Event/SimpleListenerProvider.php new file mode 100644 index 000000000..0d2499cc4 --- /dev/null +++ b/src/SDK/Common/Event/SimpleListenerProvider.php @@ -0,0 +1,47 @@ +>> */ + private array $listeners = []; + + /** + * @psalm-suppress ArgumentTypeCoercion + */ + public function getListenersForEvent(object $event): iterable + { + foreach ($this->listeners as $key => $priority) { + if (is_a($event, $key)) { + foreach ($priority as $listeners) { + foreach ($listeners as $listener) { + yield $listener; + } + } + } + } + /* $eventListeners = $this->listeners[get_class($event)] ?? [[]]; + foreach ($eventListeners as $listeners) { + foreach ($listeners as $listener) { + yield $listener; + } + }*/ + } + + public function listen(string $event, callable $listener, int $priority = 0): void + { + /*if (!array_key_exists($event, $this->listeners)) { + $this->listeners[$event] = []; + } + if (!array_key_exists($priority, $this->listeners[$event])) { + $this->listeners[$event][$priority] = []; + }*/ + $this->listeners[$event][$priority][] = $listener; + ksort($this->listeners[$event]); + } +} diff --git a/src/SDK/Common/Event/StoppableEventTrait.php b/src/SDK/Common/Event/StoppableEventTrait.php new file mode 100644 index 000000000..8ebada6df --- /dev/null +++ b/src/SDK/Common/Event/StoppableEventTrait.php @@ -0,0 +1,20 @@ +stopped; + } + + public function stopPropagation(): void + { + $this->stopped = true; + } +} diff --git a/src/SDK/Trace/Behavior/HttpSpanExporterTrait.php b/src/SDK/Trace/Behavior/HttpSpanExporterTrait.php index 4fcda9f3e..97d1bddaa 100644 --- a/src/SDK/Trace/Behavior/HttpSpanExporterTrait.php +++ b/src/SDK/Trace/Behavior/HttpSpanExporterTrait.php @@ -6,6 +6,8 @@ use InvalidArgumentException; use JsonException; +use OpenTelemetry\SDK\Common\Event\Dispatcher; +use OpenTelemetry\SDK\Common\Event\Event\ErrorEvent; use OpenTelemetry\SDK\Trace\SpanExporterInterface; use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Client\ClientInterface; @@ -46,10 +48,14 @@ protected function doExport(iterable $spans): int try { $response = $this->dispatchSpans($spans); } catch (ClientExceptionInterface $e) { + Dispatcher::getInstance()->dispatch(new ErrorEvent('Error exporting span', $e)); + return $e instanceof RequestExceptionInterface ? SpanExporterInterface::STATUS_FAILED_NOT_RETRYABLE : SpanExporterInterface::STATUS_FAILED_RETRYABLE; } catch (Throwable $e) { + Dispatcher::getInstance()->dispatch(new ErrorEvent('Error exporting span', $e)); + return SpanExporterInterface::STATUS_FAILED_NOT_RETRYABLE; } diff --git a/src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php b/src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php index 106a1beeb..3542ed979 100644 --- a/src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php +++ b/src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php @@ -6,6 +6,8 @@ use OpenTelemetry\Context\Context; use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; +use OpenTelemetry\SDK\Common\Event\Dispatcher; +use OpenTelemetry\SDK\Common\Event\Event\DebugEvent; use OpenTelemetry\SDK\Trace\ReadableSpanInterface; use OpenTelemetry\SDK\Trace\ReadWriteSpanInterface; use OpenTelemetry\SDK\Trace\SpanExporterInterface; @@ -54,7 +56,8 @@ public function shutdown(): bool } $this->running = false; - self::logDebug('Shutting down span processor'); + Dispatcher::getInstance()->dispatch(new DebugEvent('Shutting down span processor')); + //self::logDebug('Shutting down span processor'); if (null !== $this->exporter) { return $this->forceFlush() && $this->exporter->shutdown(); diff --git a/src/SDK/Trace/TracerProviderFactory.php b/src/SDK/Trace/TracerProviderFactory.php index f27410eaa..ae5ee5fab 100644 --- a/src/SDK/Trace/TracerProviderFactory.php +++ b/src/SDK/Trace/TracerProviderFactory.php @@ -6,6 +6,8 @@ use OpenTelemetry\API\Trace as API; use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; +use OpenTelemetry\SDK\Common\Event\Dispatcher; +use OpenTelemetry\SDK\Common\Event\Event\WarningEvent; final class TracerProviderFactory { @@ -31,21 +33,21 @@ public function create(): API\TracerProviderInterface try { $exporter = $this->exporterFactory->fromEnvironment(); } catch (\Throwable $t) { - self::logWarning('Unable to create exporter', ['error' => $t]); + Dispatcher::getInstance()->dispatch(new WarningEvent('Unable to create exporter', $t)); $exporter = null; } try { $sampler = $this->samplerFactory->fromEnvironment(); } catch (\Throwable $t) { - self::logWarning('Unable to create sampler', ['error' => $t]); + Dispatcher::getInstance()->dispatch(new WarningEvent('Unable to create sampler', $t)); $sampler = null; } try { $spanProcessor = $this->spanProcessorFactory->fromEnvironment($exporter); } catch (\Throwable $t) { - self::logWarning('Unable to create span processor', ['error' => $t]); + Dispatcher::getInstance()->dispatch(new WarningEvent('Unable to create span processor', $t)); $spanProcessor = null; } diff --git a/tests/Unit/SDK/Common/Event/DispatcherTest.php b/tests/Unit/SDK/Common/Event/DispatcherTest.php new file mode 100644 index 000000000..9271e67a4 --- /dev/null +++ b/tests/Unit/SDK/Common/Event/DispatcherTest.php @@ -0,0 +1,47 @@ +assertInstanceOf(SimpleDispatcher::class, $dispatcher); + $this->assertInstanceOf(SimpleListenerProvider::class, $dispatcher->getListenerProvider()); + $this->assertGreaterThan(0, count([...$dispatcher->getListenerProvider()->getListenersForEvent($this->createMock(ErrorEvent::class))])); + $this->assertGreaterThan(0, count([...$dispatcher->getListenerProvider()->getListenersForEvent($this->createMock(WarningEvent::class))])); + $this->assertGreaterThan(0, count([...$dispatcher->getListenerProvider()->getListenersForEvent($this->createMock(DebugEvent::class))])); + } + + public function test_set_instance(): void + { + $mock = $this->createMock(EventDispatcherInterface::class); + Dispatcher::setInstance($mock); + $this->assertSame($mock, Dispatcher::getInstance()); + } +} diff --git a/tests/Unit/SDK/Common/Event/SimpleDispatcherTest.php b/tests/Unit/SDK/Common/Event/SimpleDispatcherTest.php new file mode 100644 index 000000000..4066d84fd --- /dev/null +++ b/tests/Unit/SDK/Common/Event/SimpleDispatcherTest.php @@ -0,0 +1,93 @@ +listenerProvider = $this->createMock(ListenerProviderInterface::class); + } + + public function test_get_listener_provider(): void + { + $dispatcher = new SimpleDispatcher($this->listenerProvider); + $this->assertSame($this->listenerProvider, $dispatcher->getListenerProvider()); + } + + public function test_proxies_listen_for_simple_listener_provider(): void + { + $provider = $this->createMock(SimpleListenerProvider::class); + $callable = function () { + }; + $eventName = 'my.event'; + $priority = 99; + $dispatcher = new SimpleDispatcher($provider); + $provider + ->expects($this->once()) + ->method('listen') + ->with( + $this->equalTo($eventName), + $this->equalTo($callable), + $this->equalTo($priority) + ); + $dispatcher->listen($eventName, $callable, $priority); + } + + /** + * @doesNotPerformAssertions + */ + public function test_skips_other_listener_providers(): void + { + $provider = $this->createMock(ListenerProviderInterface::class); + $dispatcher = new SimpleDispatcher($provider); + $dispatcher->listen('my.event', function () { + }); + } + + /** + * @psalm-suppress UndefinedInterfaceMethod + */ + public function test_dispatch_event(): void + { + $event = new stdClass(); + $handler = function ($receivedEvent) use ($event) { + $this->assertSame($event, $receivedEvent); + }; + $this->listenerProvider->expects($this->once())->method('getListenersForEvent')->willReturn([$handler]); //@phpstan-ignore-line + $dispatcher = new SimpleDispatcher($this->listenerProvider); + $dispatcher->dispatch($event); + } + + /** + * @psalm-suppress UndefinedInterfaceMethod + */ + public function test_dispatch_stoppable_event(): void + { + $event = $this->createMock(StoppableEventInterface::class); + $event->method('isPropagationStopped')->willReturnOnConsecutiveCalls(false, true); + $handlerOne = function (StoppableEventInterface $event) { + $this->assertTrue(true, 'handler called'); + }; + $handlerTwo = function (StoppableEventInterface $event) { + $this->fail('method should not have been called'); + }; + $this->listenerProvider->method('getListenersForEvent')->willReturn([$handlerOne, $handlerTwo]); //@phpstan-ignore-line + $dispatcher = new SimpleDispatcher($this->listenerProvider); + $dispatcher->dispatch($event); + } +} diff --git a/tests/Unit/SDK/Common/Event/SimpleListenerProviderTest.php b/tests/Unit/SDK/Common/Event/SimpleListenerProviderTest.php new file mode 100644 index 000000000..79c86f2ba --- /dev/null +++ b/tests/Unit/SDK/Common/Event/SimpleListenerProviderTest.php @@ -0,0 +1,83 @@ +provider = new SimpleListenerProvider(); + } + + public function test_add_listeners(): void + { + $event = new stdClass(); + $listenerFunction = function () { + }; + $this->provider->listen(get_class($event), $listenerFunction); + $listeners = [...$this->provider->getListenersForEvent($event)]; + $this->assertCount(1, $listeners); + $this->assertSame($listenerFunction, $listeners[0]); + } + + public function test_can_add_multiple_listeners_with_same_priority(): void + { + $event = new stdClass(); + $listenerOne = function ($event) { + }; + $listenerTwo = function ($event) { + }; + $this->provider->listen(get_class($event), $listenerOne); + $this->provider->listen(get_class($event), $listenerTwo); + $listeners = [...$this->provider->getListenersForEvent($event)]; + $this->assertCount(2, $listeners); + $this->assertSame($listenerOne, $listeners[0]); + $this->assertSame($listenerTwo, $listeners[1]); + } + + public function test_listener_priority(): void + { + $event = new stdClass(); + $listenerOne = function () { + }; + $listenerTwo = function () { + }; + $listenerThree = function () { + }; + $listenerFour = function () { + }; + $this->provider->listen(get_class($event), $listenerOne, 1); + $this->provider->listen(get_class($event), $listenerTwo, -1); + $this->provider->listen(get_class($event), $listenerThree, 0); + $this->provider->listen(get_class($event), $listenerFour, 1); + $listeners = [...$this->provider->getListenersForEvent($event)]; + $this->assertCount(4, $listeners); + $this->assertSame($listenerTwo, $listeners[0]); + $this->assertSame($listenerThree, $listeners[1]); + $this->assertSame($listenerOne, $listeners[2]); + $this->assertSame($listenerFour, $listeners[3]); + } + + public function test_get_listener_for_subclass(): void + { + $event = new stdClass(); + $subclass = $this->createMock(stdClass::class); + $listener = function () { + }; + $this->provider->listen(stdClass::class, $listener); + $listeners = [...$this->provider->getListenersForEvent($subclass)]; + $this->assertCount(1, $listeners); + $this->assertSame($listener, $listeners[0]); + } +} diff --git a/tests/Unit/SDK/Common/Event/StoppableEventTraitTest.php b/tests/Unit/SDK/Common/Event/StoppableEventTraitTest.php new file mode 100644 index 000000000..f623c0548 --- /dev/null +++ b/tests/Unit/SDK/Common/Event/StoppableEventTraitTest.php @@ -0,0 +1,33 @@ +getEvent(); + $this->assertFalse($event->isPropagationStopped()); + $event->stopPropagation(); //@phpstan-ignore-line + $this->assertTrue($event->isPropagationStopped()); + } + + public function getEvent(): StoppableEventInterface + { + return new class() implements StoppableEventInterface { + use StoppableEventTrait; + }; + } +} diff --git a/tests/Unit/SDK/Trace/SpanProcessor/MultiSpanProcessorTest.php b/tests/Unit/SDK/Trace/SpanProcessor/MultiSpanProcessorTest.php index 25e9c78c5..a22b7f07b 100644 --- a/tests/Unit/SDK/Trace/SpanProcessor/MultiSpanProcessorTest.php +++ b/tests/Unit/SDK/Trace/SpanProcessor/MultiSpanProcessorTest.php @@ -4,12 +4,12 @@ namespace OpenTelemetry\Tests\Unit\SDK\Trace\SpanProcessor; -use Monolog\Test\TestCase; use OpenTelemetry\SDK\Trace\ReadableSpanInterface; use OpenTelemetry\SDK\Trace\ReadWriteSpanInterface; use OpenTelemetry\SDK\Trace\SpanProcessor\MultiSpanProcessor; use OpenTelemetry\SDK\Trace\SpanProcessorInterface; use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; /** * @covers OpenTelemetry\SDK\Trace\SpanProcessor\MultiSpanProcessor diff --git a/tests/Unit/SDK/Trace/TracerProviderFactoryTest.php b/tests/Unit/SDK/Trace/TracerProviderFactoryTest.php index 67ca69a20..29c2b04b8 100644 --- a/tests/Unit/SDK/Trace/TracerProviderFactoryTest.php +++ b/tests/Unit/SDK/Trace/TracerProviderFactoryTest.php @@ -4,25 +4,27 @@ namespace OpenTelemetry\Tests\Unit\SDK\Trace; +use OpenTelemetry\SDK\Common\Event\Dispatcher; +use OpenTelemetry\SDK\Common\Event\Event\WarningEvent; use OpenTelemetry\SDK\Common\Log\LoggerHolder; use OpenTelemetry\SDK\Trace\ExporterFactory; use OpenTelemetry\SDK\Trace\SamplerFactory; use OpenTelemetry\SDK\Trace\SpanProcessorFactory; use OpenTelemetry\SDK\Trace\TracerProviderFactory; use PHPUnit\Framework\TestCase; -use Psr\Log\LoggerInterface; +use Psr\EventDispatcher\EventDispatcherInterface; /** * @coversDefaultClass \OpenTelemetry\SDK\Trace\TracerProviderFactory */ class TracerProviderFactoryTest extends TestCase { - private $logger; + private $dispatcher; public function setUp(): void { - $this->logger = $this->createMock(LoggerInterface::class); - LoggerHolder::set($this->logger); + $this->dispatcher = $this->createMock(EventDispatcherInterface::class); + Dispatcher::setInstance($this->dispatcher); } public function tearDown(): void @@ -51,7 +53,7 @@ public function test_factory_creates_tracer(): void /** * @covers ::create */ - public function test_factory_logs_warnings_and_continues(): void + public function test_factory_emits_warnings_and_continues(): void { $exporterFactory = $this->createMock(ExporterFactory::class); $samplerFactory = $this->createMock(SamplerFactory::class); @@ -66,7 +68,11 @@ public function test_factory_logs_warnings_and_continues(): void $spanProcessorFactory->expects($this->once()) ->method('fromEnvironment') ->willThrowException(new \InvalidArgumentException('foo')); - $this->logger->expects($this->atLeast(3))->method('log'); + $this->dispatcher->expects($this->atLeast(3))->method('dispatch')->with($this->callback(function ($event) { + $this->assertInstanceOf(WarningEvent::class, $event); + + return true; + })); $factory = new TracerProviderFactory('test', $exporterFactory, $samplerFactory, $spanProcessorFactory); $factory->create(); From 195eabbbd48f6060396a994c24775bf8a542bc3f Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Mon, 13 Jun 2022 12:12:40 +1000 Subject: [PATCH 02/14] static analysis fixes --- src/SDK/Common/Event/Event/DebugEvent.php | 9 +++++- src/SDK/Common/Event/Event/ErrorEvent.php | 4 +-- src/SDK/Common/Event/Event/WarningEvent.php | 31 ++++++++++++++++++- .../SDK/Common/Event/Event/DebugEventTest.php | 23 ++++++++++++++ .../SDK/Common/Event/Event/ErrorEventTest.php | 23 ++++++++++++++ .../Common/Event/Handler/DebugHandlerTest.php | 27 ++++++++++++++++ .../Common/Event/Handler/ErrorHandlerTest.php | 28 +++++++++++++++++ .../Event/Handler/LogBasedHandlerTest.php | 27 ++++++++++++++++ .../Event/Handler/WarningHandlerTest.php | 28 +++++++++++++++++ tests/Unit/SDK/Trace/SpanTest.php | 1 + 10 files changed, 197 insertions(+), 4 deletions(-) create mode 100644 tests/Unit/SDK/Common/Event/Event/DebugEventTest.php create mode 100644 tests/Unit/SDK/Common/Event/Event/ErrorEventTest.php create mode 100644 tests/Unit/SDK/Common/Event/Handler/DebugHandlerTest.php create mode 100644 tests/Unit/SDK/Common/Event/Handler/ErrorHandlerTest.php create mode 100644 tests/Unit/SDK/Common/Event/Handler/LogBasedHandlerTest.php create mode 100644 tests/Unit/SDK/Common/Event/Handler/WarningHandlerTest.php diff --git a/src/SDK/Common/Event/Event/DebugEvent.php b/src/SDK/Common/Event/Event/DebugEvent.php index d90923577..360fb6fae 100644 --- a/src/SDK/Common/Event/Event/DebugEvent.php +++ b/src/SDK/Common/Event/Event/DebugEvent.php @@ -7,14 +7,21 @@ class DebugEvent { private string $message; + private array $extra; - public function __construct(string $message) + public function __construct(string $message, array $extra = []) { $this->message = $message; + $this->extra = $extra; } public function getMessage(): string { return $this->message; } + + public function getExtra(): array + { + return $this->extra; + } } diff --git a/src/SDK/Common/Event/Event/ErrorEvent.php b/src/SDK/Common/Event/Event/ErrorEvent.php index 9392c992e..ab59acb20 100644 --- a/src/SDK/Common/Event/Event/ErrorEvent.php +++ b/src/SDK/Common/Event/Event/ErrorEvent.php @@ -12,8 +12,8 @@ class ErrorEvent implements StoppableEventInterface { use StoppableEventTrait; - private string $message; - private Throwable $error; + protected string $message; + protected Throwable $error; public function __construct(string $message, Throwable $error) { diff --git a/src/SDK/Common/Event/Event/WarningEvent.php b/src/SDK/Common/Event/Event/WarningEvent.php index 30b0e6e16..0503d7819 100644 --- a/src/SDK/Common/Event/Event/WarningEvent.php +++ b/src/SDK/Common/Event/Event/WarningEvent.php @@ -4,6 +4,35 @@ namespace OpenTelemetry\SDK\Common\Event\Event; -class WarningEvent extends ErrorEvent +use OpenTelemetry\SDK\Common\Event\StoppableEventTrait; +use Psr\EventDispatcher\StoppableEventInterface; +use Throwable; + +class WarningEvent implements StoppableEventInterface { + use StoppableEventTrait; + + protected string $message; + protected ?Throwable $error = null; + + public function __construct(string $message, ?Throwable $error) + { + $this->message = $message; + $this->error = $error; + } + + public function getError(): ?Throwable + { + return $this->error; + } + + public function hasError(): bool + { + return $this->error !== null; + } + + public function getMessage(): string + { + return $this->message; + } } diff --git a/tests/Unit/SDK/Common/Event/Event/DebugEventTest.php b/tests/Unit/SDK/Common/Event/Event/DebugEventTest.php new file mode 100644 index 000000000..1cb0986c1 --- /dev/null +++ b/tests/Unit/SDK/Common/Event/Event/DebugEventTest.php @@ -0,0 +1,23 @@ + 'baz']; + $event = new DebugEvent($message, $extra); + $this->assertSame($message, $event->getMessage()); + $this->assertSame($extra, $event->getExtra()); + } +} diff --git a/tests/Unit/SDK/Common/Event/Event/ErrorEventTest.php b/tests/Unit/SDK/Common/Event/Event/ErrorEventTest.php new file mode 100644 index 000000000..5fcddf776 --- /dev/null +++ b/tests/Unit/SDK/Common/Event/Event/ErrorEventTest.php @@ -0,0 +1,23 @@ +assertSame($message, $event->getMessage()); + $this->assertSame($exception, $event->getError()); + } +} diff --git a/tests/Unit/SDK/Common/Event/Handler/DebugHandlerTest.php b/tests/Unit/SDK/Common/Event/Handler/DebugHandlerTest.php new file mode 100644 index 000000000..ad9eb2ec1 --- /dev/null +++ b/tests/Unit/SDK/Common/Event/Handler/DebugHandlerTest.php @@ -0,0 +1,27 @@ +logger->expects($this->once())->method('log')->with($this->equalTo(LogLevel::DEBUG)); + $handler($event); + } +} diff --git a/tests/Unit/SDK/Common/Event/Handler/ErrorHandlerTest.php b/tests/Unit/SDK/Common/Event/Handler/ErrorHandlerTest.php new file mode 100644 index 000000000..8b5698bb8 --- /dev/null +++ b/tests/Unit/SDK/Common/Event/Handler/ErrorHandlerTest.php @@ -0,0 +1,28 @@ +logger->expects($this->once())->method('log')->with($this->equalTo(LogLevel::ERROR)); + $handler($event); + } +} diff --git a/tests/Unit/SDK/Common/Event/Handler/LogBasedHandlerTest.php b/tests/Unit/SDK/Common/Event/Handler/LogBasedHandlerTest.php new file mode 100644 index 000000000..fada9155b --- /dev/null +++ b/tests/Unit/SDK/Common/Event/Handler/LogBasedHandlerTest.php @@ -0,0 +1,27 @@ +logger = $this->createMock(LoggerInterface::class); + LoggerHolder::set($this->logger); + } + + public function tearDown(): void + { + LoggerHolder::unset(); + } +} diff --git a/tests/Unit/SDK/Common/Event/Handler/WarningHandlerTest.php b/tests/Unit/SDK/Common/Event/Handler/WarningHandlerTest.php new file mode 100644 index 000000000..b38aaf328 --- /dev/null +++ b/tests/Unit/SDK/Common/Event/Handler/WarningHandlerTest.php @@ -0,0 +1,28 @@ +logger->expects($this->once())->method('log')->with($this->equalTo(LogLevel::WARNING)); + $handler($event); + } +} diff --git a/tests/Unit/SDK/Trace/SpanTest.php b/tests/Unit/SDK/Trace/SpanTest.php index 1d551086f..cef953d4a 100644 --- a/tests/Unit/SDK/Trace/SpanTest.php +++ b/tests/Unit/SDK/Trace/SpanTest.php @@ -41,6 +41,7 @@ /** * @covers OpenTelemetry\SDK\Trace\Span + * @coversDefaultClass \OpenTelemetry\SDK\Trace\Span */ class SpanTest extends MockeryTestCase { From 2093b2c20cd117fa78922614fcff1b77a2d7d648 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Mon, 13 Jun 2022 12:18:24 +1000 Subject: [PATCH 03/14] remove commented-out code --- src/SDK/Common/Event/SimpleListenerProvider.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/SDK/Common/Event/SimpleListenerProvider.php b/src/SDK/Common/Event/SimpleListenerProvider.php index 0d2499cc4..86fb99abc 100644 --- a/src/SDK/Common/Event/SimpleListenerProvider.php +++ b/src/SDK/Common/Event/SimpleListenerProvider.php @@ -25,22 +25,10 @@ public function getListenersForEvent(object $event): iterable } } } - /* $eventListeners = $this->listeners[get_class($event)] ?? [[]]; - foreach ($eventListeners as $listeners) { - foreach ($listeners as $listener) { - yield $listener; - } - }*/ } public function listen(string $event, callable $listener, int $priority = 0): void { - /*if (!array_key_exists($event, $this->listeners)) { - $this->listeners[$event] = []; - } - if (!array_key_exists($priority, $this->listeners[$event])) { - $this->listeners[$event][$priority] = []; - }*/ $this->listeners[$event][$priority][] = $listener; ksort($this->listeners[$event]); } From 85dd9ef7bc63a75013ecd11c4c2c2ea24be87f89 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Mon, 13 Jun 2022 14:02:57 +1000 Subject: [PATCH 04/14] self-review, adding tests and tidying --- src/SDK/Common/Event/Event/WarningEvent.php | 2 +- .../Trace/Behavior/HttpSpanExporterTrait.php | 4 +- .../Common/Event/Event/WarningEventTest.php | 31 +++++++ .../Behavior/HttpSpanExporterTraitTest.php | 83 +++++++++++++++++++ 4 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 tests/Unit/SDK/Common/Event/Event/WarningEventTest.php create mode 100644 tests/Unit/SDK/Trace/Behavior/HttpSpanExporterTraitTest.php diff --git a/src/SDK/Common/Event/Event/WarningEvent.php b/src/SDK/Common/Event/Event/WarningEvent.php index 0503d7819..94761d2e2 100644 --- a/src/SDK/Common/Event/Event/WarningEvent.php +++ b/src/SDK/Common/Event/Event/WarningEvent.php @@ -15,7 +15,7 @@ class WarningEvent implements StoppableEventInterface protected string $message; protected ?Throwable $error = null; - public function __construct(string $message, ?Throwable $error) + public function __construct(string $message, ?Throwable $error = null) { $this->message = $message; $this->error = $error; diff --git a/src/SDK/Trace/Behavior/HttpSpanExporterTrait.php b/src/SDK/Trace/Behavior/HttpSpanExporterTrait.php index 97d1bddaa..2566f21d5 100644 --- a/src/SDK/Trace/Behavior/HttpSpanExporterTrait.php +++ b/src/SDK/Trace/Behavior/HttpSpanExporterTrait.php @@ -4,6 +4,7 @@ namespace OpenTelemetry\SDK\Trace\Behavior; +use Exception; use InvalidArgumentException; use JsonException; use OpenTelemetry\SDK\Common\Event\Dispatcher; @@ -19,8 +20,6 @@ use Psr\Http\Message\StreamInterface; use Throwable; -//use Throwable; - /** * @phan-file-suppress PhanTypeInvalidThrowsIsInterface */ @@ -60,6 +59,7 @@ protected function doExport(iterable $spans): int } if ($response->getStatusCode() >= 400) { + Dispatcher::getInstance()->dispatch(new ErrorEvent('40x error exporting span', new Exception('', $response->getStatusCode()))); return $response->getStatusCode() < 500 ? SpanExporterInterface::STATUS_FAILED_NOT_RETRYABLE : SpanExporterInterface::STATUS_FAILED_RETRYABLE; diff --git a/tests/Unit/SDK/Common/Event/Event/WarningEventTest.php b/tests/Unit/SDK/Common/Event/Event/WarningEventTest.php new file mode 100644 index 000000000..560a30d4e --- /dev/null +++ b/tests/Unit/SDK/Common/Event/Event/WarningEventTest.php @@ -0,0 +1,31 @@ +assertSame($message, $event->getMessage()); + $this->assertSame($exception, $event->getError()); + $this->assertTrue($event->hasError()); + } + + public function test_warning_event_without_throwable(): void + { + $message = 'foo'; + $event = new WarningEvent($message); + $this->assertSame($message, $event->getMessage()); + $this->assertFalse($event->hasError()); + $this->assertNull($event->getError()); + } +} diff --git a/tests/Unit/SDK/Trace/Behavior/HttpSpanExporterTraitTest.php b/tests/Unit/SDK/Trace/Behavior/HttpSpanExporterTraitTest.php new file mode 100644 index 000000000..7bebbeb15 --- /dev/null +++ b/tests/Unit/SDK/Trace/Behavior/HttpSpanExporterTraitTest.php @@ -0,0 +1,83 @@ +dispatcher = $this->createMock(EventDispatcherInterface::class); + $request = $this->createMock(RequestInterface::class); + $this->client = $this->createMock(ClientInterface::class); + $this->exporter = $this->createExporter(); + $this->exporter->setRequest($request); + $this->exporter->setClient($this->client); + Dispatcher::setInstance($this->dispatcher); + } + + public function tearDown(): void + { + Dispatcher::unset(); + } + + public function test_export_with_client_exception_generates_error_event(): void + { + $this->client->expects($this->once())->method('sendRequest')->willThrowException($this->createMock(ClientException::class)); + $this->dispatcher->expects($this->once())->method('dispatch'); + $this->exporter->export([$this->createMock(SpanInterface::class)]); + } + + public function test_export_with_throwable_generates_error_event(): void + { + $this->client->expects($this->once())->method('sendRequest')->willThrowException($this->createMock(Throwable::class)); + $this->dispatcher->expects($this->once())->method('dispatch'); + $this->exporter->export([$this->createMock(SpanInterface::class)]); + } + + private function createExporter(): SpanExporterInterface + { + return new class extends TestCase implements SpanExporterInterface { + use HttpSpanExporterTrait; + private RequestInterface $request; + public function setRequest(RequestInterface $request): void + { + $this->request = $request; + } + public function setClient(ClientInterface $client): void + { + $this->client = $client; + } + + public function serializeTrace(iterable $spans): string + { + return ''; + } + public static function fromConnectionString(string $endpointUrl, string $name, string $args) + { + return new \stdClass(); + } + public function marshallRequest(iterable $spans): RequestInterface + { + return $this->request; + } + }; + } +} From bba3ac0b6673fbb817a13ace1a40a7537c715da2 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Mon, 13 Jun 2022 14:18:09 +1000 Subject: [PATCH 05/14] making analysis happy --- .../Trace/Behavior/HttpSpanExporterTrait.php | 1 + .../Common/Event/Event/WarningEventTest.php | 2 ++ .../Behavior/HttpSpanExporterTraitTest.php | 32 ++++++++++++------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/SDK/Trace/Behavior/HttpSpanExporterTrait.php b/src/SDK/Trace/Behavior/HttpSpanExporterTrait.php index 2566f21d5..bb78d0636 100644 --- a/src/SDK/Trace/Behavior/HttpSpanExporterTrait.php +++ b/src/SDK/Trace/Behavior/HttpSpanExporterTrait.php @@ -60,6 +60,7 @@ protected function doExport(iterable $spans): int if ($response->getStatusCode() >= 400) { Dispatcher::getInstance()->dispatch(new ErrorEvent('40x error exporting span', new Exception('', $response->getStatusCode()))); + return $response->getStatusCode() < 500 ? SpanExporterInterface::STATUS_FAILED_NOT_RETRYABLE : SpanExporterInterface::STATUS_FAILED_RETRYABLE; diff --git a/tests/Unit/SDK/Common/Event/Event/WarningEventTest.php b/tests/Unit/SDK/Common/Event/Event/WarningEventTest.php index 560a30d4e..c45c02644 100644 --- a/tests/Unit/SDK/Common/Event/Event/WarningEventTest.php +++ b/tests/Unit/SDK/Common/Event/Event/WarningEventTest.php @@ -1,5 +1,7 @@ dispatcher = $this->createMock(EventDispatcherInterface::class); $request = $this->createMock(RequestInterface::class); $this->client = $this->createMock(ClientInterface::class); $this->exporter = $this->createExporter(); - $this->exporter->setRequest($request); - $this->exporter->setClient($this->client); + $this->exporter->setRequest($request); //@phpstan-ignore-line + $this->exporter->setClient($this->client); //@phpstan-ignore-line Dispatcher::setInstance($this->dispatcher); } @@ -38,23 +43,29 @@ public function tearDown(): void Dispatcher::unset(); } + /** + * @psalm-suppress UndefinedInterfaceMethod + */ public function test_export_with_client_exception_generates_error_event(): void { - $this->client->expects($this->once())->method('sendRequest')->willThrowException($this->createMock(ClientException::class)); - $this->dispatcher->expects($this->once())->method('dispatch'); - $this->exporter->export([$this->createMock(SpanInterface::class)]); + $this->client->expects($this->once())->method('sendRequest')->willThrowException($this->createMock(ClientException::class)); //@phpstan-ignore-line + $this->dispatcher->expects($this->once())->method('dispatch'); //@phpstan-ignore-line + $this->exporter->export([$this->createMock(SpanDataInterface::class)]); } + /** + * @psalm-suppress UndefinedInterfaceMethod + */ public function test_export_with_throwable_generates_error_event(): void { - $this->client->expects($this->once())->method('sendRequest')->willThrowException($this->createMock(Throwable::class)); - $this->dispatcher->expects($this->once())->method('dispatch'); - $this->exporter->export([$this->createMock(SpanInterface::class)]); + $this->client->expects($this->once())->method('sendRequest')->willThrowException($this->createMock(Throwable::class)); //@phpstan-ignore-line + $this->dispatcher->expects($this->once())->method('dispatch'); //@phpstan-ignore-line + $this->exporter->export([$this->createMock(SpanDataInterface::class)]); } private function createExporter(): SpanExporterInterface { - return new class extends TestCase implements SpanExporterInterface { + return new class() implements SpanExporterInterface { use HttpSpanExporterTrait; private RequestInterface $request; public function setRequest(RequestInterface $request): void @@ -65,7 +76,6 @@ public function setClient(ClientInterface $client): void { $this->client = $client; } - public function serializeTrace(iterable $spans): string { return ''; From 265bdc2cdfbfb6d5d8b52f7e5c7f2a2e3a8d3e16 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Mon, 13 Jun 2022 14:38:25 +1000 Subject: [PATCH 06/14] adding tests for SpanLimits --- tests/Unit/SDK/Trace/SpanLimitsTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tests/Unit/SDK/Trace/SpanLimitsTest.php diff --git a/tests/Unit/SDK/Trace/SpanLimitsTest.php b/tests/Unit/SDK/Trace/SpanLimitsTest.php new file mode 100644 index 000000000..adcf93a33 --- /dev/null +++ b/tests/Unit/SDK/Trace/SpanLimitsTest.php @@ -0,0 +1,25 @@ +assertSame(1, $spanLimits->getAttributeLimits()->getAttributeCountLimit()); + $this->assertSame(2, $spanLimits->getAttributeLimits()->getAttributeValueLengthLimit()); + $this->assertSame(3, $spanLimits->getEventCountLimit()); + $this->assertSame(4, $spanLimits->getLinkCountLimit()); + $this->assertSame(5, $spanLimits->getAttributePerEventCountLimit()); + $this->assertSame(6, $spanLimits->getAttributePerLinkCountLimit()); + } +} From f650b8de17518262cf2ff4605b7b114fda6bb2e6 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Mon, 13 Jun 2022 14:54:00 +1000 Subject: [PATCH 07/14] tidy --- src/SDK/Common/Event/Handler/DebugEventHandler.php | 2 +- src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/SDK/Common/Event/Handler/DebugEventHandler.php b/src/SDK/Common/Event/Handler/DebugEventHandler.php index d1952b622..1a63d6ec5 100644 --- a/src/SDK/Common/Event/Handler/DebugEventHandler.php +++ b/src/SDK/Common/Event/Handler/DebugEventHandler.php @@ -13,6 +13,6 @@ class DebugEventHandler public function __invoke(DebugEvent $event): void { - self::logDebug($event->getMessage()); + self::logDebug($event->getMessage(), $event->getExtra()); } } diff --git a/src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php b/src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php index 3542ed979..36cf5a22e 100644 --- a/src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php +++ b/src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php @@ -57,7 +57,6 @@ public function shutdown(): bool $this->running = false; Dispatcher::getInstance()->dispatch(new DebugEvent('Shutting down span processor')); - //self::logDebug('Shutting down span processor'); if (null !== $this->exporter) { return $this->forceFlush() && $this->exporter->shutdown(); From 336f48129f2ef351b9d77735afa9779a6ab5f702 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Mon, 13 Jun 2022 21:27:32 +1000 Subject: [PATCH 08/14] reorder assertions --- tests/Unit/SDK/Common/Event/Event/WarningEventTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/SDK/Common/Event/Event/WarningEventTest.php b/tests/Unit/SDK/Common/Event/Event/WarningEventTest.php index c45c02644..6a85da9ae 100644 --- a/tests/Unit/SDK/Common/Event/Event/WarningEventTest.php +++ b/tests/Unit/SDK/Common/Event/Event/WarningEventTest.php @@ -18,8 +18,8 @@ public function test_warning_event_with_throwable(): void $exception = new \Exception(); $event = new WarningEvent($message, $exception); $this->assertSame($message, $event->getMessage()); - $this->assertSame($exception, $event->getError()); $this->assertTrue($event->hasError()); + $this->assertSame($exception, $event->getError()); } public function test_warning_event_without_throwable(): void From 026f4c8bfaf4f0218bfd86a47af2d144cf94bb32 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Tue, 14 Jun 2022 10:18:19 +1000 Subject: [PATCH 09/14] error -> exception --- examples/EventsExample.php | 4 ++-- src/SDK/Common/Event/Event/ErrorEvent.php | 8 ++++---- src/SDK/Common/Event/Event/WarningEvent.php | 12 ++++++------ src/SDK/Common/Event/Handler/ErrorEventHandler.php | 2 +- src/SDK/Common/Event/Handler/WarningEventHandler.php | 2 +- tests/Unit/SDK/Common/Event/Event/ErrorEventTest.php | 2 +- .../Unit/SDK/Common/Event/Event/WarningEventTest.php | 4 ++-- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/examples/EventsExample.php b/examples/EventsExample.php index 9ff316f37..d1c46c47a 100644 --- a/examples/EventsExample.php +++ b/examples/EventsExample.php @@ -38,11 +38,11 @@ */ $listenerProvider = new SimpleListenerProvider(); $listenerProvider->listen(EventType::ERROR, function (ErrorEvent $event) { - echo 'Custom handling of an error event: ' . $event->getError()->getMessage() . PHP_EOL; + echo 'Custom handling of an error event: ' . $event->getException()->getMessage() . PHP_EOL; }, -10); //runs before built-in handler $listenerProvider->listen(EventType::ERROR, function (ErrorEvent $event) { echo 'Another custom handling of an error event: ' . $event->getMessage() . PHP_EOL; - echo json_encode($event->getError()->getTrace()) . PHP_EOL; + echo json_encode($event->getException()->getTrace()) . PHP_EOL; echo 'Stopping event propagation...' . PHP_EOL; $event->stopPropagation(); }, 5); //runs after build-in handler diff --git a/src/SDK/Common/Event/Event/ErrorEvent.php b/src/SDK/Common/Event/Event/ErrorEvent.php index ab59acb20..726c0e241 100644 --- a/src/SDK/Common/Event/Event/ErrorEvent.php +++ b/src/SDK/Common/Event/Event/ErrorEvent.php @@ -13,17 +13,17 @@ class ErrorEvent implements StoppableEventInterface use StoppableEventTrait; protected string $message; - protected Throwable $error; + protected Throwable $exception; public function __construct(string $message, Throwable $error) { $this->message = $message; - $this->error = $error; + $this->exception = $error; } - public function getError(): Throwable + public function getException(): Throwable { - return $this->error; + return $this->exception; } public function getMessage(): string diff --git a/src/SDK/Common/Event/Event/WarningEvent.php b/src/SDK/Common/Event/Event/WarningEvent.php index 94761d2e2..cbbcee2a7 100644 --- a/src/SDK/Common/Event/Event/WarningEvent.php +++ b/src/SDK/Common/Event/Event/WarningEvent.php @@ -13,22 +13,22 @@ class WarningEvent implements StoppableEventInterface use StoppableEventTrait; protected string $message; - protected ?Throwable $error = null; + protected ?Throwable $exception = null; - public function __construct(string $message, ?Throwable $error = null) + public function __construct(string $message, ?Throwable $exception = null) { $this->message = $message; - $this->error = $error; + $this->exception = $exception; } - public function getError(): ?Throwable + public function getException(): ?Throwable { - return $this->error; + return $this->exception; } public function hasError(): bool { - return $this->error !== null; + return $this->exception !== null; } public function getMessage(): string diff --git a/src/SDK/Common/Event/Handler/ErrorEventHandler.php b/src/SDK/Common/Event/Handler/ErrorEventHandler.php index 46453d404..77b879f42 100644 --- a/src/SDK/Common/Event/Handler/ErrorEventHandler.php +++ b/src/SDK/Common/Event/Handler/ErrorEventHandler.php @@ -13,6 +13,6 @@ class ErrorEventHandler public function __invoke(ErrorEvent $event): void { - self::logError($event->getError()->getMessage(), ['error' => $event->getError()]); + self::logError($event->getMessage(), ['exception' => $event->getException()]); } } diff --git a/src/SDK/Common/Event/Handler/WarningEventHandler.php b/src/SDK/Common/Event/Handler/WarningEventHandler.php index 97fc518fe..07c073262 100644 --- a/src/SDK/Common/Event/Handler/WarningEventHandler.php +++ b/src/SDK/Common/Event/Handler/WarningEventHandler.php @@ -13,6 +13,6 @@ class WarningEventHandler public function __invoke(WarningEvent $event): void { - self::logWarning($event->getMessage(), ['error' => $event->getError()]); + self::logWarning($event->getMessage(), ['exception' => $event->getException()]); } } diff --git a/tests/Unit/SDK/Common/Event/Event/ErrorEventTest.php b/tests/Unit/SDK/Common/Event/Event/ErrorEventTest.php index 5fcddf776..3b39b1cbe 100644 --- a/tests/Unit/SDK/Common/Event/Event/ErrorEventTest.php +++ b/tests/Unit/SDK/Common/Event/Event/ErrorEventTest.php @@ -18,6 +18,6 @@ public function test_error_event(): void $exception = new \Exception(); $event = new ErrorEvent($message, $exception); $this->assertSame($message, $event->getMessage()); - $this->assertSame($exception, $event->getError()); + $this->assertSame($exception, $event->getException()); } } diff --git a/tests/Unit/SDK/Common/Event/Event/WarningEventTest.php b/tests/Unit/SDK/Common/Event/Event/WarningEventTest.php index 6a85da9ae..1aa876ccb 100644 --- a/tests/Unit/SDK/Common/Event/Event/WarningEventTest.php +++ b/tests/Unit/SDK/Common/Event/Event/WarningEventTest.php @@ -19,7 +19,7 @@ public function test_warning_event_with_throwable(): void $event = new WarningEvent($message, $exception); $this->assertSame($message, $event->getMessage()); $this->assertTrue($event->hasError()); - $this->assertSame($exception, $event->getError()); + $this->assertSame($exception, $event->getException()); } public function test_warning_event_without_throwable(): void @@ -28,6 +28,6 @@ public function test_warning_event_without_throwable(): void $event = new WarningEvent($message); $this->assertSame($message, $event->getMessage()); $this->assertFalse($event->hasError()); - $this->assertNull($event->getError()); + $this->assertNull($event->getException()); } } From 0a91c30d780dc2b6baa998e0b1f6cdf792740836 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Wed, 15 Jun 2022 09:45:28 +1000 Subject: [PATCH 10/14] moving event emitting logic into a trait --- src/Contrib/Jaeger/HttpSender.php | 3 --- src/Contrib/Jaeger/JaegerTransport.php | 6 ++++-- src/Contrib/Otlp/ExporterTrait.php | 2 -- src/Contrib/OtlpGrpc/Exporter.php | 10 ++++++---- src/SDK/Behavior/EmitsEventsTrait.php | 15 +++++++++++++++ src/SDK/Trace/Behavior/HttpSpanExporterTrait.php | 9 +++++---- .../Trace/SpanProcessor/SimpleSpanProcessor.php | 7 +++---- src/SDK/Trace/TracerProviderFactory.php | 11 +++++------ 8 files changed, 38 insertions(+), 25 deletions(-) create mode 100644 src/SDK/Behavior/EmitsEventsTrait.php diff --git a/src/Contrib/Jaeger/HttpSender.php b/src/Contrib/Jaeger/HttpSender.php index 60d8e5bb3..87426d5d6 100644 --- a/src/Contrib/Jaeger/HttpSender.php +++ b/src/Contrib/Jaeger/HttpSender.php @@ -9,7 +9,6 @@ use OpenTelemetry\Contrib\Jaeger\BatchAdapter\BatchAdapterFactoryInterface; use OpenTelemetry\Contrib\Jaeger\BatchAdapter\BatchAdapterInterface; use OpenTelemetry\Contrib\Jaeger\TagFactory\TagFactory; -use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; use OpenTelemetry\SDK\Resource\ResourceInfo; use OpenTelemetry\SemConv\ResourceAttributes; use Psr\Http\Client\ClientInterface; @@ -20,8 +19,6 @@ class HttpSender { - use LogsMessagesTrait; - private string $serviceName; private TProtocol $protocol; diff --git a/src/Contrib/Jaeger/JaegerTransport.php b/src/Contrib/Jaeger/JaegerTransport.php index 3ec31f5f7..a33ca9bc8 100644 --- a/src/Contrib/Jaeger/JaegerTransport.php +++ b/src/Contrib/Jaeger/JaegerTransport.php @@ -8,13 +8,15 @@ use Jaeger\Thrift\Batch; use Jaeger\Thrift\Process; use Jaeger\Thrift\Span; -use OpenTelemetry\SDK\Common\Event\Dispatcher; +use OpenTelemetry\SDK\Behavior\EmitsEventsTrait; use OpenTelemetry\SDK\Common\Event\Event\ErrorEvent; use Thrift\Exception\TTransportException; use Thrift\Protocol\TCompactProtocol; final class JaegerTransport implements TransportInterface { + use EmitsEventsTrait; + // DEFAULT_BUFFER_SIZE indicates the default maximum buffer size, or the size threshold // at which the buffer will be flushed to the agent. const DEFAULT_BUFFER_SIZE = 1; @@ -92,7 +94,7 @@ public function flush($force = false): int // reset the process tag $this->process = null; } catch (TTransportException $e) { - Dispatcher::getInstance()->dispatch(new ErrorEvent('jaeger transport failure', $e)); + self::emit(new ErrorEvent('jaeger transport failure', $e)); return 0; } diff --git a/src/Contrib/Otlp/ExporterTrait.php b/src/Contrib/Otlp/ExporterTrait.php index 4f279f852..26d477bd5 100644 --- a/src/Contrib/Otlp/ExporterTrait.php +++ b/src/Contrib/Otlp/ExporterTrait.php @@ -4,13 +4,11 @@ namespace OpenTelemetry\Contrib\Otlp; -use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; use OpenTelemetry\SDK\Common\Environment\EnvironmentVariablesTrait; use OpenTelemetry\SDK\Trace\Behavior\UsesSpanConverterTrait; trait ExporterTrait { use EnvironmentVariablesTrait; - use LogsMessagesTrait; use UsesSpanConverterTrait; } diff --git a/src/Contrib/OtlpGrpc/Exporter.php b/src/Contrib/OtlpGrpc/Exporter.php index ac24c482c..fc0d4e596 100644 --- a/src/Contrib/OtlpGrpc/Exporter.php +++ b/src/Contrib/OtlpGrpc/Exporter.php @@ -11,9 +11,10 @@ use OpenTelemetry\Contrib\Otlp\SpanConverter; use Opentelemetry\Proto\Collector\Trace\V1\ExportTraceServiceRequest; use Opentelemetry\Proto\Collector\Trace\V1\TraceServiceClient; +use OpenTelemetry\SDK\Behavior\EmitsEventsTrait; use OpenTelemetry\SDK\Common\Environment\KnownValues as Values; use OpenTelemetry\SDK\Common\Environment\Variables as Env; -use OpenTelemetry\SDK\Common\Event\Dispatcher; +use OpenTelemetry\SDK\Common\Event\Event\DebugEvent; use OpenTelemetry\SDK\Common\Event\Event\ErrorEvent; use OpenTelemetry\SDK\Common\Event\Event\WarningEvent; use OpenTelemetry\SDK\Trace\Behavior\SpanExporterTrait; @@ -21,6 +22,7 @@ class Exporter implements SpanExporterInterface { + use EmitsEventsTrait; use ExporterTrait; use SpanExporterTrait; @@ -123,7 +125,7 @@ protected function doExport(iterable $spans): int [$response, $status] = $this->client->Export($request)->wait(); if ($status->code === \Grpc\STATUS_OK) { - self::logDebug('Exported span(s)', ['spans' => $resourceSpans]); + self::emit(new DebugEvent('Exported span(s)', ['spans' => $resourceSpans])); return self::STATUS_SUCCESS; } @@ -144,12 +146,12 @@ protected function doExport(iterable $spans): int \Grpc\STATUS_DATA_LOSS, \Grpc\STATUS_UNAUTHENTICATED, ], true)) { - Dispatcher::getInstance()->dispatch(new WarningEvent('Retryable error exporting grpc span', new Exception($error['error'], $error['code']))); + self::emit(new WarningEvent('Retryable error exporting grpc span', new Exception($error['error'], $error['code']))); return self::STATUS_FAILED_RETRYABLE; } - Dispatcher::getInstance()->dispatch(new ErrorEvent('Error exporting grpc span', new Exception($error['error']))); + self::emit(new ErrorEvent('Error exporting grpc span', new Exception($error['error']))); return self::STATUS_FAILED_NOT_RETRYABLE; } diff --git a/src/SDK/Behavior/EmitsEventsTrait.php b/src/SDK/Behavior/EmitsEventsTrait.php new file mode 100644 index 000000000..92c087066 --- /dev/null +++ b/src/SDK/Behavior/EmitsEventsTrait.php @@ -0,0 +1,15 @@ +dispatch($event); + } +} diff --git a/src/SDK/Trace/Behavior/HttpSpanExporterTrait.php b/src/SDK/Trace/Behavior/HttpSpanExporterTrait.php index bb78d0636..3f05f4018 100644 --- a/src/SDK/Trace/Behavior/HttpSpanExporterTrait.php +++ b/src/SDK/Trace/Behavior/HttpSpanExporterTrait.php @@ -7,7 +7,7 @@ use Exception; use InvalidArgumentException; use JsonException; -use OpenTelemetry\SDK\Common\Event\Dispatcher; +use OpenTelemetry\SDK\Behavior\EmitsEventsTrait; use OpenTelemetry\SDK\Common\Event\Event\ErrorEvent; use OpenTelemetry\SDK\Trace\SpanExporterInterface; use Psr\Http\Client\ClientExceptionInterface; @@ -25,6 +25,7 @@ */ trait HttpSpanExporterTrait { + use EmitsEventsTrait; use SpanExporterTrait; protected string $endpointUrl; @@ -47,19 +48,19 @@ protected function doExport(iterable $spans): int try { $response = $this->dispatchSpans($spans); } catch (ClientExceptionInterface $e) { - Dispatcher::getInstance()->dispatch(new ErrorEvent('Error exporting span', $e)); + self::emit(new ErrorEvent('Error exporting span', $e)); return $e instanceof RequestExceptionInterface ? SpanExporterInterface::STATUS_FAILED_NOT_RETRYABLE : SpanExporterInterface::STATUS_FAILED_RETRYABLE; } catch (Throwable $e) { - Dispatcher::getInstance()->dispatch(new ErrorEvent('Error exporting span', $e)); + self::emit(new ErrorEvent('Error exporting span', $e)); return SpanExporterInterface::STATUS_FAILED_NOT_RETRYABLE; } if ($response->getStatusCode() >= 400) { - Dispatcher::getInstance()->dispatch(new ErrorEvent('40x error exporting span', new Exception('', $response->getStatusCode()))); + self::emit(new ErrorEvent('40x error exporting span', new Exception('', $response->getStatusCode()))); return $response->getStatusCode() < 500 ? SpanExporterInterface::STATUS_FAILED_NOT_RETRYABLE diff --git a/src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php b/src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php index 36cf5a22e..8cf09ac47 100644 --- a/src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php +++ b/src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php @@ -5,8 +5,7 @@ namespace OpenTelemetry\SDK\Trace\SpanProcessor; use OpenTelemetry\Context\Context; -use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; -use OpenTelemetry\SDK\Common\Event\Dispatcher; +use OpenTelemetry\SDK\Behavior\EmitsEventsTrait; use OpenTelemetry\SDK\Common\Event\Event\DebugEvent; use OpenTelemetry\SDK\Trace\ReadableSpanInterface; use OpenTelemetry\SDK\Trace\ReadWriteSpanInterface; @@ -15,7 +14,7 @@ class SimpleSpanProcessor implements SpanProcessorInterface { - use LogsMessagesTrait; + use EmitsEventsTrait; private ?SpanExporterInterface $exporter; private bool $running = true; @@ -56,7 +55,7 @@ public function shutdown(): bool } $this->running = false; - Dispatcher::getInstance()->dispatch(new DebugEvent('Shutting down span processor')); + self::emit(new DebugEvent('Shutting down span processor')); if (null !== $this->exporter) { return $this->forceFlush() && $this->exporter->shutdown(); diff --git a/src/SDK/Trace/TracerProviderFactory.php b/src/SDK/Trace/TracerProviderFactory.php index ae5ee5fab..3433ae82d 100644 --- a/src/SDK/Trace/TracerProviderFactory.php +++ b/src/SDK/Trace/TracerProviderFactory.php @@ -5,13 +5,12 @@ namespace OpenTelemetry\SDK\Trace; use OpenTelemetry\API\Trace as API; -use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; -use OpenTelemetry\SDK\Common\Event\Dispatcher; +use OpenTelemetry\SDK\Behavior\EmitsEventsTrait; use OpenTelemetry\SDK\Common\Event\Event\WarningEvent; final class TracerProviderFactory { - use LogsMessagesTrait; + use EmitsEventsTrait; private ExporterFactory $exporterFactory; private SamplerFactory $samplerFactory; @@ -33,21 +32,21 @@ public function create(): API\TracerProviderInterface try { $exporter = $this->exporterFactory->fromEnvironment(); } catch (\Throwable $t) { - Dispatcher::getInstance()->dispatch(new WarningEvent('Unable to create exporter', $t)); + self::emit(new WarningEvent('Unable to create exporter', $t)); $exporter = null; } try { $sampler = $this->samplerFactory->fromEnvironment(); } catch (\Throwable $t) { - Dispatcher::getInstance()->dispatch(new WarningEvent('Unable to create sampler', $t)); + self::emit(new WarningEvent('Unable to create sampler', $t)); $sampler = null; } try { $spanProcessor = $this->spanProcessorFactory->fromEnvironment($exporter); } catch (\Throwable $t) { - Dispatcher::getInstance()->dispatch(new WarningEvent('Unable to create span processor', $t)); + self::emit(new WarningEvent('Unable to create span processor', $t)); $spanProcessor = null; } From 87fca9fd5c226da6c287ed8854f111547fc2ff1d Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Wed, 22 Jun 2022 15:48:24 +1000 Subject: [PATCH 11/14] moving events into API namespace otel spec says that _all_ otel libraries (including API) should be able to expose troubleshooting telemetry, so enable that by moving events (and logging) into API --- depfile.yaml | 8 ++- examples/EventsExample.php | 18 ++--- examples/SettingUpLogging.php | 2 +- .../Behavior/EmitsEventsTrait.php | 4 +- .../Behavior/LogsMessagesTrait.php | 5 +- src/{SDK => API}/Common/Event/Dispatcher.php | 8 +-- .../Common/Event/Event/DebugEvent.php | 2 +- .../Common/Event/Event/ErrorEvent.php | 4 +- .../Common/Event/Event/WarningEvent.php | 4 +- src/API/Common/Event/EventType.php | 16 +++++ .../Event/Handler/DebugEventHandler.php | 6 +- .../Event/Handler/ErrorEventHandler.php | 6 +- .../Event/Handler/WarningEventHandler.php | 6 +- .../Common/Event/SimpleDispatcher.php | 2 +- .../Common/Event/SimpleListenerProvider.php | 2 +- .../Common/Event/StoppableEventTrait.php | 2 +- src/{SDK => API}/Common/Log/LoggerHolder.php | 2 +- src/API/composer.json | 4 +- src/Contrib/Jaeger/JaegerTransport.php | 4 +- src/Contrib/OtlpGrpc/Exporter.php | 8 +-- .../Compatibility/BC/GlobalLoggerHolder.php | 2 +- .../Dev/Compatibility/BC/LoggerHolder.php | 65 +++++++++++++++++++ src/SDK/Common/Dev/Compatibility/_load.php | 1 + src/SDK/Common/Event/EventType.php | 16 ----- .../Trace/Behavior/HttpSpanExporterTrait.php | 4 +- .../SpanProcessor/SimpleSpanProcessor.php | 4 +- src/SDK/Trace/TracerProviderFactory.php | 4 +- src/SDK/composer.json | 1 - .../Behavior/LogsMessagesTraitTest.php | 8 +-- .../Common/Event/DispatcherTest.php | 16 ++--- .../Common/Event/Event/DebugEventTest.php | 6 +- .../Common/Event/Event/ErrorEventTest.php | 7 +- .../Common/Event/Event/WarningEventTest.php | 8 +-- .../Common/Event/Handler/DebugHandlerTest.php | 9 ++- .../Common/Event/Handler/ErrorHandlerTest.php | 8 +-- .../Event/Handler/LogBasedHandlerTest.php | 5 +- .../Event/Handler/WarningHandlerTest.php | 9 ++- .../Common/Event/SimpleDispatcherTest.php | 11 ++-- .../Event/SimpleListenerProviderTest.php | 9 ++- .../Common/Event/StoppableEventTraitTest.php | 7 +- .../Unit/SDK/Common/Log/LoggerHolderTest.php | 4 +- .../Behavior/HttpSpanExporterTraitTest.php | 2 +- .../SDK/Trace/TracerProviderFactoryTest.php | 6 +- 43 files changed, 195 insertions(+), 130 deletions(-) rename src/{SDK => API}/Behavior/EmitsEventsTrait.php (68%) rename src/{SDK => API}/Behavior/LogsMessagesTrait.php (88%) rename src/{SDK => API}/Common/Event/Dispatcher.php (79%) rename src/{SDK => API}/Common/Event/Event/DebugEvent.php (89%) rename src/{SDK => API}/Common/Event/Event/ErrorEvent.php (84%) rename src/{SDK => API}/Common/Event/Event/WarningEvent.php (87%) create mode 100644 src/API/Common/Event/EventType.php rename src/{SDK => API}/Common/Event/Handler/DebugEventHandler.php (59%) rename src/{SDK => API}/Common/Event/Handler/ErrorEventHandler.php (61%) rename src/{SDK => API}/Common/Event/Handler/WarningEventHandler.php (61%) rename src/{SDK => API}/Common/Event/SimpleDispatcher.php (97%) rename src/{SDK => API}/Common/Event/SimpleListenerProvider.php (95%) rename src/{SDK => API}/Common/Event/StoppableEventTrait.php (86%) rename src/{SDK => API}/Common/Log/LoggerHolder.php (96%) create mode 100644 src/SDK/Common/Dev/Compatibility/BC/LoggerHolder.php delete mode 100644 src/SDK/Common/Event/EventType.php rename tests/Unit/{SDK => API}/Behavior/LogsMessagesTraitTest.php (89%) rename tests/Unit/{SDK => API}/Common/Event/DispatcherTest.php (74%) rename tests/Unit/{SDK => API}/Common/Event/Event/DebugEventTest.php (69%) rename tests/Unit/{SDK => API}/Common/Event/Event/ErrorEventTest.php (61%) rename tests/Unit/{SDK => API}/Common/Event/Event/WarningEventTest.php (73%) rename tests/Unit/{SDK => API}/Common/Event/Handler/DebugHandlerTest.php (61%) rename tests/Unit/{SDK => API}/Common/Event/Handler/ErrorHandlerTest.php (68%) rename tests/Unit/{SDK => API}/Common/Event/Handler/LogBasedHandlerTest.php (75%) rename tests/Unit/{SDK => API}/Common/Event/Handler/WarningHandlerTest.php (62%) rename tests/Unit/{SDK => API}/Common/Event/SimpleDispatcherTest.php (88%) rename tests/Unit/{SDK => API}/Common/Event/SimpleListenerProviderTest.php (89%) rename tests/Unit/{SDK => API}/Common/Event/StoppableEventTraitTest.php (75%) diff --git a/depfile.yaml b/depfile.yaml index 95f55995a..acccb0298 100644 --- a/depfile.yaml +++ b/depfile.yaml @@ -39,6 +39,10 @@ layers: collectors: - type: className regex: ^Grpc\\ + - name: PsrEventDispatcher + collectors: + - type: className + regex: ^Psr\\EventDispatcher\\ - name: PsrLog collectors: - type: className @@ -82,13 +86,15 @@ ruleset: SemConv: ~ API: - Context + - PsrEventDispatcher + - PsrLog - SemConv SDK: - API - Context - SemConv - - PsrLog - PsrHttp + - PsrLog - Http - NyholmDsn - Composer diff --git a/examples/EventsExample.php b/examples/EventsExample.php index d1c46c47a..afb9dc6f8 100644 --- a/examples/EventsExample.php +++ b/examples/EventsExample.php @@ -6,13 +6,13 @@ use Monolog\Formatter\JsonFormatter; use Monolog\Handler\StreamHandler; use Monolog\Logger; +use OpenTelemetry\API\Common\Event\Dispatcher; +use OpenTelemetry\API\Common\Event\Event\ErrorEvent; +use OpenTelemetry\API\Common\Event\EventType; +use OpenTelemetry\API\Common\Event\SimpleDispatcher; +use OpenTelemetry\API\Common\Event\SimpleListenerProvider; +use OpenTelemetry\API\Common\Log\LoggerHolder; use OpenTelemetry\Contrib\Zipkin\Exporter as ZipkinExporter; -use OpenTelemetry\SDK\Common\Event\Dispatcher; -use OpenTelemetry\SDK\Common\Event\Event\ErrorEvent; -use OpenTelemetry\SDK\Common\Event\EventType; -use OpenTelemetry\SDK\Common\Event\SimpleDispatcher; -use OpenTelemetry\SDK\Common\Event\SimpleListenerProvider; -use OpenTelemetry\SDK\Common\Log\LoggerHolder; use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; use OpenTelemetry\SDK\Trace\TracerProvider; use Psr\Log\LogLevel; @@ -42,12 +42,12 @@ }, -10); //runs before built-in handler $listenerProvider->listen(EventType::ERROR, function (ErrorEvent $event) { echo 'Another custom handling of an error event: ' . $event->getMessage() . PHP_EOL; - echo json_encode($event->getException()->getTrace()) . PHP_EOL; + echo 'Trace: ' . json_encode($event->getException()->getTrace()) . PHP_EOL; echo 'Stopping event propagation...' . PHP_EOL; $event->stopPropagation(); -}, 5); //runs after build-in handler +}, 5); //runs after built-in handler $listenerProvider->listen(EventType::ERROR, function (ErrorEvent $event) { - echo 'This will not be executed, because a high priority handler stopped event propagation.' . PHP_EOL; + echo 'This will not be executed, because a higher priority handler stopped event propagation.' . PHP_EOL; }, 10); Dispatcher::setInstance(new SimpleDispatcher($listenerProvider)); diff --git a/examples/SettingUpLogging.php b/examples/SettingUpLogging.php index 2c7e3d984..0046a2975 100644 --- a/examples/SettingUpLogging.php +++ b/examples/SettingUpLogging.php @@ -5,8 +5,8 @@ use Monolog\Handler\StreamHandler; use Monolog\Logger; +use OpenTelemetry\API\Common\Log\LoggerHolder; use OpenTelemetry\Contrib\OtlpGrpc\Exporter as OtlpGrpcExporter; -use OpenTelemetry\SDK\Common\Log\LoggerHolder; use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; use OpenTelemetry\SDK\Trace\TracerProvider; use Psr\Log\LogLevel; diff --git a/src/SDK/Behavior/EmitsEventsTrait.php b/src/API/Behavior/EmitsEventsTrait.php similarity index 68% rename from src/SDK/Behavior/EmitsEventsTrait.php rename to src/API/Behavior/EmitsEventsTrait.php index 92c087066..36be9f012 100644 --- a/src/SDK/Behavior/EmitsEventsTrait.php +++ b/src/API/Behavior/EmitsEventsTrait.php @@ -2,9 +2,9 @@ declare(strict_types=1); -namespace OpenTelemetry\SDK\Behavior; +namespace OpenTelemetry\API\Behavior; -use OpenTelemetry\SDK\Common\Event\Dispatcher; +use OpenTelemetry\API\Common\Event\Dispatcher; trait EmitsEventsTrait { diff --git a/src/SDK/Behavior/LogsMessagesTrait.php b/src/API/Behavior/LogsMessagesTrait.php similarity index 88% rename from src/SDK/Behavior/LogsMessagesTrait.php rename to src/API/Behavior/LogsMessagesTrait.php index 23b3fb7d2..244cf5201 100644 --- a/src/SDK/Behavior/LogsMessagesTrait.php +++ b/src/API/Behavior/LogsMessagesTrait.php @@ -2,16 +2,15 @@ declare(strict_types=1); -namespace OpenTelemetry\SDK\Behavior; +namespace OpenTelemetry\API\Behavior; -use OpenTelemetry\SDK\Common\Log\LoggerHolder; +use OpenTelemetry\API\Common\Log\LoggerHolder; use Psr\Log\LogLevel; trait LogsMessagesTrait { private static function doLog(string $level, string $message, array $context): void { - $context['source'] = get_called_class(); LoggerHolder::get()->log($level, $message, $context); } diff --git a/src/SDK/Common/Event/Dispatcher.php b/src/API/Common/Event/Dispatcher.php similarity index 79% rename from src/SDK/Common/Event/Dispatcher.php rename to src/API/Common/Event/Dispatcher.php index 816fb5b84..a07374850 100644 --- a/src/SDK/Common/Event/Dispatcher.php +++ b/src/API/Common/Event/Dispatcher.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace OpenTelemetry\SDK\Common\Event; +namespace OpenTelemetry\API\Common\Event; -use OpenTelemetry\SDK\Common\Event\Handler\DebugEventHandler; -use OpenTelemetry\SDK\Common\Event\Handler\ErrorEventHandler; -use OpenTelemetry\SDK\Common\Event\Handler\WarningEventHandler; +use OpenTelemetry\API\Common\Event\Handler\DebugEventHandler; +use OpenTelemetry\API\Common\Event\Handler\ErrorEventHandler; +use OpenTelemetry\API\Common\Event\Handler\WarningEventHandler; use Psr\EventDispatcher\EventDispatcherInterface; class Dispatcher diff --git a/src/SDK/Common/Event/Event/DebugEvent.php b/src/API/Common/Event/Event/DebugEvent.php similarity index 89% rename from src/SDK/Common/Event/Event/DebugEvent.php rename to src/API/Common/Event/Event/DebugEvent.php index 360fb6fae..b2433dbe7 100644 --- a/src/SDK/Common/Event/Event/DebugEvent.php +++ b/src/API/Common/Event/Event/DebugEvent.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace OpenTelemetry\SDK\Common\Event\Event; +namespace OpenTelemetry\API\Common\Event\Event; class DebugEvent { diff --git a/src/SDK/Common/Event/Event/ErrorEvent.php b/src/API/Common/Event/Event/ErrorEvent.php similarity index 84% rename from src/SDK/Common/Event/Event/ErrorEvent.php rename to src/API/Common/Event/Event/ErrorEvent.php index 726c0e241..8d8fd6901 100644 --- a/src/SDK/Common/Event/Event/ErrorEvent.php +++ b/src/API/Common/Event/Event/ErrorEvent.php @@ -2,9 +2,9 @@ declare(strict_types=1); -namespace OpenTelemetry\SDK\Common\Event\Event; +namespace OpenTelemetry\API\Common\Event\Event; -use OpenTelemetry\SDK\Common\Event\StoppableEventTrait; +use OpenTelemetry\API\Common\Event\StoppableEventTrait; use Psr\EventDispatcher\StoppableEventInterface; use Throwable; diff --git a/src/SDK/Common/Event/Event/WarningEvent.php b/src/API/Common/Event/Event/WarningEvent.php similarity index 87% rename from src/SDK/Common/Event/Event/WarningEvent.php rename to src/API/Common/Event/Event/WarningEvent.php index cbbcee2a7..95437187f 100644 --- a/src/SDK/Common/Event/Event/WarningEvent.php +++ b/src/API/Common/Event/Event/WarningEvent.php @@ -2,9 +2,9 @@ declare(strict_types=1); -namespace OpenTelemetry\SDK\Common\Event\Event; +namespace OpenTelemetry\API\Common\Event\Event; -use OpenTelemetry\SDK\Common\Event\StoppableEventTrait; +use OpenTelemetry\API\Common\Event\StoppableEventTrait; use Psr\EventDispatcher\StoppableEventInterface; use Throwable; diff --git a/src/API/Common/Event/EventType.php b/src/API/Common/Event/EventType.php new file mode 100644 index 000000000..bc0abda2f --- /dev/null +++ b/src/API/Common/Event/EventType.php @@ -0,0 +1,16 @@ +assertSame($message, $event->getMessage()); $this->assertSame($exception, $event->getException()); } diff --git a/tests/Unit/SDK/Common/Event/Event/WarningEventTest.php b/tests/Unit/API/Common/Event/Event/WarningEventTest.php similarity index 73% rename from tests/Unit/SDK/Common/Event/Event/WarningEventTest.php rename to tests/Unit/API/Common/Event/Event/WarningEventTest.php index 1aa876ccb..30cf392d5 100644 --- a/tests/Unit/SDK/Common/Event/Event/WarningEventTest.php +++ b/tests/Unit/API/Common/Event/Event/WarningEventTest.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace OpenTelemetry\Tests\Unit\SDK\Common\Event\Event; +namespace OpenTelemetry\Tests\Unit\API\Common\Event\Event; -use OpenTelemetry\SDK\Common\Event\Event\WarningEvent; +use OpenTelemetry\API\Common\Event\Event\WarningEvent; use PHPUnit\Framework\TestCase; /** - * @covers \OpenTelemetry\SDK\Common\Event\Event\WarningEvent + * @covers \OpenTelemetry\API\Common\Event\Event\WarningEvent */ class WarningEventTest extends TestCase { @@ -16,7 +16,7 @@ public function test_warning_event_with_throwable(): void { $message = 'foo'; $exception = new \Exception(); - $event = new WarningEvent($message, $exception); + $event = new \OpenTelemetry\API\Common\Event\Event\WarningEvent($message, $exception); $this->assertSame($message, $event->getMessage()); $this->assertTrue($event->hasError()); $this->assertSame($exception, $event->getException()); diff --git a/tests/Unit/SDK/Common/Event/Handler/DebugHandlerTest.php b/tests/Unit/API/Common/Event/Handler/DebugHandlerTest.php similarity index 61% rename from tests/Unit/SDK/Common/Event/Handler/DebugHandlerTest.php rename to tests/Unit/API/Common/Event/Handler/DebugHandlerTest.php index ad9eb2ec1..a31eaf214 100644 --- a/tests/Unit/SDK/Common/Event/Handler/DebugHandlerTest.php +++ b/tests/Unit/API/Common/Event/Handler/DebugHandlerTest.php @@ -2,14 +2,13 @@ declare(strict_types=1); -namespace OpenTelemetry\Tests\Unit\SDK\Common\Event\Handler; +namespace OpenTelemetry\Tests\Unit\API\Common\Event\Handler; -use OpenTelemetry\SDK\Common\Event\Event\DebugEvent; -use OpenTelemetry\SDK\Common\Event\Handler\DebugEventHandler; +use OpenTelemetry\API\Common\Event\Event\DebugEvent; use Psr\Log\LogLevel; /** - * @covers \OpenTelemetry\SDK\Common\Event\Handler\DebugEventHandler + * @covers \OpenTelemetry\API\Common\Event\Handler\DebugEventHandler */ class DebugHandlerTest extends LogBasedHandlerTest { @@ -19,7 +18,7 @@ class DebugHandlerTest extends LogBasedHandlerTest public function test_logs_event(): void { $event = new DebugEvent('foo'); - $handler = new DebugEventHandler(); + $handler = new \OpenTelemetry\API\Common\Event\Handler\DebugEventHandler(); // @phpstan-ignore-next-line $this->logger->expects($this->once())->method('log')->with($this->equalTo(LogLevel::DEBUG)); $handler($event); diff --git a/tests/Unit/SDK/Common/Event/Handler/ErrorHandlerTest.php b/tests/Unit/API/Common/Event/Handler/ErrorHandlerTest.php similarity index 68% rename from tests/Unit/SDK/Common/Event/Handler/ErrorHandlerTest.php rename to tests/Unit/API/Common/Event/Handler/ErrorHandlerTest.php index 8b5698bb8..fd23ceaac 100644 --- a/tests/Unit/SDK/Common/Event/Handler/ErrorHandlerTest.php +++ b/tests/Unit/API/Common/Event/Handler/ErrorHandlerTest.php @@ -2,15 +2,15 @@ declare(strict_types=1); -namespace OpenTelemetry\Tests\Unit\SDK\Common\Event\Handler; +namespace OpenTelemetry\Tests\Unit\API\Common\Event\Handler; use Exception; -use OpenTelemetry\SDK\Common\Event\Event\ErrorEvent; -use OpenTelemetry\SDK\Common\Event\Handler\ErrorEventHandler; +use OpenTelemetry\API\Common\Event\Event\ErrorEvent; +use OpenTelemetry\API\Common\Event\Handler\ErrorEventHandler; use Psr\Log\LogLevel; /** - * @covers \OpenTelemetry\SDK\Common\Event\Handler\ErrorEventHandler + * @covers \OpenTelemetry\API\Common\Event\Handler\ErrorEventHandler */ class ErrorHandlerTest extends LogBasedHandlerTest { diff --git a/tests/Unit/SDK/Common/Event/Handler/LogBasedHandlerTest.php b/tests/Unit/API/Common/Event/Handler/LogBasedHandlerTest.php similarity index 75% rename from tests/Unit/SDK/Common/Event/Handler/LogBasedHandlerTest.php rename to tests/Unit/API/Common/Event/Handler/LogBasedHandlerTest.php index fada9155b..7cb3dd0d6 100644 --- a/tests/Unit/SDK/Common/Event/Handler/LogBasedHandlerTest.php +++ b/tests/Unit/API/Common/Event/Handler/LogBasedHandlerTest.php @@ -2,10 +2,9 @@ declare(strict_types=1); -namespace OpenTelemetry\Tests\Unit\SDK\Common\Event\Handler; +namespace OpenTelemetry\Tests\Unit\API\Common\Event\Handler; -use OpenTelemetry\SDK\Common\Log\LoggerHolder; -use PHPUnit\Framework\MockObject\MockObject; +use OpenTelemetry\API\Common\Log\LoggerHolder; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; diff --git a/tests/Unit/SDK/Common/Event/Handler/WarningHandlerTest.php b/tests/Unit/API/Common/Event/Handler/WarningHandlerTest.php similarity index 62% rename from tests/Unit/SDK/Common/Event/Handler/WarningHandlerTest.php rename to tests/Unit/API/Common/Event/Handler/WarningHandlerTest.php index b38aaf328..0fa39b71b 100644 --- a/tests/Unit/SDK/Common/Event/Handler/WarningHandlerTest.php +++ b/tests/Unit/API/Common/Event/Handler/WarningHandlerTest.php @@ -2,15 +2,14 @@ declare(strict_types=1); -namespace OpenTelemetry\Tests\Unit\SDK\Common\Event\Handler; +namespace OpenTelemetry\Tests\Unit\API\Common\Event\Handler; use Exception; -use OpenTelemetry\SDK\Common\Event\Event\WarningEvent; -use OpenTelemetry\SDK\Common\Event\Handler\WarningEventHandler; +use OpenTelemetry\API\Common\Event\Event\WarningEvent; use Psr\Log\LogLevel; /** - * @covers \OpenTelemetry\SDK\Common\Event\Handler\WarningEventHandler + * @covers \OpenTelemetry\API\Common\Event\Handler\WarningEventHandler */ class WarningHandlerTest extends LogBasedHandlerTest { @@ -20,7 +19,7 @@ class WarningHandlerTest extends LogBasedHandlerTest public function test_logs_event(): void { $event = new WarningEvent('foo', new Exception()); - $handler = new WarningEventHandler(); + $handler = new \OpenTelemetry\API\Common\Event\Handler\WarningEventHandler(); // @phpstan-ignore-next-line $this->logger->expects($this->once())->method('log')->with($this->equalTo(LogLevel::WARNING)); $handler($event); diff --git a/tests/Unit/SDK/Common/Event/SimpleDispatcherTest.php b/tests/Unit/API/Common/Event/SimpleDispatcherTest.php similarity index 88% rename from tests/Unit/SDK/Common/Event/SimpleDispatcherTest.php rename to tests/Unit/API/Common/Event/SimpleDispatcherTest.php index 4066d84fd..4e1927bd4 100644 --- a/tests/Unit/SDK/Common/Event/SimpleDispatcherTest.php +++ b/tests/Unit/API/Common/Event/SimpleDispatcherTest.php @@ -2,17 +2,16 @@ declare(strict_types=1); -namespace OpenTelemetry\Tests\Unit\SDK\Common\Event; +namespace OpenTelemetry\Tests\Unit\API\Common\Event; -use OpenTelemetry\SDK\Common\Event\SimpleDispatcher; -use OpenTelemetry\SDK\Common\Event\SimpleListenerProvider; +use OpenTelemetry\API\Common\Event\SimpleDispatcher; use PHPUnit\Framework\TestCase; use Psr\EventDispatcher\ListenerProviderInterface; use Psr\EventDispatcher\StoppableEventInterface; use stdClass; /** - * @covers \OpenTelemetry\SDK\Common\Event\SimpleDispatcher + * @covers \OpenTelemetry\API\Common\Event\SimpleDispatcher */ class SimpleDispatcherTest extends TestCase { @@ -31,7 +30,7 @@ public function test_get_listener_provider(): void public function test_proxies_listen_for_simple_listener_provider(): void { - $provider = $this->createMock(SimpleListenerProvider::class); + $provider = $this->createMock(\OpenTelemetry\API\Common\Event\SimpleListenerProvider::class); $callable = function () { }; $eventName = 'my.event'; @@ -69,7 +68,7 @@ public function test_dispatch_event(): void $this->assertSame($event, $receivedEvent); }; $this->listenerProvider->expects($this->once())->method('getListenersForEvent')->willReturn([$handler]); //@phpstan-ignore-line - $dispatcher = new SimpleDispatcher($this->listenerProvider); + $dispatcher = new \OpenTelemetry\API\Common\Event\SimpleDispatcher($this->listenerProvider); $dispatcher->dispatch($event); } diff --git a/tests/Unit/SDK/Common/Event/SimpleListenerProviderTest.php b/tests/Unit/API/Common/Event/SimpleListenerProviderTest.php similarity index 89% rename from tests/Unit/SDK/Common/Event/SimpleListenerProviderTest.php rename to tests/Unit/API/Common/Event/SimpleListenerProviderTest.php index 79c86f2ba..f51557a5e 100644 --- a/tests/Unit/SDK/Common/Event/SimpleListenerProviderTest.php +++ b/tests/Unit/API/Common/Event/SimpleListenerProviderTest.php @@ -2,22 +2,21 @@ declare(strict_types=1); -namespace OpenTelemetry\Tests\Unit\SDK\Common\Event; +namespace OpenTelemetry\Tests\Unit\API\Common\Event; -use OpenTelemetry\SDK\Common\Event\SimpleListenerProvider; use PHPUnit\Framework\TestCase; use stdClass; /** - * @covers \OpenTelemetry\SDK\Common\Event\SimpleListenerProvider + * @covers \OpenTelemetry\API\Common\Event\SimpleListenerProvider */ class SimpleListenerProviderTest extends TestCase { - private SimpleListenerProvider $provider; + private \OpenTelemetry\API\Common\Event\SimpleListenerProvider $provider; public function setUp(): void { - $this->provider = new SimpleListenerProvider(); + $this->provider = new \OpenTelemetry\API\Common\Event\SimpleListenerProvider(); } public function test_add_listeners(): void diff --git a/tests/Unit/SDK/Common/Event/StoppableEventTraitTest.php b/tests/Unit/API/Common/Event/StoppableEventTraitTest.php similarity index 75% rename from tests/Unit/SDK/Common/Event/StoppableEventTraitTest.php rename to tests/Unit/API/Common/Event/StoppableEventTraitTest.php index f623c0548..d08f95101 100644 --- a/tests/Unit/SDK/Common/Event/StoppableEventTraitTest.php +++ b/tests/Unit/API/Common/Event/StoppableEventTraitTest.php @@ -2,14 +2,13 @@ declare(strict_types=1); -namespace OpenTelemetry\Tests\Unit\SDK\Common\Event; +namespace OpenTelemetry\Tests\Unit\API\Common\Event; -use OpenTelemetry\SDK\Common\Event\StoppableEventTrait; use PHPUnit\Framework\TestCase; use Psr\EventDispatcher\StoppableEventInterface; /** - * @covers \OpenTelemetry\SDK\Common\Event\StoppableEventTrait + * @covers \OpenTelemetry\API\Common\Event\StoppableEventTrait */ class StoppableEventTraitTest extends TestCase { @@ -27,7 +26,7 @@ public function test_stop_event(): void public function getEvent(): StoppableEventInterface { return new class() implements StoppableEventInterface { - use StoppableEventTrait; + use \OpenTelemetry\API\Common\Event\StoppableEventTrait; }; } } diff --git a/tests/Unit/SDK/Common/Log/LoggerHolderTest.php b/tests/Unit/SDK/Common/Log/LoggerHolderTest.php index d19801818..26c78886b 100644 --- a/tests/Unit/SDK/Common/Log/LoggerHolderTest.php +++ b/tests/Unit/SDK/Common/Log/LoggerHolderTest.php @@ -4,13 +4,13 @@ namespace OpenTelemetry\Tests\Unit\SDK\Common\Log; -use OpenTelemetry\SDK\Common\Log\LoggerHolder; +use OpenTelemetry\API\Common\Log\LoggerHolder; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; /** - * @covers \OpenTelemetry\SDK\Common\Log\LoggerHolder + * @covers \OpenTelemetry\API\Common\Log\LoggerHolder */ class LoggerHolderTest extends TestCase { diff --git a/tests/Unit/SDK/Trace/Behavior/HttpSpanExporterTraitTest.php b/tests/Unit/SDK/Trace/Behavior/HttpSpanExporterTraitTest.php index be3757225..ee92c4c55 100644 --- a/tests/Unit/SDK/Trace/Behavior/HttpSpanExporterTraitTest.php +++ b/tests/Unit/SDK/Trace/Behavior/HttpSpanExporterTraitTest.php @@ -5,7 +5,7 @@ namespace OpenTelemetry\Tests\Unit\SDK\Trace\Behavior; use GuzzleHttp\Exception\ClientException; -use OpenTelemetry\SDK\Common\Event\Dispatcher; +use OpenTelemetry\API\Common\Event\Dispatcher; use OpenTelemetry\SDK\Trace\Behavior\HttpSpanExporterTrait; use OpenTelemetry\SDK\Trace\SpanDataInterface; use OpenTelemetry\SDK\Trace\SpanExporterInterface; diff --git a/tests/Unit/SDK/Trace/TracerProviderFactoryTest.php b/tests/Unit/SDK/Trace/TracerProviderFactoryTest.php index 29c2b04b8..002b9063a 100644 --- a/tests/Unit/SDK/Trace/TracerProviderFactoryTest.php +++ b/tests/Unit/SDK/Trace/TracerProviderFactoryTest.php @@ -4,9 +4,9 @@ namespace OpenTelemetry\Tests\Unit\SDK\Trace; -use OpenTelemetry\SDK\Common\Event\Dispatcher; -use OpenTelemetry\SDK\Common\Event\Event\WarningEvent; -use OpenTelemetry\SDK\Common\Log\LoggerHolder; +use OpenTelemetry\API\Common\Event\Dispatcher; +use OpenTelemetry\API\Common\Event\Event\WarningEvent; +use OpenTelemetry\API\Common\Log\LoggerHolder; use OpenTelemetry\SDK\Trace\ExporterFactory; use OpenTelemetry\SDK\Trace\SamplerFactory; use OpenTelemetry\SDK\Trace\SpanProcessorFactory; From 122cc83bd34101fdfe186827712b63d181f83bfb Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Wed, 22 Jun 2022 20:00:28 +1000 Subject: [PATCH 12/14] benchmark test for events --- tests/Benchmark/EventBench.php | 78 ++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 tests/Benchmark/EventBench.php diff --git a/tests/Benchmark/EventBench.php b/tests/Benchmark/EventBench.php new file mode 100644 index 000000000..389ced05c --- /dev/null +++ b/tests/Benchmark/EventBench.php @@ -0,0 +1,78 @@ +listenerProvider = new SimpleListenerProvider(); + $this->dispatcher = new SimpleDispatcher($this->listenerProvider); + $this->function = function(){}; + $this->event = new stdClass(); + } + + public function addEventsToListener(): void + { + for ($i=0; $i<10; $i++) { + $this->listenerProvider->listen('event_'.$i, $this->function); + } + $this->listenerProvider->listen(get_class($this->event), $this->function); + } + + /** + * @ParamProviders("provideListenerCounts") + * @Revs(1000) + * @Iterations(10) + * @OutputTimeUnit("microseconds") + */ + public function benchAddListeners(array $params): void + { + for ($i=0; $i<$params[0]; $i++) { + $this->listenerProvider->listen('event_'.$i, $this->function); + } + } + + /** + * @ParamProviders("provideListenerCounts") + * @Revs(1000) + * @Iterations(10) + * @OutputTimeUnit("microseconds") + */ + public function benchAddListenersForSameEvent(array $params): void + { + for ($i=0; $i<$params[0]; $i++) { + $this->listenerProvider->listen('event', $this->function); + } + } + + /** + * @BeforeMethods("addEventsToListener") + * @Revs(1000) + * @Iterations(10) + * @OutputTimeUnit("microseconds") + */ + public function benchDispatchEvent(): void + { + $this->dispatcher->dispatch($this->event); + } + + public function provideListenerCounts(): \Generator + { + yield [1]; + yield [4]; + yield [16]; + yield [256]; + } +} \ No newline at end of file From 43fb0702a888b12ab9bf1be8befbe6170396c72e Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Wed, 22 Jun 2022 20:02:42 +1000 Subject: [PATCH 13/14] fix name --- tests/Benchmark/EventBench.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/Benchmark/EventBench.php b/tests/Benchmark/EventBench.php index 389ced05c..367cbcc02 100644 --- a/tests/Benchmark/EventBench.php +++ b/tests/Benchmark/EventBench.php @@ -4,6 +4,7 @@ namespace OpenTelemetry\Tests\Benchmark; +use Generator; use OpenTelemetry\API\Common\Event\SimpleDispatcher; use OpenTelemetry\API\Common\Event\SimpleListenerProvider; use stdClass; @@ -12,23 +13,23 @@ class EventBench { private SimpleDispatcher $dispatcher; private SimpleListenerProvider $listenerProvider; - private $function; + private $listener; private object $event; public function __construct() { $this->listenerProvider = new SimpleListenerProvider(); $this->dispatcher = new SimpleDispatcher($this->listenerProvider); - $this->function = function(){}; + $this->listener = function(){}; $this->event = new stdClass(); } public function addEventsToListener(): void { for ($i=0; $i<10; $i++) { - $this->listenerProvider->listen('event_'.$i, $this->function); + $this->listenerProvider->listen('event_'.$i, $this->listener); } - $this->listenerProvider->listen(get_class($this->event), $this->function); + $this->listenerProvider->listen(get_class($this->event), $this->listener); } /** @@ -40,7 +41,7 @@ public function addEventsToListener(): void public function benchAddListeners(array $params): void { for ($i=0; $i<$params[0]; $i++) { - $this->listenerProvider->listen('event_'.$i, $this->function); + $this->listenerProvider->listen('event_'.$i, $this->listener); } } @@ -53,7 +54,7 @@ public function benchAddListeners(array $params): void public function benchAddListenersForSameEvent(array $params): void { for ($i=0; $i<$params[0]; $i++) { - $this->listenerProvider->listen('event', $this->function); + $this->listenerProvider->listen('event', $this->listener); } } @@ -68,7 +69,7 @@ public function benchDispatchEvent(): void $this->dispatcher->dispatch($this->event); } - public function provideListenerCounts(): \Generator + public function provideListenerCounts(): Generator { yield [1]; yield [4]; From 9099ff753d7677d28df736c089fb5d978c8fbda2 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Wed, 22 Jun 2022 20:12:12 +1000 Subject: [PATCH 14/14] linting --- tests/Benchmark/EventBench.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/Benchmark/EventBench.php b/tests/Benchmark/EventBench.php index 367cbcc02..ecc9b92c7 100644 --- a/tests/Benchmark/EventBench.php +++ b/tests/Benchmark/EventBench.php @@ -20,14 +20,15 @@ public function __construct() { $this->listenerProvider = new SimpleListenerProvider(); $this->dispatcher = new SimpleDispatcher($this->listenerProvider); - $this->listener = function(){}; + $this->listener = function () { + }; $this->event = new stdClass(); } public function addEventsToListener(): void { for ($i=0; $i<10; $i++) { - $this->listenerProvider->listen('event_'.$i, $this->listener); + $this->listenerProvider->listen('event_' . $i, $this->listener); } $this->listenerProvider->listen(get_class($this->event), $this->listener); } @@ -41,7 +42,7 @@ public function addEventsToListener(): void public function benchAddListeners(array $params): void { for ($i=0; $i<$params[0]; $i++) { - $this->listenerProvider->listen('event_'.$i, $this->listener); + $this->listenerProvider->listen('event_' . $i, $this->listener); } } @@ -76,4 +77,4 @@ public function provideListenerCounts(): Generator yield [16]; yield [256]; } -} \ No newline at end of file +}