Skip to content

Commit

Permalink
Merge pull request #40 from filipgolonka/transformPayload
Browse files Browse the repository at this point in the history
Allow to provide transformation callback in the client configuration
  • Loading branch information
ftrrtf authored May 15, 2018
2 parents 04da392 + 345dda2 commit 5d5d3ab
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ before_install:

install:
- if [[ $SYMFONY_VERSION != "" ]]; then composer require "symfony/framework-bundle:${SYMFONY_VERSION}" --no-update -n; fi;
- composer update $COMPOSER_FLAGS --prefer-dist --no-interaction
- travis_wait 20 composer update $COMPOSER_FLAGS --prefer-dist --no-interaction

script:
- vendor/bin/phpunit --verbose
Expand Down
3 changes: 3 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public function getConfigTreeBuilder()
->scalarNode('check_ignore_function_provider')
->defaultValue('ftrrtf_rollbar.check_ignore_function_provider.default')
->end()
->scalarNode('transform_payload_function_provider')
->defaultValue('ftrrtf_rollbar.transform_payload_function_provider.default')
->end()
->arrayNode('allowed_js_hosts')
->prototype('scalar')
->end()
Expand Down
8 changes: 8 additions & 0 deletions DependencyInjection/FtrrtfRollbarExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public function load(array $configs, ContainerBuilder $container)
$config['notifier']['client']['check_ignore_function_provider']
);
}

if (isset($config['notifier']['client']['transform_payload_function_provider'])) {
$container->setParameter(
'ftrrtf_rollbar.notifier.client.transform_payload_function_provider',
$config['notifier']['client']['transform_payload_function_provider']
);
}

$loader->load('client.xml');
}

Expand Down
18 changes: 18 additions & 0 deletions Provider/AnonymizeTransformPayloadFunctionProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Ftrrtf\RollbarBundle\Provider;

class AnonymizeTransformPayloadFunctionProvider implements TransformPayloadFunctionProviderInterface
{
/**
* {@inheritdoc}
*/
public function getTransformFunctionCode()
{
return 'function (payload) {
payload.data.person = {};
payload.data.request.user_ip = \'\';
return payload;
}';
}
}
14 changes: 14 additions & 0 deletions Provider/NullTransformPayloadFunctionProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Ftrrtf\RollbarBundle\Provider;

class NullTransformPayloadFunctionProvider implements TransformPayloadFunctionProviderInterface
{
/**
* {@inheritdoc}
*/
public function getTransformFunctionCode()
{
return 'function (payload) { return payload; }';
}
}
14 changes: 14 additions & 0 deletions Provider/TransformPayloadFunctionProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Ftrrtf\RollbarBundle\Provider;

/**
* Interface for "transform" function provider.
*/
interface TransformPayloadFunctionProviderInterface
{
/**
* @return string
*/
public function getTransformFunctionCode();
}
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,18 @@ ftrrtf_rollbar:
code_version: "some version string, such as a version number or git sha"
guess_uncaught_frames: true
rollbarjs_version: "rollbar_js_version" # i.e. v1.7, if empty then the newest available version (v1.x) will be used

# if you want to overwrite checkIgnore function (client js), you have to deliver
# service id which implements Ftrrtf\RollbarBundle\Provider\CheckIgnoreFunctionProviderInterface
# check_ignore_function_provider: my_service_name

# if you want to overwrite transform function (client js), you have to deliver
# service id which implements Ftrrtf\RollbarBundle\Provider\TransformPayloadFunctionProviderInterface
# transform_payload_function_provider: my_service_name

# there is also predefined transformer, which removes user ip and person from the payload
# transform_payload_function_provider: ftrrtf_rollbar.transform_payload_function_provider.anonymize

# optional - if not passed, window.location.origin will be only allowed host
# allowed_js_hosts:
# - http://myhost.mydomain.com
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/client.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<argument>%ftrrtf_rollbar.environment.options%</argument>
<argument type="service" id="ftrrtf_rollbar.helper.user"/>
<argument type="expression">service(parameter('ftrrtf_rollbar.notifier.client.check_ignore_function_provider'))</argument>
<!--<argument type="service" id="ftrrtf_rollbar.check_ignore_function_provider.default" />-->
<argument type="expression">service(parameter('ftrrtf_rollbar.notifier.client.transform_payload_function_provider'))</argument>
<tag name="twig.extension" />
</service>
</services>
Expand Down
6 changes: 6 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
<parameters>
<parameter key="ftrrtf_rollbar.helper.user.class">Ftrrtf\RollbarBundle\Helper\UserHelper</parameter>
<parameter key="ftrrtf_rollbar.check_ignore_function_provider.default.class">Ftrrtf\RollbarBundle\Provider\NullCheckIgnoreFunctionProvider</parameter>
<parameter key="ftrrtf_rollbar.transform_payload_function_provider.default.class">Ftrrtf\RollbarBundle\Provider\NullTransformPayloadFunctionProvider</parameter>
<parameter key="ftrrtf_rollbar.transform_payload_function_provider.anonymize.class">Ftrrtf\RollbarBundle\Provider\AnonymizeTransformPayloadFunctionProvider</parameter>
</parameters>

<services>
<service id="ftrrtf_rollbar.helper.user" class="%ftrrtf_rollbar.helper.user.class%">
</service>
<service id="ftrrtf_rollbar.check_ignore_function_provider.default" class="%ftrrtf_rollbar.check_ignore_function_provider.default.class%">
</service>
<service id="ftrrtf_rollbar.transform_payload_function_provider.default" class="%ftrrtf_rollbar.transform_payload_function_provider.default.class%">
</service>
<service id="ftrrtf_rollbar.transform_payload_function_provider.anonymize" class="%ftrrtf_rollbar.transform_payload_function_provider.anonymize.class%">
</service>
</services>
</container>
1 change: 1 addition & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function configurationDataProvider()
'rollbarjs_version' => 'v1',
'allowed_js_hosts' => array(),
'check_ignore_function_provider' => 'ftrrtf_rollbar.check_ignore_function_provider.default',
'transform_payload_function_provider' => 'ftrrtf_rollbar.transform_payload_function_provider.default',
),
),
),
Expand Down
4 changes: 3 additions & 1 deletion Tests/DependencyInjection/FtrrtfRollbarExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ public function testClientNotifier()
'http://myhost.mydomain.com',
'http://myhost2.mydomain.com',
),
'check_ignore_function_provider' => 'ftrrtf_rollbar.check_ignore_function_provider.default'
'check_ignore_function_provider' => 'ftrrtf_rollbar.check_ignore_function_provider.default',
'transform_payload_function_provider' => 'ftrrtf_rollbar.transform_payload_function_provider.default'
),
'ftrrtf_rollbar.notifier.client.options'
);
Expand All @@ -157,6 +158,7 @@ public function testClientNotifier()
'%ftrrtf_rollbar.environment.options%',
new Reference('ftrrtf_rollbar.helper.user'),
new Expression("service(parameter('ftrrtf_rollbar.notifier.client.check_ignore_function_provider'))"),
new Expression("service(parameter('ftrrtf_rollbar.notifier.client.transform_payload_function_provider'))"),
)
);

Expand Down
22 changes: 17 additions & 5 deletions Twig/RollbarExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Ftrrtf\RollbarBundle\Helper\UserHelper;
use Ftrrtf\RollbarBundle\Provider\CheckIgnoreFunctionProviderInterface;
use Ftrrtf\RollbarBundle\Provider\TransformPayloadFunctionProviderInterface;

/**
* Rollbar twig extension.
Expand Down Expand Up @@ -31,21 +32,29 @@ class RollbarExtension extends \Twig_Extension
private $checkIgnoreFunctionProvider;

/**
* @param array $notifierOptions
* @param array $environmentOptions
* @param UserHelper $userHelper
* @param CheckIgnoreFunctionProviderInterface $checkIgnoreFunctionProvider
* @var TransformPayloadFunctionProviderInterface
*/
private $transformPayloadFunctionProvider;

/**
* @param array $notifierOptions
* @param array $environmentOptions
* @param UserHelper $userHelper
* @param CheckIgnoreFunctionProviderInterface $checkIgnoreFunctionProvider
* @param TransformPayloadFunctionProviderInterface $transformPayloadFunctionProvider
*/
public function __construct(
array $notifierOptions,
array $environmentOptions,
UserHelper $userHelper,
CheckIgnoreFunctionProviderInterface $checkIgnoreFunctionProvider
CheckIgnoreFunctionProviderInterface $checkIgnoreFunctionProvider,
TransformPayloadFunctionProviderInterface $transformPayloadFunctionProvider
) {
$this->notifierOptions = $notifierOptions;
$this->environmentOptions = $environmentOptions;
$this->userHelper = $userHelper;
$this->checkIgnoreFunctionProvider = $checkIgnoreFunctionProvider;
$this->transformPayloadFunctionProvider = $transformPayloadFunctionProvider;
}

/**
Expand Down Expand Up @@ -105,9 +114,12 @@ public function getInitRollbarCode(array $context)

$checkIgnoreConfig = $this->getCheckIgnoreConfig();

$transform = $this->transformPayloadFunctionProvider->getTransformFunctionCode();

return <<<END_HTML
<script>
var _rollbarConfig = {$config};
_rollbarConfig.transform = {$transform};
// Rollbar Snippet
!function(r){function o(e){if(t[e])return t[e].exports;var n=t[e]={exports:{},id:e,loaded:!1};return r[e].call(n.exports,n,n.exports,o),n.loaded=!0,n.exports}var t={};return o.m=r,o.c=t,o.p="",o(0)}([function(r,o,t){"use strict";var e=t(1).Rollbar,n=t(2);_rollbarConfig.rollbarJsUrl=_rollbarConfig.rollbarJsUrl||"https://d37gvrvc0wt4s1.cloudfront.net/js/{$rollbarJsVersion}/rollbar.min.js";var a=e.init(window,_rollbarConfig),i=n(a,_rollbarConfig);a.loadFull(window,document,!_rollbarConfig.async,_rollbarConfig,i)},function(r,o){"use strict";function t(r){return function(){try{return r.apply(this,arguments)}catch(o){try{console.error("[Rollbar]: Internal error",o)}catch(t){}}}}function e(r,o,t){window._rollbarWrappedError&&(t[4]||(t[4]=window._rollbarWrappedError),t[5]||(t[5]=window._rollbarWrappedError._rollbarContext),window._rollbarWrappedError=null),r.uncaughtError.apply(r,t),o&&o.apply(window,t)}function n(r){var o=function(){var o=Array.prototype.slice.call(arguments,0);e(r,r._rollbarOldOnError,o)};return o.belongsToShim=!0,o}function a(r){this.shimId=++s,this.notifier=null,this.parentShim=r,this._rollbarOldOnError=null}function i(r){var o=a;return t(function(){if(this.notifier)return this.notifier[r].apply(this.notifier,arguments);var t=this,e="scope"===r;e&&(t=new o(this));var n=Array.prototype.slice.call(arguments,0),a={shim:t,method:r,args:n,ts:new Date};return window._rollbarShimQueue.push(a),e?t:void 0})}function l(r,o){if(o.hasOwnProperty&&o.hasOwnProperty("addEventListener")){var t=o.addEventListener;o.addEventListener=function(o,e,n){t.call(this,o,r.wrap(e),n)};var e=o.removeEventListener;o.removeEventListener=function(r,o,t){e.call(this,r,o&&o._wrapped?o._wrapped:o,t)}}}var s=0;a.init=function(r,o){var e=o.globalAlias||"Rollbar";if("object"==typeof r[e])return r[e];r._rollbarShimQueue=[],r._rollbarWrappedError=null,o=o||{};var i=new a;return t(function(){if(i.configure(o),o.captureUncaught){i._rollbarOldOnError=r.onerror,r.onerror=n(i);var t,a,s="EventTarget,Window,Node,ApplicationCache,AudioTrackList,ChannelMergerNode,CryptoOperation,EventSource,FileReader,HTMLUnknownElement,IDBDatabase,IDBRequest,IDBTransaction,KeyOperation,MediaController,MessagePort,ModalWindow,Notification,SVGElementInstance,Screen,TextTrack,TextTrackCue,TextTrackList,WebSocket,WebSocketWorker,Worker,XMLHttpRequest,XMLHttpRequestEventTarget,XMLHttpRequestUpload".split(",");for(t=0;t<s.length;++t)a=s[t],r[a]&&r[a].prototype&&l(i,r[a].prototype)}return r[e]=i,i})()},a.prototype.loadFull=function(r,o,e,n,a){var i=function(){var o;if(void 0===r._rollbarPayloadQueue){var t,e,n,i;for(o=new Error("rollbar.js did not load");t=r._rollbarShimQueue.shift();)for(n=t.args,i=0;i<n.length;++i)if(e=n[i],"function"==typeof e){e(o);break}}"function"==typeof a&&a(o)},l=!1,s=o.createElement("script"),u=o.getElementsByTagName("script")[0],p=u.parentNode;s.crossOrigin="",s.src=n.rollbarJsUrl,s.async=!e,s.onload=s.onreadystatechange=t(function(){if(!(l||this.readyState&&"loaded"!==this.readyState&&"complete"!==this.readyState)){s.onload=s.onreadystatechange=null;try{p.removeChild(s)}catch(r){}l=!0,i()}}),p.insertBefore(s,u)},a.prototype.wrap=function(r,o){try{var t;if(t="function"==typeof o?o:function(){return o||{}},"function"!=typeof r)return r;if(r._isWrap)return r;if(!r._wrapped){r._wrapped=function(){try{return r.apply(this,arguments)}catch(o){throw o._rollbarContext=t()||{},o._rollbarContext._wrappedSource=r.toString(),window._rollbarWrappedError=o,o}},r._wrapped._isWrap=!0;for(var e in r)r.hasOwnProperty(e)&&(r._wrapped[e]=r[e])}return r._wrapped}catch(n){return r}};for(var u="log,debug,info,warn,warning,error,critical,global,configure,scope,uncaughtError".split(","),p=0;p<u.length;++p)a.prototype[u[p]]=i(u[p]);r.exports={Rollbar:a,_rollbarWindowOnError:e}},function(r,o){"use strict";r.exports=function(r,o){return function(t){if(!t&&!window._rollbarInitialized){var e=window.RollbarNotifier,n=o||{},a=n.globalAlias||"Rollbar",i=window.Rollbar.init(n,r);i._processShimQueue(window._rollbarShimQueue||[]),window[a]=i,window._rollbarInitialized=!0,e.processPayloads()}}}}]);
// End Rollbar Snippet
Expand Down
40 changes: 31 additions & 9 deletions spec/Ftrrtf/RollbarBundle/Twig/RollbarExtensionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace spec\Ftrrtf\RollbarBundle\Twig;

use Ftrrtf\RollbarBundle\Provider\TransformPayloadFunctionProviderInterface;
use Ftrrtf\RollbarBundle\Tests\Fake\Application;
use Ftrrtf\RollbarBundle\Helper\UserHelper;
use Ftrrtf\RollbarBundle\Provider\CheckIgnoreFunctionProviderInterface;
Expand All @@ -16,9 +17,18 @@ class RollbarExtensionSpec extends ObjectBehavior
{
const EXPECTED_EXTENSION_NAME = 'ftrrtf_rollbar';

function let(UserHelper $helper, CheckIgnoreFunctionProviderInterface $checkIgnoreFunctionProvider)
{
$this->beConstructedWith([], [], $helper, $checkIgnoreFunctionProvider);
function let(
UserHelper $helper,
CheckIgnoreFunctionProviderInterface $checkIgnoreFunctionProvider,
TransformPayloadFunctionProviderInterface $transformPayloadFunctionProvider
) {
$this->beConstructedWith(
[],
[],
$helper,
$checkIgnoreFunctionProvider,
$transformPayloadFunctionProvider
);
}

function it_is_initializable()
Expand All @@ -32,41 +42,53 @@ function it_has_name()
$this->getName()->shouldReturn(self::EXPECTED_EXTENSION_NAME);
}

function it_uses_the_newest_version_of_rollbarjs(UserHelper $helper, Application $app, CheckIgnoreFunctionProviderInterface $checkIgnoreFunctionProvider)
{
function it_uses_the_newest_version_of_rollbarjs(
UserHelper $helper,
Application $app,
CheckIgnoreFunctionProviderInterface $checkIgnoreFunctionProvider,
TransformPayloadFunctionProviderInterface $transformPayloadFunctionProvider
) {
$this->beConstructedWith(
[
'access_token' => 'access_token',
'source_map_enabled' => false,
'allowed_js_hosts' => [],
'check_ignore_function_provider' => null,
'transform_payload_function_provider' => null,
'rollbarjs_version' => 'v1',
],
[
'environment' => 'test',
],
$helper,
$checkIgnoreFunctionProvider
$checkIgnoreFunctionProvider,
$transformPayloadFunctionProvider
);

$this->getInitRollbarCode(['app' => $app])->shouldMatch('/v1/');
}

function it_allows_to_select_rollbarjs_version(UserHelper $helper, Application $app, CheckIgnoreFunctionProviderInterface $checkIgnoreFunctionProvider)
{
function it_allows_to_select_rollbarjs_version(
UserHelper $helper,
Application $app,
CheckIgnoreFunctionProviderInterface $checkIgnoreFunctionProvider,
TransformPayloadFunctionProviderInterface $transformPayloadFunctionProvider
) {
$this->beConstructedWith(
[
'access_token' => 'access_token',
'source_map_enabled' => false,
'allowed_js_hosts' => [],
'check_ignore_function_provider' => null,
'transform_payload_function_provider' => null,
'rollbarjs_version' => 'v1.7',
],
[
'environment' => 'test',
],
$helper,
$checkIgnoreFunctionProvider
$checkIgnoreFunctionProvider,
$transformPayloadFunctionProvider
);

$this->getInitRollbarCode(['app' => $app])->shouldMatch('/v1.7/');
Expand Down

0 comments on commit 5d5d3ab

Please sign in to comment.