From 9abe33611dbb0dfed019939775e1bba8dca8dec5 Mon Sep 17 00:00:00 2001 From: ryzy Date: Tue, 25 Oct 2016 13:16:52 +0200 Subject: [PATCH] FEAT: add `includeSubContextInEnvironment` option ...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. --- Classes/M12/Rollbar/Rollbar.php | 37 ++++++++++++++++++++++++++++++-- Configuration/Settings.yaml | 4 ++++ README.md | 4 ++++ Tests/Functional/RollbarTest.php | 17 +++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/Classes/M12/Rollbar/Rollbar.php b/Classes/M12/Rollbar/Rollbar.php index 9a78041..a4eb308 100644 --- a/Classes/M12/Rollbar/Rollbar.php +++ b/Classes/M12/Rollbar/Rollbar.php @@ -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; } @@ -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; } @@ -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()); + } } diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index 0bf60ed..13e5374 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -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 diff --git a/README.md b/README.md index 2f4512c..bc463d9 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/Tests/Functional/RollbarTest.php b/Tests/Functional/RollbarTest.php index 2430640..105f513 100644 --- a/Tests/Functional/RollbarTest.php +++ b/Tests/Functional/RollbarTest.php @@ -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 @@ -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()); + } }