Skip to content

Commit

Permalink
Add transaction handling
Browse files Browse the repository at this point in the history
```php
$ctx = $client->transaction->push('/route/name');
routeName();
$client->transaction->pop($ctx);
```
  • Loading branch information
dcramer committed Sep 29, 2016
1 parent 38dad0f commit 2f3f424
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
11 changes: 7 additions & 4 deletions lib/Raven/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function __construct($options_or_dsn=null, $options=array())
$this->_pending_events = array();
$this->context = new Raven_Context();
$this->breadcrumbs = new Raven_Breadcrumbs();

$this->sdk = Raven_Util::get($options, 'sdk', array(
'name' => 'sentry-php',
'version' => self::VERSION,
Expand All @@ -113,6 +114,11 @@ public function __construct($options_or_dsn=null, $options=array())
$this->_curl_handler = new Raven_CurlHandler($this->get_curl_options());
}

$this->transaction = new Raven_TransactionStack();
if ($this->is_http_request() && isset($_SERVER['REQUEST_URI'])) {
$this->transaction->push($_SERVER['REQUEST_URI']);
}

if (Raven_Util::get($options, 'install_default_breadcrumb_handlers', true)) {
$this->registerDefaultBreadcrumbHandlers();
}
Expand Down Expand Up @@ -416,10 +422,6 @@ public function captureException($exception, $data=null, $logger=null, $vars=nul

if ($data === null) {
$data = array();
} elseif (!is_array($data)) {
$data = array(
'culprit' => (string)$data,
);
}

$exc = $exception;
Expand Down Expand Up @@ -599,6 +601,7 @@ public function get_default_data()
'tags' => $this->tags,
'platform' => 'php',
'sdk' => $this->sdk,
'culprit' => $this->transaction->peek(),
);
}

Expand Down
48 changes: 48 additions & 0 deletions lib/Raven/TransactionStack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/*
* This file is part of Raven.
*
* (c) Sentry Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

class Raven_TransactionStack
{
public function __construct()
{
$this->stack = array();
}

public function clear()
{
$this->stack = array();
}

public function peek()
{
$len = count($this->stack);
if ($len === 0) {
return null;
}
return $this->stack[$len - 1];
}

public function push($context)
{
$this->stack[] = $context;
}

public function pop($context=null)
{
if (!$context) {
return array_pop($this->stack);
}
while (!empty($this->stack)) {
if (array_pop($this->stack) === $context) {
return $context;
}
}
}
}
13 changes: 2 additions & 11 deletions test/Raven/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,17 +435,6 @@ public function testCaptureExceptionHandlesOptionsAsSecondArg()
$this->assertEquals($event['culprit'], 'test');
}

public function testCaptureExceptionHandlesCulpritAsSecondArg()
{
$client = new Dummy_Raven_Client();
$ex = $this->create_exception();
$client->captureException($ex, 'test');
$events = $client->getSentEvents();
$this->assertEquals(count($events), 1);
$event = array_pop($events);
$this->assertEquals($event['culprit'], 'test');
}

public function testCaptureExceptionHandlesExcludeOption()
{
$client = new Dummy_Raven_Client(array(
Expand Down Expand Up @@ -514,6 +503,7 @@ public function testDefaultProcessorsContainSanitizeDataProcessor()
public function testGetDefaultData()
{
$client = new Dummy_Raven_Client();
$client->transaction->push('test');
$expected = array(
'platform' => 'php',
'project' => $client->project,
Expand All @@ -525,6 +515,7 @@ public function testGetDefaultData()
'name' => 'sentry-php',
'version' => $client::VERSION,
),
'culprit' => 'test',
);
$this->assertEquals($expected, $client->get_default_data());
}
Expand Down

0 comments on commit 2f3f424

Please sign in to comment.