generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
174 additions
and
0 deletions.
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,101 @@ | ||
<?php | ||
|
||
namespace Keepsuit\LaravelOpenTelemetry\Instrumentation; | ||
|
||
use Illuminate\Cache\Events\CacheHit; | ||
use Illuminate\Cache\Events\CacheMissed; | ||
use Illuminate\Cache\Events\KeyForgotten; | ||
use Illuminate\Cache\Events\KeyWritten; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Str; | ||
use Keepsuit\LaravelOpenTelemetry\Facades\Tracer; | ||
|
||
class EventInstrumentation implements Instrumentation | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
protected static array $ignoredEvents = []; | ||
|
||
public function register(array $options): void | ||
{ | ||
static::$ignoredEvents = Arr::get($options, 'ignored', []); | ||
|
||
app('events')->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); | ||
// } | ||
} |
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,58 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Arr; | ||
use Keepsuit\LaravelOpenTelemetry\Instrumentation\EventInstrumentation; | ||
use Keepsuit\LaravelOpenTelemetry\Tests\Support\TestEvent; | ||
|
||
it('records string event', function () { | ||
withRootSpan(function () { | ||
event('test-event', 'value'); | ||
}); | ||
|
||
$rootSpan = Arr::last(getRecordedSpans()); | ||
|
||
/** @var \OpenTelemetry\SDK\Trace\Event $event */ | ||
$event = Arr::last($rootSpan->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(); | ||
}); |
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,10 @@ | ||
<?php | ||
|
||
namespace Keepsuit\LaravelOpenTelemetry\Tests\Support; | ||
|
||
class TestEvent | ||
{ | ||
public function __construct(public string $value) | ||
{ | ||
} | ||
} |