Skip to content

Commit

Permalink
record events
Browse files Browse the repository at this point in the history
  • Loading branch information
cappuc committed Apr 13, 2023
1 parent a58c136 commit 91f020b
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/opentelemetry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
],
],

/**
Expand Down
101 changes: 101 additions & 0 deletions src/Instrumentation/EventInstrumentation.php
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);
// }
}
58 changes: 58 additions & 0 deletions tests/Instrumentation/EventInstrumentation.php
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();
});
10 changes: 10 additions & 0 deletions tests/Support/TestEvent.php
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)
{
}
}

0 comments on commit 91f020b

Please sign in to comment.