Permalink
Browse files

feature #27343 [Messenger][Profiler] Show dispatch caller (ogizanagi)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[Messenger][Profiler] Show dispatch caller

| Q             | A
| ------------- | ---
| Branch?       | 4.2 <!-- see below -->
| Bug fix?      | no
| New feature?  | yes <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes <!-- please add some, will be required by reviewers -->
| Fixed tickets | N/A  <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A

Just something I missed on my checklist in the first PR:

![mai-22-2018 19-51-52](https://user-images.githubusercontent.com/2211145/40380514-a0ba3326-5df9-11e8-9dd6-82a42dc7ccae.gif)

Commits
-------

1c2f43f [Messenger][Profiler] Show dispatch caller
  • Loading branch information...
sroze committed May 31, 2018
2 parents c8ce780 + 1c2f43f commit 59375662a3d7f020abc31b673ccbe2b76dbba7b7
@@ -55,7 +55,8 @@
.message-bus .badge.status-some-errors { line-height: 16px; border-bottom: 2px solid #B0413E; }
.message-item .sf-toggle-content.sf-toggle-visible { display: table-row-group; }
.message-item tbody.sf-toggle-content.sf-toggle-visible { display: table-row-group; }
td.message-bus-dispatch-caller { background: #f1f2f3; }
</style>
{% endblock %}
@@ -100,12 +101,12 @@
{% macro render_bus_messages(messages, showBus = false) %}
{% set discr = random() %}
{% for i, dispatchCall in messages %}
{% for dispatchCall in messages %}
<table class="message-item">
<thead>
<tr>
<th colspan="2" class="sf-toggle"
data-toggle-selector="#message-item-{{ discr }}-{{ i }}-details"
data-toggle-selector="#message-item-{{ discr }}-{{ loop.index0 }}-details"
data-toggle-initial="{{ loop.first ? 'display' }}"
>
<span class="dump-inline">{{ profiler_dump(dispatchCall.message.type) }}</span>
@@ -122,7 +123,31 @@
</th>
</tr>
</thead>
<tbody id="message-item-{{ discr }}-{{ i }}-details" class="sf-toggle-content">
<tbody id="message-item-{{ discr }}-{{ loop.index0 }}-details" class="sf-toggle-content">
<tr>
<td colspan="2" class="message-bus-dispatch-caller">
<span class="metadata">In
{% set caller = dispatchCall.caller %}
{% if caller.line %}
{% set link = caller.file|file_link(caller.line) %}
{% if link %}
<a href="{{ link }}" title="{{ caller.file }}">{{ caller.name }}</a>
{% else %}
<abbr title="{{ caller.file }}">{{ caller.name }}</abbr>
{% endif %}
{% else %}
{{ caller.name }}
{% endif %}
line <a class="text-small sf-toggle" data-toggle-selector="#sf-trace-{{ discr }}-{{ loop.index0 }}">{{ caller.line }}</a>
</span>
<div class="hidden" id="sf-trace-{{ discr }}-{{ loop.index0 }}">
<div class="trace">
{{ caller.file|file_excerpt(caller.line) }}
</div>
</div>
</td>
</tr>
{% if showBus %}
<tr>
<td class="text-bold">Bus</td>
@@ -914,6 +914,27 @@ table.logs .metadata {
background: rgba(255, 255, 153, 0.5);
}
{# Messenger panel
========================================================================= #}
#collector-content .message-bus .trace {
border: 1px solid #DDD;
background: #FFF;
padding: 10px;
margin: 0.5em 0;
overflow: auto;
}
#collector-content .message-bus .trace {
font-size: 12px;
}
#collector-content .message-bus .trace li {
margin-bottom: 0;
padding: 0;
}
#collector-content .message-bus .trace li.selected {
background: rgba(255, 255, 153, 0.5);
}
{# Dump panel
========================================================================= #}
#collector-content .sf-dump {
@@ -97,6 +97,7 @@ private function collectMessage(string $busName, array $tracedMessage)
'type' => new ClassStub(\get_class($message)),
'value' => $message,
),
'caller' => $tracedMessage['caller'],
);
if (array_key_exists('result', $tracedMessage)) {
@@ -59,6 +59,7 @@ public function testHandle($returnedValue, $expected)
public function getHandleTestData()
{
$file = __FILE__;
$messageDump = <<<DUMP
"bus" => "default"
"envelopeItems" => null
@@ -68,12 +69,17 @@ public function getHandleTestData()
-message: "dummy message"
}
]
"caller" => array:3 [
"name" => "MessengerDataCollectorTest.php"
"file" => "$file"
"line" => %d
]
DUMP;
yield 'no returned value' => array(
null,
<<<DUMP
array:4 [
array:5 [
$messageDump
"result" => array:2 [
"type" => "NULL"
@@ -86,7 +92,7 @@ public function getHandleTestData()
yield 'scalar returned value' => array(
'returned value',
<<<DUMP
array:4 [
array:5 [
$messageDump
"result" => array:2 [
"type" => "string"
@@ -99,7 +105,7 @@ public function getHandleTestData()
yield 'array returned value' => array(
array('returned value'),
<<<DUMP
array:4 [
array:5 [
$messageDump
"result" => array:2 [
"type" => "array"
@@ -124,6 +130,7 @@ public function testHandleWithException()
$collector->registerBus('default', $bus);
try {
$line = __LINE__ + 1;
$bus->dispatch($message);
} catch (\Throwable $e) {
// Ignore.
@@ -134,8 +141,9 @@ public function testHandleWithException()
$messages = $collector->getMessages();
$this->assertCount(1, $messages);
$file = __FILE__;
$this->assertStringMatchesFormat(<<<DUMP
array:4 [
array:5 [
"bus" => "default"
"envelopeItems" => null
"message" => array:2 [
@@ -144,6 +152,11 @@ public function testHandleWithException()
-message: "dummy message"
}
]
"caller" => array:3 [
"name" => "MessengerDataCollectorTest.php"
"file" => "$file"
"line" => $line
]
"exception" => array:2 [
"type" => "RuntimeException"
"value" => RuntimeException %A
@@ -28,12 +28,18 @@ public function testItTracesResult()
$bus->expects($this->once())->method('dispatch')->with($message)->willReturn($result = array('foo' => 'bar'));
$traceableBus = new TraceableMessageBus($bus);
$line = __LINE__ + 1;
$this->assertSame($result, $traceableBus->dispatch($message));
$this->assertCount(1, $tracedMessages = $traceableBus->getDispatchedMessages());
$this->assertArraySubset(array(
'message' => $message,
'result' => $result,
'envelopeItems' => null,
'caller' => array(
'name' => 'TraceableMessageBusTest.php',
'file' => __FILE__,
'line' => $line,
),
), $tracedMessages[0], true);
}
@@ -45,12 +51,18 @@ public function testItTracesResultWithEnvelope()
$bus->expects($this->once())->method('dispatch')->with($envelope)->willReturn($result = array('foo' => 'bar'));
$traceableBus = new TraceableMessageBus($bus);
$line = __LINE__ + 1;
$this->assertSame($result, $traceableBus->dispatch($envelope));
$this->assertCount(1, $tracedMessages = $traceableBus->getDispatchedMessages());
$this->assertArraySubset(array(
'message' => $message,
'result' => $result,
'envelopeItems' => array($envelopeItem),
'caller' => array(
'name' => 'TraceableMessageBusTest.php',
'file' => __FILE__,
'line' => $line,
),
), $tracedMessages[0], true);
}
@@ -64,6 +76,7 @@ public function testItTracesExceptions()
$traceableBus = new TraceableMessageBus($bus);
try {
$line = __LINE__ + 1;
$traceableBus->dispatch($message);
} catch (\RuntimeException $e) {
$this->assertSame($exception, $e);
@@ -74,6 +87,11 @@ public function testItTracesExceptions()
'message' => $message,
'exception' => $exception,
'envelopeItems' => null,
'caller' => array(
'name' => 'TraceableMessageBusTest.php',
'file' => __FILE__,
'line' => $line,
),
), $tracedMessages[0], true);
}
}
@@ -29,6 +29,7 @@ public function __construct(MessageBusInterface $decoratedBus)
*/
public function dispatch($message)
{
$caller = $this->getCaller();
$callTime = microtime(true);
$messageToTrace = $message instanceof Envelope ? $message->getMessage() : $message;
$envelopeItems = $message instanceof Envelope ? array_values($message->all()) : null;
@@ -41,6 +42,7 @@ public function dispatch($message)
'message' => $messageToTrace,
'result' => $result,
'callTime' => $callTime,
'caller' => $caller,
);
return $result;
@@ -50,6 +52,7 @@ public function dispatch($message)
'message' => $messageToTrace,
'exception' => $e,
'callTime' => $callTime,
'caller' => $caller,
);
throw $e;
@@ -65,4 +68,40 @@ public function reset()
{
$this->dispatchedMessages = array();
}
private function getCaller(): array
{
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 8);
$file = $trace[1]['file'];
$line = $trace[1]['line'];
for ($i = 2; $i < 8; ++$i) {
if (isset($trace[$i]['class'], $trace[$i]['function'])
&& 'dispatch' === $trace[$i]['function']
&& is_a($trace[$i]['class'], MessageBusInterface::class, true)
) {
$file = $trace[$i]['file'];
$line = $trace[$i]['line'];
while (++$i < 8) {
if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && 0 !== strpos(
$trace[$i]['function'],
'call_user_func'
)) {
$file = $trace[$i]['file'];
$line = $trace[$i]['line'];
break;
}
}
break;
}
}
$name = str_replace('\\', '/', $file);
$name = substr($name, strrpos($name, '/') + 1);
return compact('name', 'file', 'line');
}
}

0 comments on commit 5937566

Please sign in to comment.