-
Notifications
You must be signed in to change notification settings - Fork 117
KIND::CONSUMER instrumentation implemented #307
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 8 commits
018e7fd
66fa258
bdbb36f
730c424
8ad2de6
ea5f1b1
f6a23dd
3e46678
da0cf7b
8204d40
d9338d4
97ffab1
84e16ed
9e81e32
ee4522d
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 |
|---|---|---|
|
|
@@ -36,10 +36,9 @@ public static function register(): void | |
| { | ||
| $instrumentation = new CachedInstrumentation('io.opentelemetry.contrib.php.symfony_messenger'); | ||
|
|
||
| /** | ||
| * MessageBusInterface dispatches messages to the handlers. | ||
| */ | ||
| hook( | ||
|
|
||
| // Instrument MessageBusInterface (message dispatching) | ||
| hook( | ||
| MessageBusInterface::class, | ||
| 'dispatch', | ||
| pre: static function ( | ||
|
|
@@ -54,19 +53,17 @@ public static function register(): void | |
| $message = $params[0]; | ||
| $messageClass = \get_class($message); | ||
|
|
||
| /** @psalm-suppress ArgumentTypeCoercion */ | ||
| // Instrument dispatch as a "send" operation with SpanKind::KIND_PRODUCER | ||
| $builder = $instrumentation | ||
| ->tracer() | ||
| ->spanBuilder(\sprintf('DISPATCH %s', $messageClass)) | ||
| ->setSpanKind(SpanKind::KIND_PRODUCER) | ||
| ->spanBuilder(\sprintf('publish %s', $messageClass)) | ||
|
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. Why not "dispatch"? 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. The span types and names should be consistent with the spec here: But it's kind of confusing TBH, it mentions both "publish" and "send", in the example it gives @brettmc can you clarify the spec wording here maybe? |
||
| ->setSpanKind(SpanKind::KIND_PRODUCER) // Set KIND_PRODUCER for dispatch | ||
| ->setAttribute(TraceAttributes::CODE_FUNCTION, $function) | ||
| ->setAttribute(TraceAttributes::CODE_NAMESPACE, $class) | ||
| ->setAttribute(TraceAttributes::CODE_FILEPATH, $filename) | ||
| ->setAttribute(TraceAttributes::CODE_LINENO, $lineno) | ||
|
|
||
| ->setAttribute(self::ATTRIBUTE_MESSENGER_BUS, $class) | ||
| ->setAttribute(self::ATTRIBUTE_MESSENGER_MESSAGE, $messageClass) | ||
| ; | ||
| ->setAttribute(self::ATTRIBUTE_MESSENGER_MESSAGE, $messageClass); | ||
|
|
||
| $parent = Context::getCurrent(); | ||
| $span = $builder | ||
|
|
@@ -103,9 +100,7 @@ public static function register(): void | |
| } | ||
| ); | ||
|
|
||
| /** | ||
| * SenderInterface sends messages to a transport. | ||
| */ | ||
| // Instrument SenderInterface (sending messages to transport) | ||
| hook( | ||
| SenderInterface::class, | ||
| 'send', | ||
|
|
@@ -121,8 +116,19 @@ public static function register(): void | |
| $envelope = $params[0]; | ||
| $messageClass = \get_class($envelope->getMessage()); | ||
|
|
||
| /** @psalm-suppress ArgumentTypeCoercion */ | ||
| // Instrument sending as a "send" operation with SpanKind::KIND_PRODUCER | ||
| $builder = $instrumentation | ||
| <<<<<<< HEAD | ||
RichardChukwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ->tracer() | ||
| ->spanBuilder(\sprintf('send %s', $messageClass)) | ||
| ->setSpanKind(SpanKind::KIND_PRODUCER) // Set KIND_PRODUCER for sending | ||
| ->setAttribute(TraceAttributes::CODE_FUNCTION, $function) | ||
| ->setAttribute(TraceAttributes::CODE_NAMESPACE, $class) | ||
| ->setAttribute(TraceAttributes::CODE_FILEPATH, $filename) | ||
| ->setAttribute(TraceAttributes::CODE_LINENO, $lineno) | ||
| ->setAttribute(self::ATTRIBUTE_MESSENGER_TRANSPORT, $class) | ||
| ->setAttribute(self::ATTRIBUTE_MESSENGER_MESSAGE, $messageClass); | ||
| ======= | ||
| ->tracer() | ||
| ->spanBuilder(\sprintf('SEND %s', $messageClass)) | ||
| ->setSpanKind(SpanKind::KIND_PRODUCER) | ||
|
|
@@ -134,9 +140,9 @@ public static function register(): void | |
| ->setAttribute(self::ATTRIBUTE_MESSENGER_TRANSPORT, $class) | ||
| ->setAttribute(self::ATTRIBUTE_MESSENGER_MESSAGE, $messageClass) | ||
| ; | ||
| >>>>>>> 78a04cebaeba48d60a00dc1c48653695b926299d | ||
|
|
||
| $parent = Context::getCurrent(); | ||
|
|
||
| $span = $builder | ||
| ->setParent($parent) | ||
| ->startSpan(); | ||
|
|
@@ -168,6 +174,69 @@ public static function register(): void | |
| $span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage()); | ||
| } | ||
|
|
||
| $span->end(); | ||
| } | ||
| ); | ||
|
|
||
| // Instrument the receiving of messages (consumer-side) | ||
| hook( | ||
| SenderInterface::class, | ||
RichardChukwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 'receive', | ||
| pre: static function ( | ||
| SenderInterface $bus, | ||
| array $params, | ||
| string $class, | ||
| string $function, | ||
| ?string $filename, | ||
| ?int $lineno, | ||
| ) use ($instrumentation): array { | ||
| /** @var Envelope $envelope */ | ||
| $envelope = $params[0]; | ||
| $messageClass = \get_class($envelope->getMessage()); | ||
|
|
||
| // Instrument receiving as a "consume" operation with SpanKind::KIND_CONSUMER | ||
| $builder = $instrumentation | ||
| ->tracer() | ||
| ->spanBuilder(\sprintf('consume %s', $messageClass)) | ||
| ->setSpanKind(SpanKind::KIND_CONSUMER) // Set KIND_CONSUMER for receiving | ||
| ->setAttribute(TraceAttributes::CODE_FUNCTION, $function) | ||
| ->setAttribute(TraceAttributes::CODE_NAMESPACE, $class) | ||
| ->setAttribute(TraceAttributes::CODE_FILEPATH, $filename) | ||
| ->setAttribute(TraceAttributes::CODE_LINENO, $lineno) | ||
| ->setAttribute(self::ATTRIBUTE_MESSENGER_TRANSPORT, $class) | ||
| ->setAttribute(self::ATTRIBUTE_MESSENGER_MESSAGE, $messageClass); | ||
|
|
||
| $parent = Context::getCurrent(); | ||
| $span = $builder | ||
| ->setParent($parent) | ||
| ->startSpan(); | ||
|
|
||
| $context = $span->storeInContext($parent); | ||
| Context::storage()->attach($context); | ||
|
|
||
| return $params; | ||
| }, | ||
| post: static function ( | ||
| SenderInterface $sender, | ||
| array $params, | ||
| ?Envelope $result, | ||
| ?\Throwable $exception | ||
| ): void { | ||
| $scope = Context::storage()->scope(); | ||
| if (null === $scope) { | ||
| return; | ||
| } | ||
|
|
||
| $scope->detach(); | ||
| $span = Span::fromContext($scope->context()); | ||
|
|
||
| if (null !== $exception) { | ||
| $span->recordException($exception, [ | ||
| TraceAttributes::EXCEPTION_ESCAPED => true, | ||
| ]); | ||
| $span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage()); | ||
| } | ||
|
|
||
| $span->end(); | ||
| } | ||
| ); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.