Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e507cd0
commit 53886fa
Showing
11 changed files
with
337 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,39 @@ | ||
<?php | ||
|
||
namespace Sentry\Laravel; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
abstract class BaseServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Abstract type to bind Sentry as in the Service Container. | ||
* | ||
* @var string | ||
*/ | ||
public static $abstract = 'sentry'; | ||
|
||
/** | ||
* Check if a DSN was set in the config. | ||
* | ||
* @return bool | ||
*/ | ||
protected function hasDsnSet(): bool | ||
{ | ||
$config = $this->getUserConfig(); | ||
|
||
return !empty($config['dsn']); | ||
} | ||
|
||
/** | ||
* Retrieve the user configuration. | ||
* | ||
* @return array | ||
*/ | ||
protected function getUserConfig(): array | ||
{ | ||
$config = $this->app['config'][static::$abstract]; | ||
|
||
return empty($config) ? [] : $config; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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,157 @@ | ||
<?php | ||
|
||
namespace Sentry\Laravel\Tracing; | ||
|
||
use Exception; | ||
use Illuminate\Contracts\Events\Dispatcher; | ||
use Illuminate\Database\Events\QueryExecuted; | ||
use Illuminate\Routing\Events\RouteMatched; | ||
use Illuminate\Routing\Route; | ||
use RuntimeException; | ||
use Sentry\Laravel\Integration; | ||
use Sentry\SentrySdk; | ||
use Sentry\Tracing\SpanContext; | ||
use Sentry\Tracing\Transaction; | ||
|
||
class EventHandler | ||
{ | ||
/** | ||
* Map event handlers to events. | ||
* | ||
* @var array | ||
*/ | ||
protected static $eventHandlerMap = [ | ||
'router.matched' => 'routerMatched', // Until Laravel 5.1 | ||
RouteMatched::class => 'routeMatched', // Since Laravel 5.2 | ||
|
||
'illuminate.query' => 'query', // Until Laravel 5.1 | ||
QueryExecuted::class => 'queryExecuted', // Since Laravel 5.2 | ||
]; | ||
|
||
/** | ||
* The Laravel event dispatcher. | ||
* | ||
* @var \Illuminate\Contracts\Events\Dispatcher | ||
*/ | ||
private $events; | ||
|
||
/** | ||
* EventHandler constructor. | ||
* | ||
* @param \Illuminate\Contracts\Events\Dispatcher $events | ||
*/ | ||
public function __construct(Dispatcher $events) | ||
{ | ||
$this->events = $events; | ||
} | ||
|
||
/** | ||
* Attach all event handlers. | ||
*/ | ||
public function subscribe(): void | ||
{ | ||
foreach (static::$eventHandlerMap as $eventName => $handler) { | ||
$this->events->listen($eventName, [$this, $handler]); | ||
} | ||
} | ||
|
||
/** | ||
* Pass through the event and capture any errors. | ||
* | ||
* @param string $method | ||
* @param array $arguments | ||
*/ | ||
public function __call($method, $arguments) | ||
{ | ||
$handlerMethod = $handlerMethod = "{$method}Handler"; | ||
|
||
if (!method_exists($this, $handlerMethod)) { | ||
throw new RuntimeException("Missing tracing event handler: {$handlerMethod}"); | ||
} | ||
|
||
$parentSpan = Integration::currentTracingSpan(); | ||
|
||
// If there is no tracing span active there is no need to handle the event | ||
if ($parentSpan === null) { | ||
return; | ||
} | ||
|
||
try { | ||
call_user_func_array([$this, $handlerMethod], $arguments); | ||
} catch (Exception $exception) { | ||
// Ignore | ||
} | ||
} | ||
|
||
/** | ||
* Until Laravel 5.1 | ||
* | ||
* @param \Illuminate\Routing\Route $route | ||
*/ | ||
protected function routerMatchedHandler(Route $route): void | ||
{ | ||
$transaction = SentrySdk::getCurrentHub()->getTransaction(); | ||
|
||
if ($transaction instanceof Transaction) { | ||
$routeName = Integration::extractNameForRoute($route) ?? '<unlabeled transaction>'; | ||
|
||
$transaction->setName($routeName); | ||
$transaction->setData([ | ||
'action' => $route->getActionName(), | ||
'name' => $route->getName(), | ||
]); | ||
} | ||
} | ||
|
||
/** | ||
* Since Laravel 5.2 | ||
* | ||
* @param \Illuminate\Routing\Events\RouteMatched $match | ||
*/ | ||
protected function routeMatchedHandler(RouteMatched $match): void | ||
{ | ||
$this->routerMatchedHandler($match->route); | ||
} | ||
|
||
/** | ||
* Until Laravel 5.1 | ||
* | ||
* @param string $query | ||
* @param array $bindings | ||
* @param int $time | ||
* @param string $connectionName | ||
*/ | ||
protected function queryHandler($query, $bindings, $time, $connectionName): void | ||
{ | ||
$this->recordQuerySpan($query, $time); | ||
} | ||
|
||
/** | ||
* Since Laravel 5.2 | ||
* | ||
* @param \Illuminate\Database\Events\QueryExecuted $query | ||
*/ | ||
protected function queryExecutedHandler(QueryExecuted $query): void | ||
{ | ||
$this->recordQuerySpan($query->sql, $query->time); | ||
} | ||
|
||
/** | ||
* Helper to add an query breadcrumb. | ||
* | ||
* @param string $query | ||
* @param float|null $time | ||
*/ | ||
private function recordQuerySpan($query, $time): void | ||
{ | ||
$parentSpan = Integration::currentTracingSpan(); | ||
|
||
$context = new SpanContext(); | ||
$context->setOp('sql.query'); | ||
$context->setDescription($query); | ||
$context->setStartTimestamp(microtime(true) - $time / 1000); | ||
$context->setEndTimestamp($context->getStartTimestamp() + $time / 1000); | ||
|
||
$parentSpan->startChild($context); | ||
} | ||
} |
Oops, something went wrong.