-
Notifications
You must be signed in to change notification settings - Fork 160
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
Add support for tracing Dispatched Events in Laravel 5.8+ #1897
Add support for tracing Dispatched Events in Laravel 5.8+ #1897
Conversation
dd3b598
to
890ab21
Compare
These tests made me think, and maybe you're handling this with other code?.... Should this integration be tracking the dispatching of the event, even if there are no listeners to the event? Or, will the datadog ui be smart enough that if there are no listeners, an empty span for a dispatched event won't show up? Unsure what it looked like in pre 5.8 when the method Thanks for your work on this! |
Pre 5.8 it looked exactly like it does in the tests here: dd-trace-php/tests/Integrations/Laravel/V4/CommonScenariosTest.php Lines 57 to 86 in 79fe244
No, the datadog UI does not magically know about there being an event handler. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
Anyway, on the topic of:
Should this integration be tracking the dispatching of the event, even if there are no listeners to the event.
Currently, the way to go is yes.
In particular it isn't that simple to determine whether there's a receiver: it might be broadcast conditionally, there might be listeners on an interface, which are not directly tied to the event itself (Dispatcher::addInterfaceListeners e.g.).
Now the question is, how would we, the tracer, know whether the event actually was dispatched anywhere? There is in fact a way to do that: wrapping any returned Closure from $this->getListeners()
, but that will be visible in stacktraces. Just simply tracing the returned Closures would be an option - but that functionality is going to only be implemented next week.
Anyway, we may consider improving upon that in future, but for now, that's what we're going to do.
Thanks @bwoebi for digging into this PR and taking it to the finish line- and so promptly! |
0.84.0 just has been released, hope it works just as you expect :-) |
We also trace these things: use Illuminate\Queue\Jobs\Job;
\DDTrace\trace_method(Worker::class, 'runJob', function (\DDTrace\SpanData $span, $args) {
/** @var Job $job */
$job = $args[0];
$span->service = 'worker';
$span->resource = $job->resolveName();
$span->name = 'laravel.queue';
$span->meta['job.name'] = $job->getName();
$span->meta['job.id'] = $job->getJobId();
$span->meta['connection'] = $job->getConnectionName();
$span->meta['queue'] = $job->getQueue();
$span->meta['attempts'] = $job->attempts();
});
\DDTrace\trace_method(Dispatcher::class, 'dispatchNow', function (\DDTrace\SpanData $span, $args) {
$job = $args[0];
$span->service = 'job';
$span->name = 'laravel.job';
$span->resource = class_basename($job);
if (method_exists($job, 'context')) {
$context = $job->context();
if (is_array($context)) {
foreach ($context as $key => $value) {
if (null !== $value) {
$span->meta[$key] = (string) $value;
}
}
}
}
}); and another one for our cron/command dispatcher but that one is completely custom. |
Sorry for dredging this up but after updating our staging environment, this caused a bit of trouble for us. Just in case anyone comes here trying to figure out how to reduce the insane amount of spans this produces... \DD_Trace\trace_method('Illuminate\Events\Dispatcher', 'dispatch', function ($span) {
//$span->resource contains the event name, you can use this
//to decide if you want to drop the span and its children
return false;
}); We added some logic in the closure to only drop the noise from Laravel events and keep our own events being tracked. $span->resource holds the event name. As mentioned here it's not perfect but seemed to do the trick for us.
|
Description
Add the appropriate 5.8+ Event Dispatcher method for tracing. See: https://laravel.com/docs/5.8/upgrade#events
Readiness checklist
Reviewer checklist