Skip to content

Commit

Permalink
Add runtime information (#564)
Browse files Browse the repository at this point in the history
* add php runtime information
* add more tests
* also support user provided runtime context
* change array_merge order so [runtime][name|version] is overrideable
* add test for overriden runtime
  • Loading branch information
hjanuschka authored and stayallive committed Mar 15, 2018
1 parent 351cb63 commit 9d84277
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/Raven/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,10 @@ public function capture($data, $stack = null, $vars = null)
unset($data['site']);
}

$existing_runtime_context = isset($data['contexts']['runtime']) ? $data['contexts']['runtime'] : array();
$runtime_context = array('version' => PHP_VERSION, 'name' => 'php');
$data['contexts']['runtime'] = array_merge($runtime_context, $existing_runtime_context);

if (!$this->breadcrumbs->is_empty()) {
$data['breadcrumbs'] = $this->breadcrumbs->fetch();
}
Expand Down
92 changes: 92 additions & 0 deletions test/Raven/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,98 @@ public function testCaptureMessageWithUserContext()
), $event['user']);
}

/**
* @covers Raven_Client::capture
*/
public function testRuntimeContext()
{
$client = new Dummy_Raven_Client();

$client->captureMessage('test');
$events = $client->getSentEvents();
$event = array_pop($events);
$this->assertEquals(PHP_VERSION, $event['contexts']['runtime']['version']);
$this->assertEquals('php', $event['contexts']['runtime']['name']);
}

/**
* @covers Raven_Client::capture
*/
public function testRuntimeOnCustomContext()
{
$client = new Dummy_Raven_Client();

$data = array('contexts' => array(
'mine' => array(
'line' => 1216,
'stack' => array(
1, array(
'foo' => 'bar',
'level4' => array(array('level5', 'level5 a'), 2),
), 3
),
),
));

$client->captureMessage('test', array(), $data);

$events = $client->getSentEvents();
$event = array_pop($events);
$this->assertEquals(PHP_VERSION, $event['contexts']['runtime']['version']);
$this->assertEquals('php', $event['contexts']['runtime']['name']);
$this->assertEquals(1216, $event['contexts']['mine']['line']);
}

/**
* @covers Raven_Client::capture
*/
public function testRuntimeOnOverrideRuntimeItself()
{
$client = new Dummy_Raven_Client();

$data = array('contexts' => array(
'runtime' => array(
'name' => 'sentry',
'version' => '0.1.1-alpha.1'
),
));

$client->captureMessage('test', array(), $data);

$events = $client->getSentEvents();
$event = array_pop($events);
$this->assertEquals('0.1.1-alpha.1', $event['contexts']['runtime']['version']);
$this->assertEquals('sentry', $event['contexts']['runtime']['name']);
}

/**
* @covers Raven_Client::capture
*/
public function testRuntimeOnExistingRuntimeContext()
{
$client = new Dummy_Raven_Client();

$data = array('contexts' => array(
'runtime' => array(
'line' => 1216,
'stack' => array(
1, array(
'foo' => 'bar',
'level4' => array(array('level5', 'level5 a'), 2),
), 3
),
),
));

$client->captureMessage('test', array(), $data);

$events = $client->getSentEvents();
$event = array_pop($events);
$this->assertEquals(PHP_VERSION, $event['contexts']['runtime']['version']);
$this->assertEquals('php', $event['contexts']['runtime']['name']);
$this->assertEquals(1216, $event['contexts']['runtime']['line']);
}

/**
* @covers Raven_Client::captureMessage
*/
Expand Down

0 comments on commit 9d84277

Please # to comment.