-
Notifications
You must be signed in to change notification settings - Fork 117
Feat: Add Redis watcher #293
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
Merged
brettmc
merged 8 commits into
open-telemetry:main
from
MilesChou:add-laravel-redis-connection
Sep 23, 2024
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
be6cafb
Feat: Add Redis watcher
MilesChou 5227a8c
Update src/Instrumentation/Laravel/src/Watchers/RedisCommand/RedisCom…
MilesChou 014b591
Return null when get db index
MilesChou 86fe930
Renaming
MilesChou 633a56e
Fix return null statement
MilesChou 7af92f0
Fix return null statement in fetchDbHost()
MilesChou dc9b046
Use DB_STATEMENT for compatibility with semconv 1.24.
MilesChou 5d3c2ec
Remove unused class
MilesChou 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 hidden or 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
107 changes: 107 additions & 0 deletions
107
src/Instrumentation/Laravel/src/Watchers/RedisCommand/RedisCommandWatcher.php
This file contains hidden or 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,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\RedisCommand; | ||
|
||
use Illuminate\Contracts\Foundation\Application; | ||
use Illuminate\Redis\Connections\Connection; | ||
use Illuminate\Redis\Connections\PhpRedisConnection; | ||
use Illuminate\Redis\Connections\PredisConnection; | ||
use Illuminate\Redis\Events\CommandExecuted; | ||
use OpenTelemetry\API\Instrumentation\CachedInstrumentation; | ||
use OpenTelemetry\API\Trace\SpanKind; | ||
use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\Watcher; | ||
use OpenTelemetry\SemConv\TraceAttributes; | ||
use OpenTelemetry\SemConv\TraceAttributeValues; | ||
use RangeException; | ||
use RuntimeException; | ||
use Throwable; | ||
|
||
/** | ||
* Watch the Redis Command event | ||
* | ||
* Call facade `Redis::enableEvents()` before using this watcher | ||
*/ | ||
class RedisCommandWatcher extends Watcher | ||
{ | ||
public function __construct( | ||
private CachedInstrumentation $instrumentation, | ||
) { | ||
} | ||
|
||
/** @psalm-suppress UndefinedInterfaceMethod */ | ||
public function register(Application $app): void | ||
{ | ||
/** @phan-suppress-next-line PhanTypeArraySuspicious */ | ||
$app['events']->listen(CommandExecuted::class, [$this, 'recordRedisCommand']); | ||
} | ||
|
||
/** | ||
* Record a Redis command. | ||
*/ | ||
/** @psalm-suppress UndefinedThisPropertyFetch */ | ||
public function recordRedisCommand(CommandExecuted $event): void | ||
{ | ||
$nowInNs = (int) (microtime(true) * 1E9); | ||
|
||
$operationName = strtoupper($event->command); | ||
|
||
/** @psalm-suppress ArgumentTypeCoercion */ | ||
$span = $this->instrumentation->tracer() | ||
->spanBuilder($operationName) | ||
->setSpanKind(SpanKind::KIND_CLIENT) | ||
->setStartTimestamp($this->calculateQueryStartTime($nowInNs, $event->time)) | ||
->startSpan(); | ||
|
||
// See https://opentelemetry.io/docs/specs/semconv/database/redis/ | ||
$attributes = [ | ||
TraceAttributes::DB_SYSTEM => TraceAttributeValues::DB_SYSTEM_REDIS, | ||
TraceAttributes::DB_NAME => $this->fetchDbIndex($event->connection), | ||
TraceAttributes::DB_OPERATION => $operationName, | ||
TraceAttributes::DB_STATEMENT => Serializer::serializeCommand($event->command, $event->parameters), | ||
TraceAttributes::SERVER_ADDRESS => $this->fetchDbHost($event->connection), | ||
]; | ||
|
||
/** @psalm-suppress PossiblyInvalidArgument */ | ||
$span->setAttributes($attributes); | ||
$span->end($nowInNs); | ||
} | ||
|
||
private function calculateQueryStartTime(int $nowInNs, float $queryTimeMs): int | ||
{ | ||
return (int) ($nowInNs - ($queryTimeMs * 1E6)); | ||
} | ||
|
||
private function fetchDbIndex(Connection $connection): ?int | ||
{ | ||
try { | ||
if ($connection instanceof PhpRedisConnection) { | ||
return $connection->client()->getDbNum(); | ||
} elseif ($connection instanceof PredisConnection) { | ||
/** @psalm-suppress PossiblyUndefinedMethod */ | ||
return $connection->client()->getConnection()->getParameters()->database; | ||
} | ||
|
||
return null; | ||
} catch (Throwable $e) { | ||
return null; | ||
} | ||
} | ||
|
||
private function fetchDbHost(Connection $connection): ?string | ||
{ | ||
try { | ||
if ($connection instanceof PhpRedisConnection) { | ||
return $connection->client()->getHost(); | ||
} elseif ($connection instanceof PredisConnection) { | ||
/** @psalm-suppress PossiblyUndefinedMethod */ | ||
return $connection->client()->getConnection()->getParameters()->host; | ||
} | ||
|
||
return null; | ||
} catch (Throwable $e) { | ||
return null; | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/Instrumentation/Laravel/src/Watchers/RedisCommand/Serializer.php
This file contains hidden or 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,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\RedisCommand; | ||
|
||
/** | ||
* @see https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/packages/opentelemetry-redis-common/src/index.ts | ||
*/ | ||
class Serializer | ||
{ | ||
/** | ||
* List of regexes and the number of arguments that should be serialized for matching commands. | ||
* For example, HSET should serialize which key and field it's operating on, but not its value. | ||
* Setting the subset to -1 will serialize all arguments. | ||
* Commands without a match will have their first argument serialized. | ||
* | ||
* Refer to https://redis.io/commands/ for the full list. | ||
*/ | ||
private const SERIALIZATION_SUBSETS = [ | ||
[ | ||
'regex' => '/^ECHO/i', | ||
'args' => 0, | ||
], | ||
[ | ||
'regex' => '/^(LPUSH|MSET|PFA|PUBLISH|RPUSH|SADD|SET|SPUBLISH|XADD|ZADD)/i', | ||
'args' => 1, | ||
], | ||
[ | ||
'regex' => '/^(HSET|HMSET|LSET|LINSERT)/i', | ||
'args' => 2, | ||
], | ||
[ | ||
'regex' => '/^(ACL|BIT|B[LRZ]|CLIENT|CLUSTER|CONFIG|COMMAND|DECR|DEL|EVAL|EX|FUNCTION|GEO|GET|HINCR|HMGET|HSCAN|INCR|L[TRLM]|MEMORY|P[EFISTU]|RPOP|S[CDIMORSU]|XACK|X[CDGILPRT]|Z[CDILMPRS])/i', | ||
'args' => -1, | ||
], | ||
]; | ||
|
||
/** | ||
* Given the redis command name and arguments, return a combination of the | ||
* command name + the allowed arguments according to `SERIALIZATION_SUBSETS`. | ||
* | ||
* @param string $command The redis command name | ||
* @param array $params The redis command arguments | ||
* @return string A combination of the command name + args according to `SERIALIZATION_SUBSETS`. | ||
*/ | ||
public static function serializeCommand(string $command, array $params): string | ||
{ | ||
if (count($params) === 0) { | ||
return $command; | ||
} | ||
|
||
$paramsToSerializeNum = 0; | ||
|
||
// Find the number of arguments to serialize for the given command | ||
foreach (self::SERIALIZATION_SUBSETS as $subset) { | ||
if (preg_match($subset['regex'], $command)) { | ||
$paramsToSerializeNum = $subset['args']; | ||
|
||
break; | ||
} | ||
} | ||
|
||
// Serialize the allowed number of arguments | ||
$paramsToSerialize = ($paramsToSerializeNum >= 0) ? array_slice($params, 0, $paramsToSerializeNum) : $params; | ||
|
||
// If there are more arguments than serialized, add a placeholder | ||
if (count($params) > count($paramsToSerialize)) { | ||
$paramsToSerialize[] = '[' . (count($params) - $paramsToSerializeNum) . ' other arguments]'; | ||
} | ||
|
||
return $command . ' ' . implode(' ', $paramsToSerialize); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Instrumentation/Laravel/tests/Unit/Watches/RedisCommand/SerializerTest.php
This file contains hidden or 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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Unit\Watches\RedisCommand; | ||
|
||
use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\RedisCommand\Serializer; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SerializerTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider serializeCases | ||
*/ | ||
public function testSerialize($command, $params, $expected): void | ||
{ | ||
$this->assertSame($expected, Serializer::serializeCommand($command, $params)); | ||
} | ||
|
||
public function serializeCases(): iterable | ||
{ | ||
// Only serialize command | ||
yield ['ECHO', ['param1'], 'ECHO [1 other arguments]']; | ||
|
||
// Only serialize 1 params | ||
yield ['SET', ['param1', 'param2'], 'SET param1 [1 other arguments]']; | ||
yield ['SET', ['param1', 'param2', 'param3'], 'SET param1 [2 other arguments]']; | ||
|
||
// Only serialize 2 params | ||
yield ['HSET', ['param1', 'param2', 'param3'], 'HSET param1 param2 [1 other arguments]']; | ||
|
||
// Serialize all params | ||
yield ['DEL', ['param1', 'param2', 'param3', 'param4'], 'DEL param1 param2 param3 param4']; | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.