Skip to content
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

Improve tracing #387

Merged
merged 9 commits into from Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Sentry/Laravel/EventHandler.php
Expand Up @@ -278,14 +278,16 @@ private function addQueryBreadcrumb($query, $bindings, $time, $connectionName)
$data['bindings'] = $bindings;
}

$transaction = SentrySdk::getCurrentHub()->getTransaction();
if (null !== $transaction) {
$parentSpan = Integration::currentTracingSpan();

if (null !== $parentSpan) {
$context = new SpanContext();
$context->setOp('sql.query');
$context->setDescription($query);
$context->setStartTimestamp(microtime(true) - $time / 1000);
$context->setEndTimestamp($context->getStartTimestamp() + $time / 1000);
$transaction->startChild($context);

$parentSpan->startChild($context);
}

Integration::addBreadcrumb(new Breadcrumb(
Expand Down
6 changes: 3 additions & 3 deletions src/Sentry/Laravel/PublishConfigCommand.php
Expand Up @@ -63,7 +63,6 @@ public function setEnvironmentValue(array $values)

if (count($values) > 0) {
foreach ($values as $envKey => $envValue) {

$str .= "\n"; // In case the searched variable is in the last line without \n
$keyPosition = strpos($str, "{$envKey}=");
$endOfLinePosition = strpos($str, "\n", $keyPosition);
Expand All @@ -75,12 +74,13 @@ public function setEnvironmentValue(array $values)
} else {
$str = str_replace($oldLine, "{$envKey}={$envValue}", $str);
}

}
}

$str = substr($str, 0, -1);
if (!file_put_contents($envFile, $str)) return false;
if (!file_put_contents($envFile, $str)) {
return false;
}
return true;
}
}
27 changes: 27 additions & 0 deletions src/Sentry/Laravel/Tracing/Middleware.php
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Sentry\SentrySdk;
use Sentry\State\Hub;
use Sentry\Tracing\SpanContext;
Expand All @@ -18,6 +19,13 @@ class Middleware
*/
protected $transaction;

/**
* The span for the `app.handle` part of the application.
*
* @var \Sentry\Tracing\Span|null
*/
protected $appSpan;

/**
* Handle an incoming request.
*
Expand Down Expand Up @@ -46,6 +54,18 @@ public function handle($request, Closure $next)
public function terminate($request, $response): void
{
if ($this->transaction !== null && app()->bound('sentry')) {
if ($this->appSpan !== null) {
$this->appSpan->finish();
}

// Make sure we set the transaction and not have a child span in the Sentry SDK
// If the transaction is not on the scope during finish, the trace.context is wrong
SentrySdk::getCurrentHub()->setSpan($this->transaction);

if ($response instanceof Response) {
$this->transaction->setHttpStatus($response->status());
}

$this->transaction->finish();
}
}
Expand Down Expand Up @@ -81,6 +101,13 @@ private function startTransaction(Request $request, Hub $sentry): void
$spanContextStart->setStartTimestamp(defined('LARAVEL_START') ? LARAVEL_START : $request->server('REQUEST_TIME_FLOAT', $fallbackTime));
$spanContextStart->setEndTimestamp(microtime(true));
$this->transaction->startChild($spanContextStart);

$appContextStart = new SpanContext();
$appContextStart->setOp('app.handle');
$appContextStart->setStartTimestamp(microtime(true));
$this->appSpan = $this->transaction->startChild($appContextStart);

SentrySdk::getCurrentHub()->setSpan($this->appSpan);
});
}
}
Expand Down
1 change: 0 additions & 1 deletion src/Sentry/Laravel/Tracing/ServiceProvider.php
Expand Up @@ -53,7 +53,6 @@ private function wrapViewEngine(Engine $realEngine): Engine
/** @var ViewFactory $viewFactory */
$viewFactory = $this->app->make('view');

/** @noinspection UnusedFunctionResultInspection */
$viewFactory->composer('*', static function (View $view) use ($viewFactory) : void {
$viewFactory->share(ViewEngineDecorator::SHARED_KEY, $view->name());
});
Expand Down
11 changes: 9 additions & 2 deletions src/Sentry/Laravel/Tracing/ViewEngineDecorator.php
@@ -1,12 +1,12 @@
<?php


namespace Sentry\Laravel\Tracing;

use Illuminate\Contracts\View\Engine;
use Illuminate\View\Compilers\CompilerInterface;
use Illuminate\View\Factory;
use Sentry\Laravel\Integration;
use Sentry\SentrySdk;
use Sentry\Tracing\SpanContext;

final class ViewEngineDecorator implements Engine
Expand Down Expand Up @@ -42,15 +42,22 @@ public function get($path, array $data = []): string

$span = $parentSpan->startChild($context);

$scope = SentrySdk::getCurrentHub()->pushScope();
$scope->setSpan($span);

$result = $this->engine->get($path, $data);

$span->finish();

SentrySdk::getCurrentHub()->popScope();

return $result;
}

/**
* Laravel uses this function internally
* Laravel uses this function internally.
*
* @internal
*/
public function getCompiler(): CompilerInterface
{
Expand Down