Skip to content

Commit

Permalink
improve performance tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
rommelfreddy committed Aug 8, 2024
1 parent e7dbc18 commit 97573d4
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 149 deletions.
34 changes: 34 additions & 0 deletions Model/PerformanceTracingDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace JustBetter\Sentry\Model;

use Sentry\State\Scope;
use Sentry\Tracing\Span;

class PerformanceTracingDto
{
public function __construct(
private Scope $scope,
private ?Span $parentSpan = null,
private ?Span $span = null
)
{
}

public function getScope(): Scope
{
return $this->scope;
}

public function getParentSpan(): ?Span
{
return $this->parentSpan;
}

public function getSpan(): ?Span
{
return $this->span;
}
}
22 changes: 19 additions & 3 deletions Model/SentryInteraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,36 @@

// phpcs:disable Magento2.Functions.DiscouragedFunction

use JustBetter\Sentry\Helper\Data;
use Throwable;

use function Sentry\captureException;
use function Sentry\init;

class SentryInteraction
{
public function initialize($config)
public function __construct(
private Data $sentryHelper
)
{
}

public function initialize($config): void
{
init($config);
}

public function captureException(\Throwable $ex)
public function captureException(Throwable $ex): void
{
if (!$this->sentryHelper->shouldCaptureException($ex)) {
return;
}

ob_start();
captureException($ex);
try {
captureException($ex);
} catch (Throwable) {
}
ob_end_clean();
}
}
50 changes: 31 additions & 19 deletions Model/SentryPerformance.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\ObjectManagerInterface;
use Sentry\SentrySdk;
use Sentry\Tracing\Span;
use Sentry\Tracing\SpanContext;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Sentry\Tracing\TransactionSource;

use Throwable;
use function Sentry\startTransaction;
use function Sentry\trace;

class SentryPerformance
{
Expand All @@ -31,7 +34,8 @@ public function __construct(
private HttpRequest $request,
private ObjectManagerInterface $objectManager,
private Data $helper
) {
)
{
}

public function startTransaction(AppInterface $app): void
Expand All @@ -48,14 +52,14 @@ public function startTransaction(AppInterface $app): void
$this->request->getHeader('baggage') ?: ''
);

$requestPath = '/'.ltrim($this->request->getRequestUri(), '/');
$requestPath = '/' . ltrim($this->request->getRequestUri(), '/');

$context->setName($requestPath);
$context->setSource(TransactionSource::url());
$context->setStartTimestamp($requestStartTime);

$context->setData([
'url' => $requestPath,
'url' => $requestPath,
'method' => strtoupper($this->request->getMethod()),
]);

Expand All @@ -68,8 +72,6 @@ public function startTransaction(AppInterface $app): void
}

$this->transaction = $transaction;

// Set the current transaction as the current span so we can retrieve it later
SentrySdk::getCurrentHub()->setSpan($transaction);
}

Expand All @@ -92,7 +94,7 @@ public function finishTransaction(ResponseInterface|int $statusCode): void
}

if ($statusCode instanceof Response) {
$statusCode = (int) $statusCode->getStatusCode();
$statusCode = (int)$statusCode->getStatusCode();
}

if (is_numeric($statusCode)) {
Expand All @@ -113,28 +115,38 @@ public function finishTransaction(ResponseInterface|int $statusCode): void
} elseif ($state->getAreaCode() === 'graphql') {
$this->transaction->setOp('graphql');
} else {
$this->transaction->setOp('other');
$this->transaction->setOp($state->getAreaCode());
}

// Finish the transaction, this submits the transaction and it's span to Sentry
$this->transaction->finish();
try {
// Finish the transaction, this submits the transaction and it's span to Sentry
$this->transaction->finish();
} catch (Throwable) {
}

$this->transaction = null;
}

public function addSqlQuery($sql, $startTime, $endTime = null): void
public static function traceStart(SpanContext $context): PerformanceTracingDto
{
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
if (!$parentSpan) {
return;
$scope = SentrySdk::getCurrentHub()->pushScope();
$span = null;

$parentSpan = $scope->getSpan();
if ($parentSpan !== null && $parentSpan->getSampled()) {
$span = $parentSpan->startChild($context);
$scope->setSpan($span);
}

$context = new SpanContext();
$context->setOp('db.sql.query');
$context->setDescription($sql);
$context->setStartTimestamp($startTime);
$context->setEndTimestamp($endTime ?: microtime(true));
return new PerformanceTracingDto($scope, $parentSpan, $span);
}

$parentSpan->startChild($context);
public static function traceEnd(PerformanceTracingDto $context): void
{
if ($context->getSpan()) {
$context->getSpan()->finish();
$context->getScope()->setSpan($context->getParentSpan());
}
SentrySdk::getCurrentHub()->popScope();
}
}
22 changes: 7 additions & 15 deletions Plugin/GlobalExceptionCatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Magento\Framework\DataObject;
use Magento\Framework\DataObjectFactory;
use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
use Sentry\Tracing\SpanContext;
use Throwable;
use function Sentry\trace;

class GlobalExceptionCatcher
{
Expand Down Expand Up @@ -71,22 +74,11 @@ public function aroundLaunch(AppInterface $subject, callable $proceed)

try {
return $response = $proceed();
} catch (\Throwable $ex) {
try {
if ($this->sentryHelper->shouldCaptureException($ex)) {
$this->sentryInteraction->captureException($ex);
}
} catch (\Throwable $bigProblem) {
// do nothing if sentry fails
}

throw $ex;
} catch (Throwable $exception) {
$this->sentryInteraction->captureException($exception);
throw $exception;
} finally {
try {
$this->sentryPerformance->finishTransaction($response ?? 500);
} catch (\Throwable $bigProblem) {
// do nothing if sentry fails
}
$this->sentryPerformance->finishTransaction($response ?? 500);
}
}
}
44 changes: 0 additions & 44 deletions Plugin/LoggerProxyPlugin.php

This file was deleted.

37 changes: 37 additions & 0 deletions Plugin/Profiling/DbQueryLoggerPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace JustBetter\Sentry\Plugin\Profiling;

use JustBetter\Sentry\Model\PerformanceTracingDto;
use JustBetter\Sentry\Model\SentryPerformance;
use Magento\Framework\DB\LoggerInterface;
use Sentry\Tracing\SpanContext;

use function Sentry\trace;

class DbQueryLoggerPlugin
{
private ?PerformanceTracingDto $tracingDto = null;

public function beforeStartTimer(LoggerInterface $subject): void
{
$this->tracingDto = SentryPerformance::traceStart(
SpanContext::make()
->setOp('db.sql.query')
->setStartTimestamp(microtime(true))
);
}

public function beforeLogStats(LoggerInterface $subject, $type, $sql, $bind = [], $result = null): void
{
if ($this->tracingDto === null) {
return;
}

$this->tracingDto->getSpan()?->setDescription($sql);
SentryPerformance::traceEnd($this->tracingDto);
$this->tracingDto = null;
}
}
Loading

0 comments on commit 97573d4

Please sign in to comment.