-
Notifications
You must be signed in to change notification settings - Fork 10
feat(RelayOpenTelemetry): add db.statement attribute #32
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
Changes from all commits
1fdda35
8e0d3dd
e12e89d
669138b
8c20a1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,6 +62,13 @@ class RelayOpenTelemetry | |
| '_prefix', | ||
| ]; | ||
|
|
||
| /** | ||
| * Maximum string length when formatting commands for OpenTelemetry. | ||
| * | ||
| * @var int | ||
| */ | ||
| protected const MAX_STR_LEN = 100; | ||
|
|
||
| /** | ||
| * Creates a new instance. | ||
| * | ||
|
|
@@ -112,9 +119,13 @@ public function __call(string $name, array $arguments) | |
| return $this->relay->{$name}(...$arguments); | ||
| } | ||
|
|
||
| $operation = strtoupper($name); | ||
| $stmt = $this->fmtCommand($operation, $arguments); | ||
|
|
||
| $span = $this->tracer->spanBuilder('Relay::' . strtolower($name)) | ||
| ->setAttribute('db.operation', strtoupper($name)) | ||
| ->setAttribute('db.system', 'redis') | ||
| ->setAttribute('db.operation', $operation) | ||
| ->setAttribute('db.statement', $stmt) | ||
|
Comment on lines
+127
to
+128
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "This" is
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's also sending invalid data currently. We're working on the stubs to make this easier.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will make it easier to identify various types of methods: https://relay.so/api
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, sounds like I should wait for some input from you.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vmihailenco: We're exploring whether we can use 8.0 attributes in the class stubs. If so we could use reflection to check whether a method should be bypassed ( Instead of maintaining arrays of functions we could parse all class methods once, or each method once when it's executed.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for update. Let me know what changes do you expect here. My 5 cents are that I would not worry about this. I'd say that tracing |
||
| ->setSpanKind(SpanKind::KIND_CLIENT) | ||
| ->startSpan(); | ||
|
|
||
|
|
@@ -152,9 +163,11 @@ public static function __callStatic(string $name, array $arguments) | |
| */ | ||
| public function scan(&$iterator, $match = null, int $count = 0, ?string $type = null) | ||
| { | ||
| $stmt = $this->fmtScan('SCAN', null, $iterator, $match, $count, $type); | ||
| $span = $this->tracer->spanBuilder('Relay::scan') | ||
| ->setAttribute('db.operation', 'SCAN') | ||
| ->setAttribute('db.system', 'redis') | ||
| ->setAttribute('db.operation', 'SCAN') | ||
| ->setAttribute('db.statement', $stmt) | ||
| ->setSpanKind(SpanKind::KIND_CLIENT) | ||
| ->startSpan(); | ||
|
|
||
|
|
@@ -180,9 +193,11 @@ public function scan(&$iterator, $match = null, int $count = 0, ?string $type = | |
| */ | ||
| public function hscan($key, &$iterator, $match = null, int $count = 0) | ||
| { | ||
| $stmt = $this->fmtScan('HSCAN', $key, $iterator, $match, $count); | ||
| $span = $this->tracer->spanBuilder('Relay::hscan') | ||
| ->setAttribute('db.operation', 'HSCAN') | ||
| ->setAttribute('db.system', 'redis') | ||
| ->setAttribute('db.operation', 'HSCAN') | ||
| ->setAttribute('db.statement', $stmt) | ||
| ->setSpanKind(SpanKind::KIND_CLIENT) | ||
| ->startSpan(); | ||
|
|
||
|
|
@@ -208,9 +223,11 @@ public function hscan($key, &$iterator, $match = null, int $count = 0) | |
| */ | ||
| public function sscan($key, &$iterator, $match = null, int $count = 0) | ||
| { | ||
| $stmt = $this->fmtScan('SSCAN', $key, $iterator, $match, $count); | ||
| $span = $this->tracer->spanBuilder('Relay::sscan') | ||
| ->setAttribute('db.operation', 'SSCAN') | ||
| ->setAttribute('db.system', 'redis') | ||
| ->setAttribute('db.operation', 'SSCAN') | ||
| ->setAttribute('db.statement', $stmt) | ||
| ->setSpanKind(SpanKind::KIND_CLIENT) | ||
| ->startSpan(); | ||
|
|
||
|
|
@@ -236,9 +253,11 @@ public function sscan($key, &$iterator, $match = null, int $count = 0) | |
| */ | ||
| public function zscan($key, &$iterator, $match = null, int $count = 0) | ||
| { | ||
| $stmt = $this->fmtScan('ZSCAN', $key, $iterator, $match, $count); | ||
| $span = $this->tracer->spanBuilder('Relay::zscan') | ||
| ->setAttribute('db.operation', 'ZSCAN') | ||
| ->setAttribute('db.system', 'redis') | ||
| ->setAttribute('db.operation', 'ZSCAN') | ||
| ->setAttribute('db.statement', $stmt) | ||
| ->setSpanKind(SpanKind::KIND_CLIENT) | ||
| ->startSpan(); | ||
|
|
||
|
|
@@ -298,9 +317,11 @@ public function executeBufferedTransaction(Transaction $transaction) | |
| ? 'pipeline' | ||
| : 'multi'; | ||
|
|
||
| $span = $this->tracer->spanBuilder('Relay::exec') | ||
| ->setAttribute('db.operation', 'EXEC') | ||
| $stmt = $this->fmtCommands($transaction->commands); | ||
| $span = $this->tracer->spanBuilder('Relay::' . $method) | ||
| ->setAttribute('db.system', 'redis') | ||
| ->setAttribute('db.operation', $method) | ||
| ->setAttribute('db.statement', $stmt) | ||
| ->setSpanKind(SpanKind::KIND_CLIENT) | ||
| ->startSpan(); | ||
|
|
||
|
|
@@ -320,4 +341,99 @@ public function executeBufferedTransaction(Transaction $transaction) | |
| $span->end(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Formats multiple commands for db.statement attribute. | ||
| * | ||
| * @param array<mixed> $commands | ||
| * @return string | ||
| */ | ||
| protected function fmtCommands(array $commands) | ||
| { | ||
| $acc = []; | ||
| $len = 0; | ||
| foreach ($commands as $command) { | ||
| $command = $this->fmtCommand($command[0], $command[1]); | ||
| $len += strlen($command); | ||
| if ($len > 5000) { | ||
| break; | ||
| } | ||
| array_push($acc, $command); | ||
| } | ||
| return implode(PHP_EOL, $acc); | ||
| } | ||
|
|
||
| /** | ||
| * Formats a single command for db.statement attribute. | ||
| * | ||
| * @param string $name | ||
| * @param array<mixed> $arguments | ||
| * @return string | ||
| */ | ||
| protected function fmtCommand(string $operation, array $arguments) | ||
| { | ||
| $acc = [$operation]; | ||
| $len = 0; | ||
| $this->fmtArguments($acc, $len, $arguments); | ||
| return implode(' ', $acc); | ||
| } | ||
|
|
||
| /** | ||
| * Formats passed arguments as strings. | ||
| * | ||
| * @param array<string> $acc | ||
| * @param array<mixed> $arguments | ||
| * @return void | ||
| */ | ||
| protected function fmtArguments(&$acc, int &$len, $arguments) | ||
| { | ||
| foreach ($arguments as $value) { | ||
| if (is_array($value)) { | ||
| $this->fmtArguments($acc, $len, $value); | ||
| continue; | ||
| } | ||
|
|
||
| $str = strval($value); | ||
| if (strlen($str) > self::MAX_STR_LEN) { | ||
| $str = substr($str, 0, self::MAX_STR_LEN - 3) + '...'; | ||
| } | ||
|
|
||
| $len += strlen($str); | ||
| if ($len > 1000) { | ||
| array_push($acc, '...'); | ||
| break; | ||
| } | ||
| array_push($acc, $str); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Formats SCAN command arguments. | ||
| * | ||
| * @param string $name | ||
| * @param mixed $key | ||
| * @param mixed $iterator | ||
| * @param mixed $match | ||
| * @param int $count | ||
| * @param ?string $type | ||
| * @return string | ||
| */ | ||
| protected function fmtScan(string $name, $key, $iterator, $match = null, int $count = 0, ?string $type = null) | ||
| { | ||
| $args = [$name]; | ||
| if ($key) { | ||
| array_push($args, $key); | ||
| } | ||
| array_push($args, $iterator); | ||
| if ($match) { | ||
| array_push($args, 'MATCH', $match); | ||
| } | ||
| if ($count > 0) { | ||
| array_push($args, 'COUNT', $count); | ||
| } | ||
| if ($type) { | ||
| array_push($args, 'TYPE', $type); | ||
| } | ||
| return implode(' ', $args); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.