-
Notifications
You must be signed in to change notification settings - Fork 70
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
Performance tracking #106
Draft
barryvdh
wants to merge
6
commits into
justbetter:master
Choose a base branch
from
barryvdh:feat-performance
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+245
−0
Draft
Performance tracking #106
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
476cf9d
Add performance sampling
barryvdh 69499b6
Add initial performance tracking
barryvdh 5519e4a
Use Response plugin for finishing
barryvdh 56a24bb
Add SQL queries
barryvdh 8bd5139
CS
barryvdh 248d6b5
TWeak DB logging
barryvdh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JustBetter\Sentry\Model; | ||
|
||
// phpcs:disable Magento2.Functions.DiscouragedFunction | ||
|
||
use Magento\Framework\App\Request\Http as HttpRequest; | ||
use Magento\Framework\App\Response\Http as HttpResponse; | ||
use Magento\Framework\App\ResponseInterface; | ||
use Sentry\SentrySdk; | ||
use Sentry\Tracing\SpanContext; | ||
use Sentry\Tracing\Transaction; | ||
use Sentry\Tracing\TransactionContext; | ||
use Sentry\Tracing\TransactionSource; | ||
|
||
class SentryPerformance | ||
{ | ||
/** @var Transaction|null */ | ||
private $transaction; | ||
|
||
/** @var \Magento\Framework\App\ResourceConnection */ | ||
private $resourceConnection; | ||
|
||
public function __construct(\Magento\Framework\App\ResourceConnection $resourceConnection) | ||
{ | ||
$this->resourceConnection = $resourceConnection; | ||
} | ||
|
||
public function startTransaction(HttpRequest $request) | ||
{ | ||
$requestStartTime = $request->getServer('REQUEST_TIME_FLOAT', microtime(true)); | ||
|
||
$context = TransactionContext::fromHeaders( | ||
$request->getHeader('sentry-trace') ?: '', | ||
$request->getHeader('baggage') ?: '' | ||
); | ||
|
||
$requestPath = '/'.ltrim($request->getRequestUri(), '/'); | ||
|
||
$context->setOp('http.server'); | ||
$context->setName($requestPath); | ||
$context->setSource(TransactionSource::url()); | ||
$context->setStartTimestamp($requestStartTime); | ||
|
||
$context->setData([ | ||
'url' => $requestPath, | ||
'method' => strtoupper($request->getMethod()), | ||
]); | ||
|
||
// Start the transaction | ||
$transaction = \Sentry\startTransaction($context); | ||
|
||
// If this transaction is not sampled, don't set it either and stop doing work from this point on | ||
if (!$transaction->getSampled()) { | ||
return; | ||
} | ||
|
||
$this->transaction = $transaction; | ||
|
||
// Set the current transaction as the current span so we can retrieve it later | ||
\Sentry\SentrySdk::getCurrentHub()->setSpan($transaction); | ||
} | ||
|
||
/** | ||
* @param ResponseInterface|Response $response | ||
*/ | ||
public function finishTransaction(ResponseInterface $response) | ||
{ | ||
if ($this->transaction) { | ||
if ($response instanceof HttpResponse) { | ||
$this->transaction->setHttpStatus($response->getStatusCode()); | ||
} | ||
|
||
// Finish the transaction, this submits the transaction and it's span to Sentry | ||
$this->transaction->finish(); | ||
|
||
$this->transaction = null; | ||
} | ||
} | ||
|
||
public function addSqlQuery($sql, $startTime, $endTime = null) | ||
{ | ||
$parentSpan = SentrySdk::getCurrentHub()->getSpan(); | ||
if (!$parentSpan) { | ||
return; | ||
} | ||
|
||
$context = new SpanContext(); | ||
$context->setOp('db.sql.query'); | ||
$context->setDescription($sql); | ||
$context->setStartTimestamp($startTime); | ||
$context->setEndTimestamp($endTime ?: microtime(true)); | ||
|
||
$parentSpan->startChild($context); | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace JustBetter\Sentry\Observer; | ||
|
||
use JustBetter\Sentry\Model\SentryPerformance; | ||
use Magento\Framework\App\Request\Http; | ||
use Magento\Framework\Event\ObserverInterface; | ||
|
||
class ControllerActionPredispatchObserver implements ObserverInterface | ||
{ | ||
/** @var SentryPerformance */ | ||
private $sentryPerformance; | ||
|
||
/** @var Http */ | ||
private $request; | ||
|
||
public function __construct(SentryPerformance $sentryPerformance, Http $request) | ||
{ | ||
$this->sentryPerformance = $sentryPerformance; | ||
$this->request = $request; | ||
} | ||
|
||
public function execute(\Magento\Framework\Event\Observer $observer) | ||
{ | ||
$this->sentryPerformance->startTransaction($this->request); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might it be a good idea to add the check wether we're doing performance tracking in these places as well? To ensure no code related to performance tracking is executed when it's disabled |
||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
namespace JustBetter\Sentry\Plugin; | ||
|
||
use JustBetter\Sentry\Model\SentryPerformance; | ||
use Magento\Framework\DB\Logger\LoggerProxy; | ||
|
||
/** | ||
* Plugin to add DB Queries from the ProxyLogger to Sentry | ||
*/ | ||
class LoggerProxyPlugin | ||
{ | ||
/** @var SentryPerformance */ | ||
private $sentryPerformance; | ||
|
||
/** @var float|null */ | ||
private $timer; | ||
|
||
public function __construct(SentryPerformance $sentryPerformance) | ||
{ | ||
$this->sentryPerformance = $sentryPerformance; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function beforeStartTimer() | ||
{ | ||
$this->timer = microtime(true); | ||
} | ||
|
||
/** | ||
* @param LoggerProxy $subject | ||
* @param string $type | ||
* @param string $sql | ||
* @param array $bind | ||
* @param \Zend_Db_Statement_Pdo|null $result | ||
* @return void | ||
*/ | ||
public function beforeLogStats(LoggerProxy $subject, $type, $sql, $bind = [], $result = null) | ||
{ | ||
$this->sentryPerformance->addSqlQuery($sql, $this->timer); | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace JustBetter\Sentry\Plugin; | ||
|
||
use JustBetter\Sentry\Model\SentryPerformance; | ||
use Magento\Framework\App\Request\Http; | ||
use Magento\Framework\App\ResponseInterface; | ||
|
||
/** | ||
* Plugin to sample request and send them to Sentry | ||
*/ | ||
class SampleRequest | ||
{ | ||
/** @var SentryPerformance */ | ||
private $sentryPerformance; | ||
|
||
/** @var Http */ | ||
private $request; | ||
|
||
public function __construct(SentryPerformance $sentryPerformance, Http $request) | ||
{ | ||
$this->sentryPerformance = $sentryPerformance; | ||
$this->request = $request; | ||
} | ||
|
||
/** | ||
* Add our toolbar to the response. | ||
* | ||
* @param ResponseInterface $response | ||
*/ | ||
public function beforeSendResponse(ResponseInterface $response) | ||
{ | ||
$this->sentryPerformance->finishTransaction($response); | ||
} | ||
} |
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,18 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
|
||
<type name="Magento\Framework\App\ResponseInterface"> | ||
<plugin name="JustBetter\Sentry\Plugin\SampleRequest" | ||
type="JustBetter\Sentry\Plugin\SampleRequest" | ||
sortOrder="99" | ||
/> | ||
</type> | ||
|
||
<type name="Magento\Framework\DB\Logger\LoggerProxy"> | ||
<plugin name="JustBetter\Sentry\Plugin\LoggerProxyPlugin" | ||
type="JustBetter\Sentry\Plugin\LoggerProxyPlugin" | ||
sortOrder="99" | ||
/> | ||
</type> | ||
|
||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> | ||
<event name="controller_action_predispatch"> | ||
<observer name="SentryPredispatch" instance="JustBetter\Sentry\Observer\ControllerActionPredispatchObserver" /> | ||
</event> | ||
</config> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The
$resourceConnection
is not used in this file, can it be removed without issues?