diff --git a/config/opentelemetry.php b/config/opentelemetry.php index 7a36840..e502652 100644 --- a/config/opentelemetry.php +++ b/config/opentelemetry.php @@ -44,6 +44,11 @@ Instrumentation\QueueInstrumentation::class => env('OT_INSTRUMENTATION_QUEUE', true), Instrumentation\CacheInstrumentation::class => env('OT_INSTRUMENTATION_CACHE', true), + + Instrumentation\EventInstrumentation::class => [ + 'enabled' => env('OT_INSTRUMENTATION_EVENT', true), + 'ignored' => [], + ], ], /** diff --git a/src/Instrumentation/EventInstrumentation.php b/src/Instrumentation/EventInstrumentation.php new file mode 100644 index 0000000..1927afd --- /dev/null +++ b/src/Instrumentation/EventInstrumentation.php @@ -0,0 +1,101 @@ +listen('*', [$this, 'recordEvent']); + } + + public function recordEvent(string $event, array $payload): void + { + if ($this->isInternalLaravelEvent($event) || $this->isIgnoredEvent($event)) { + return; + } + + Tracer::activeSpan()->addEvent(sprintf('Event %s fired', $event), [ + 'event.name' => $event, + ]); + } + + protected function isInternalLaravelEvent(string $event): bool + { + return Str::is([ + 'Illuminate\*', + 'Laravel\Octane\*', + 'Laravel\Scout\*', + 'eloquent*', + 'bootstrapped*', + 'bootstrapping*', + 'creating*', + 'composing*', + ], $event); + } + + protected function isIgnoredEvent(string $event): bool + { + return in_array($event, static::$ignoredEvents); + } + +// public function recordCacheHit(CacheHit $event): void +// { +// $this->addEvent('cache hit', [ +// 'key' => $event->key, +// 'tags' => json_encode($event->tags), +// ]); +// } +// +// public function recordCacheMiss(CacheMissed $event): void +// { +// $this->addEvent('cache miss', [ +// 'key' => $event->key, +// 'tags' => json_encode($event->tags), +// ]); +// } +// +// /** @psalm-suppress UndefinedPropertyFetch */ +// public function recordCacheSet(KeyWritten $event): void +// { +// $ttl = property_exists($event, 'minutes') +// ? $event->minutes * 60 +// : $event->seconds; +// +// $this->addEvent('cache set', [ +// 'key' => $event->key, +// 'tags' => json_encode($event->tags), +// 'expires_at' => $ttl > 0 ? now()->addSeconds($ttl)->getTimestamp() : 'never', +// 'expires_in_seconds' => $ttl > 0 ? $ttl : 'never', +// 'expires_in_human' => $ttl > 0 ? now()->addSeconds($ttl)->diffForHumans() : 'never', +// ]); +// } +// +// public function recordCacheForget(KeyForgotten $event): void +// { +// $this->addEvent('cache forget', [ +// 'key' => $event->key, +// 'tags' => json_encode($event->tags), +// ]); +// } +// +// private function addEvent(string $name, iterable $attributes = []): void +// { +// Tracer::activeSpan()->addEvent($name, $attributes); +// } +} diff --git a/tests/Instrumentation/EventInstrumentation.php b/tests/Instrumentation/EventInstrumentation.php new file mode 100644 index 0000000..680f2c2 --- /dev/null +++ b/tests/Instrumentation/EventInstrumentation.php @@ -0,0 +1,58 @@ +getEvents()); + + expect($event) + ->not->toBeNull() + ->getName()->toBe('Event test-event fired') + ->getAttributes()->toArray()->toBe([ + 'event.name' => 'test-event', + ]); +}); + +it('records class event', function () { + withRootSpan(function () { + event(new TestEvent('test')); + }); + + $rootSpan = Arr::last(getRecordedSpans()); + + /** @var \OpenTelemetry\SDK\Trace\Event $event */ + $event = Arr::last($rootSpan->getEvents()); + + expect($event) + ->not->toBeNull() + ->getName()->toBe(sprintf('Event %s fired', TestEvent::class)) + ->getAttributes()->toArray()->toBe([ + 'event.name' => TestEvent::class, + ]); +}); + +it('can ignore events', function () { + app()->make(EventInstrumentation::class)->register([ + 'ignored' => ['test-event'], + ]); + + withRootSpan(function () { + event('test-event', 'value'); + }); + + $rootSpan = Arr::last(getRecordedSpans()); + + /** @var \OpenTelemetry\SDK\Trace\Event $event */ + $event = Arr::last($rootSpan->getEvents()); + + expect($event)->toBeNull(); +}); diff --git a/tests/Support/TestEvent.php b/tests/Support/TestEvent.php new file mode 100644 index 0000000..1469b24 --- /dev/null +++ b/tests/Support/TestEvent.php @@ -0,0 +1,10 @@ +