Skip to content

Commit

Permalink
FEAT: add includeSubContextInEnvironment option
Browse files Browse the repository at this point in the history
...to allow sending just a main FLOW_CONTEXT, w/o sub-context part.

This might be useful to ease filtering by environment in Rollbar panel.
  • Loading branch information
ryzy committed Oct 25, 2016
1 parent 3c580ca commit 9abe336
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
37 changes: 35 additions & 2 deletions Classes/M12/Rollbar/Rollbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,16 @@ public function getRollbarSettings()
{
$settings = $this->settings['rollbarSettings'];
$settings['root'] = rtrim(FLOW_PATH_ROOT, '/');
$settings['environment'] = strtolower($this->environment->getContext()); // Rollbar expects it lowercase
$settings['person_fn'] = [$this, 'getPersonData'];

$settings['environment'] = $this->getEnvironment();
if ($this->getEnvironmentStr() !== $this->getEnvironment()) {
// If includeSubContextInEnvironment=false and we have a sub-context in FLOW_CONTEXT, add it here.
// Note: currently there's no possibility to send arbitrary payload via PHP
// so include that information in _SERVER[argv], which is included in all reports.
$_SERVER['argv']['FLOW_CONTEXT'] = (string)$this->environment->getContext();
}

return $settings;
}

Expand All @@ -94,11 +101,16 @@ public function getRollbarSettings()
public function getRollbarJsSettings()
{
$jsSettings = $this->settings['rollbarJsSettings'];
$jsSettings['payload']['environment'] = strtolower($this->environment->getContext());
if (($personData = $this->getPersonData())) {
$jsSettings['payload']['person'] = $personData;
}

$jsSettings['payload']['environment'] = $this->getEnvironment();
if ($this->getEnvironmentStr() !== $this->getEnvironment()) {
// If includeSubContextInEnvironment=false and we have a sub-context in FLOW_CONTEXT, add it here.
$jsSettings['payload']['FLOW_CONTEXT'] = (string)$this->environment->getContext();
}

return $jsSettings;
}

Expand All @@ -121,4 +133,25 @@ public function getPersonData()
? [ 'id' => $account->getAccountIdentifier() ]
: [];
}

/**
* Get environment name from FLOW_CONTEXT (lowercase - that's what Rollbar expects)
* based on `includeSubContextInEnvironment` option
*
* @return string
*/
public function getEnvironment()
{
return $this->settings['includeSubContextInEnvironment']
? $this->getEnvironmentStr()
: explode('/', $this->getEnvironmentStr())[0];
}

/**
* @return string Full FLOW_CONTEXT, lower-cased
*/
protected function getEnvironmentStr()
{
return strtolower($this->environment->getContext());
}
}
4 changes: 4 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ M12:

# Path to Rollbar html/js template
templatePath: 'resource://M12.Rollbar/Private/Templates/Rollbar.html'

# Assuming FLOW_CONTEXT contains sub-context, should it be sent in the `environment` key?
# When set to false *and* sub-context *is* present, whole FLOW_CONTEXT will be sent as a separate metadata.
includeSubContextInEnvironment: false
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ M12:
# Path to Rollbar html/js template
templatePath: 'resource://M12.Rollbar/Private/Templates/Rollbar.html'
# Assuming FLOW_CONTEXT contains sub-context, should it be sent in the `environment` key?
# When set to false *and* sub-context *is* present, whole FLOW_CONTEXT will be sent as a separate metadata.
includeSubContextInEnvironment: false
```


Expand Down
17 changes: 17 additions & 0 deletions Tests/Functional/RollbarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace M12\Rollbar\Tests\Functional;

use M12\Rollbar\Rollbar;
use TYPO3\Flow\Reflection\ObjectAccess;
use TYPO3\Flow\Security\Account;

class RollbarTest extends \TYPO3\Flow\Tests\FunctionalTestCase
Expand Down Expand Up @@ -96,4 +97,20 @@ public function getRollbarSettings_whenSecurityContextInitialised()
$this->assertFalse(empty($person));
$this->assertEquals($person['id'], $account->getAccountIdentifier());
}

/**
* @test
*/
public function getEnvironment()
{
$this->assertEquals('testing', $this->rollbar->getEnvironment());

// switch setting `includeSubContextInEnvironment`
$settings = ObjectAccess::getProperty($this->rollbar, 'settings', true);
$settings['includeSubContextInEnvironment'] = true;
ObjectAccess::setProperty($this->rollbar, 'settings', $settings, true);

$this->assertTrue(ObjectAccess::getProperty($this->rollbar, 'settings', true)['includeSubContextInEnvironment']);
$this->assertEquals('testing', $this->rollbar->getEnvironment());
}
}

0 comments on commit 9abe336

Please sign in to comment.