-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding event dispatcher #708
Closed
Closed
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
851049b
adding events
brettmc 195eabb
static analysis fixes
brettmc 2093b2c
remove commented-out code
brettmc 85dd9ef
self-review, adding tests and tidying
brettmc bba3ac0
making analysis happy
brettmc 265bdc2
adding tests for SpanLimits
brettmc f650b8d
tidy
brettmc 336f481
reorder assertions
brettmc 026f4c8
error -> exception
brettmc 0a91c30
moving event emitting logic into a trait
brettmc 87fca9f
moving events into API namespace
brettmc dc8f757
Merge branch 'main' into feature/event-dispatcher
brettmc 122cc83
benchmark test for events
brettmc 43fb070
fix name
brettmc 9099ff7
linting
brettmc db10181
Merge branch 'open-telemetry:main' into feature/event-dispatcher
brettmc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Monolog\Formatter\JsonFormatter; | ||
use Monolog\Handler\StreamHandler; | ||
use Monolog\Logger; | ||
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; | ||
|
||
/** | ||
* This example shows how you can hook in to OpenTelemetry's events, such as when an error or warning is emitted by | ||
* the library. OpenTelemetry will always attempt to log and continue on error, and may provide no-op implementations | ||
* of classes if it was unable to create them as requested. | ||
* The default event handlers will use a PSR-3 logger to log events - verbosity and output format can be controlled | ||
* through the usual PSR-3 log levels and formatters (if available). | ||
* If you wish to do something different with events, you may provide an alternative PSR-14 implementation, but be | ||
* aware that you will need to register handlers for all events that you are interested in. | ||
*/ | ||
|
||
//logger used by default event handlers | ||
LoggerHolder::set( | ||
new Logger('otel-php', [(new StreamHandler(STDOUT, LogLevel::DEBUG))->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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\SDK\Common\Event; | ||
|
||
use OpenTelemetry\SDK\Common\Event\Handler\DebugEventHandler; | ||
use OpenTelemetry\SDK\Common\Event\Handler\ErrorEventHandler; | ||
use OpenTelemetry\SDK\Common\Event\Handler\WarningEventHandler; | ||
use Psr\EventDispatcher\EventDispatcherInterface; | ||
|
||
class Dispatcher | ||
{ | ||
private static ?EventDispatcherInterface $instance = null; | ||
|
||
public static function getInstance(): EventDispatcherInterface | ||
{ | ||
if (self::$instance === null) { | ||
$dispatcher = new SimpleDispatcher(new SimpleListenerProvider()); | ||
$dispatcher->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\SDK\Common\Event\Event; | ||
|
||
class DebugEvent | ||
{ | ||
private string $message; | ||
private array $extra; | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\SDK\Common\Event\Event; | ||
|
||
use OpenTelemetry\SDK\Common\Event\StoppableEventTrait; | ||
use Psr\EventDispatcher\StoppableEventInterface; | ||
use Throwable; | ||
|
||
class ErrorEvent implements StoppableEventInterface | ||
{ | ||
use StoppableEventTrait; | ||
|
||
protected string $message; | ||
protected Throwable $error; | ||
|
||
public function __construct(string $message, Throwable $error) | ||
{ | ||
$this->message = $message; | ||
$this->error = $error; | ||
} | ||
|
||
public function getError(): Throwable | ||
{ | ||
return $this->error; | ||
} | ||
|
||
public function getMessage(): string | ||
{ | ||
return $this->message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\SDK\Common\Event\Event; | ||
|
||
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 = null) | ||
{ | ||
$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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\SDK\Common\Event; | ||
|
||
use OpenTelemetry\SDK\Common\Event\Event\DebugEvent; | ||
use OpenTelemetry\SDK\Common\Event\Event\ErrorEvent; | ||
use OpenTelemetry\SDK\Common\Event\Event\WarningEvent; | ||
|
||
class EventType | ||
{ | ||
public const ERROR = ErrorEvent::class; | ||
public const WARNING = WarningEvent::class; | ||
public const DEBUG = DebugEvent::class; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\SDK\Common\Event\Handler; | ||
|
||
use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; | ||
use OpenTelemetry\SDK\Common\Event\Event\DebugEvent; | ||
|
||
class DebugEventHandler | ||
{ | ||
use LogsMessagesTrait; | ||
|
||
public function __invoke(DebugEvent $event): void | ||
{ | ||
self::logDebug($event->getMessage(), $event->getExtra()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\SDK\Common\Event\Handler; | ||
|
||
use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; | ||
use OpenTelemetry\SDK\Common\Event\Event\ErrorEvent; | ||
|
||
class ErrorEventHandler | ||
{ | ||
use LogsMessagesTrait; | ||
|
||
public function __invoke(ErrorEvent $event): void | ||
{ | ||
self::logError($event->getError()->getMessage(), ['error' => $event->getError()]); | ||
brettmc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\SDK\Common\Event\Handler; | ||
|
||
use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; | ||
use OpenTelemetry\SDK\Common\Event\Event\WarningEvent; | ||
|
||
class WarningEventHandler | ||
{ | ||
use LogsMessagesTrait; | ||
|
||
public function __invoke(WarningEvent $event): void | ||
{ | ||
self::logWarning($event->getMessage(), ['error' => $event->getError()]); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tidal - this might be an opportunity to move
LoggerHolder
somewhere underSDK\Event
? A way to get rid of the static singleton could be to create something like anAbstractLoggableEvent
(or trait) which accepts aLoggerInterface
and is extended by our events as required?