Skip to content

Adding a breadcrumbCallback for filtering and cleaning #788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,23 @@ Those configuration options are documented below:
}
}

.. describe:: breadcrumbCallback

A function that allows filtering or mutating breadcrumb payloads.
Return false to throw away the breadcrumb.

.. code-block:: javascript

{
breadcrumbCallback: function(crumb) {
if (crumb.type === 'http') {
return crumb;
}

return false;
}
}

.. describe:: shouldSendCallback

A callback function that allows you to apply your own filters to
Expand Down
26 changes: 26 additions & 0 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,16 @@ Raven.prototype = {
timestamp: now() / 1000
}, obj);

if (isFunction(this._globalOptions.breadcrumbCallback)) {
var result = this._globalOptions.breadcrumbCallback(crumb);

if (isObject(result) && !isEmptyObject(result)) {
crumb = result;
} else if (result === false) {
return this;
}
}

this._breadcrumbs.push(crumb);
if (this._breadcrumbs.length > this._globalOptions.maxBreadcrumbs) {
this._breadcrumbs.shift();
Expand Down Expand Up @@ -530,6 +540,22 @@ Raven.prototype = {
return this;
},

/*
* Set the breadcrumbCallback option
*
* @param {function} callback The callback to run which allows filtering
* or mutating breadcrumbs
* @return {Raven}
*/
setBreadcrumbCallback: function(callback) {
var original = this._globalOptions.breadcrumbCallback;
this._globalOptions.breadcrumbCallback = isFunction(callback)
? function (data) { return callback(data, original); }
: callback;

return this;
},

/*
* Set the shouldSendCallback option
*
Expand Down
222 changes: 222 additions & 0 deletions test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1961,6 +1961,41 @@ describe('Raven (public API)', function() {
});
});

describe('.setBreadcrumbCallback', function() {
it('should set the globalOptions.breadcrumbCallback attribute', function() {
var foo = sinon.stub();
Raven.setBreadcrumbCallback(foo);

// note that breadcrumbCallback creates a callback/closure around
// foo, so can't test for equality - just verify that calling the wrapper
// also calls foo
Raven._globalOptions.breadcrumbCallback();
assert.isTrue(foo.calledOnce);
});

it('should clear globalOptions.breadcrumbCallback with no arguments', function() {
var foo = function(){};
Raven._globalOptions.breadcrumbCallback = foo;
Raven.setBreadcrumbCallback();
assert.isUndefined(Raven._globalOptions.breadcrumbCallback);
});

it('should generate a wrapper that passes the prior callback as the 2nd argument', function () {
var foo = sinon.stub();
var bar = sinon.spy(function(data, orig) {
assert.equal(orig, foo);
foo();
});
Raven._globalOptions.breadcrumbCallback = foo;
Raven.setBreadcrumbCallback(bar);
Raven._globalOptions.breadcrumbCallback({
'a': 1 // "data"
});
assert.isTrue(bar.calledOnce);
assert.isTrue(foo.calledOnce);
});
});

describe('.setShouldSendCallback', function() {
it('should set the globalOptions.shouldSendCallback attribute', function() {
var foo = sinon.stub();
Expand Down Expand Up @@ -2216,6 +2251,193 @@ describe('Raven (public API)', function() {
{ message: 'lol', timestamp: 0.1 }
]);
});

describe('breadcrumbCallback', function() {
it('should filter the breadcrumb if it returns false', function() {
Raven.setBreadcrumbCallback(function() {
return false;
});

Raven.captureBreadcrumb({
type: 'http',
data: {
url: 'http://example.org/api/0/auth/',
status_code: 200
}
});

assert.strictEqual(Raven._breadcrumbs.length, 0);
});

it('should not filter the breadcrumb if callback returns undefined', function() {
Raven.setBreadcrumbCallback(function() {});

Raven.captureBreadcrumb({
type: 'http',
data: {
url: 'http://example.org/api/0/auth/',
status_code: 200
}
});

assert.deepEqual(Raven._breadcrumbs, [{
type: 'http',
timestamp: 0.1,
data: {
url: 'http://example.org/api/0/auth/',
status_code: 200
}
}]);
});

it('should mutate the breadcrumb if it returns an object', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"replace" is a better descriptor than "mutate" – you can mutate the input crumb and not return anything, different from replacing the value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test below this is for replacing, this test probably shouldn't return in the callback to actually test mutation.

Raven.setBreadcrumbCallback(function(crumb) {
crumb.type = 'whee';
return crumb;
});

Raven.captureBreadcrumb({
type: 'http',
data: {
url: 'http://example.org/api/0/auth/',
status_code: 200
}
});

assert.deepEqual(Raven._breadcrumbs, [{
type: 'whee',
timestamp: 0.1,
data: {
url: 'http://example.org/api/0/auth/',
status_code: 200
}
}]);
});

it('should enable replacing the breadcrumb', function() {
Raven.setBreadcrumbCallback(function() {
return {
foo: 'bar'
}
});

Raven.captureBreadcrumb({
type: 'http',
data: {
url: 'http://example.org/api/0/auth/',
status_code: 200
}
});

assert.deepEqual(Raven._breadcrumbs[0], {
foo: 'bar'
});
});

it('should not replace the breadcrumb if a non object is returned', function() {
Raven.setBreadcrumbCallback(function() {
return 'foo';
});

Raven.captureBreadcrumb({
type: 'http',
data: {
url: 'http://example.org/api/0/auth/',
status_code: 200
}
});

assert.deepEqual(Raven._breadcrumbs, [{
type: 'http',
timestamp: 0.1,
data: {
url: 'http://example.org/api/0/auth/',
status_code: 200
}
}]);
});

it('should not replace the breadcrumb if an empty object is returned', function() {
Raven.setBreadcrumbCallback(function() {
return {};
});

Raven.captureBreadcrumb({
type: 'http',
data: {
url: 'http://example.org/api/0/auth/',
status_code: 200
}
});

assert.deepEqual(Raven._breadcrumbs, [{
type: 'http',
timestamp: 0.1,
data: {
url: 'http://example.org/api/0/auth/',
status_code: 200
}
}]);
});

it('should call the callback with the breadcrumb object', function() {
var callback = this.sinon.stub().returnsArg(0);
Raven.setBreadcrumbCallback(callback);

Raven.captureBreadcrumb({
type: 'http',
data: {
url: 'http://example.org/api/0/auth/',
status_code: 200
}
});

assert.isTrue(callback.calledWith({
type: 'http',
timestamp: 0.1,
data: {
url: 'http://example.org/api/0/auth/',
status_code: 200
}
}));
});

it('should enable filtering one breadcrumb but not another', function() {
function callback(data) {
if (data.data.url === 'example') {
return false;
}

return data;
}

Raven.setBreadcrumbCallback(callback);
Raven.captureBreadcrumb({
type: 'http',
data: {
url: 'example',
status_code: 200
}
});

Raven.captureBreadcrumb({
type: 'http',
data: {
url: 'foo',
status_code: 301
}
});

assert.deepEqual(Raven._breadcrumbs, [{
type: 'http',
timestamp: 0.1,
data: {
url: 'foo',
status_code: 301
}
}]);
});
});
});

describe('._captureUrlChange', function () {
Expand Down
3 changes: 3 additions & 0 deletions typescript/raven.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ interface RavenStatic {
/** Specify a function that allows mutation of the data payload right before being sent to Sentry. */
setDataCallback(data: any, orig?: any): RavenStatic;

/** Specify a callback function that allows you to mutate or filter breadcrumbs when they are captured. */
setBreadcrumbCallback(data: any, orig?: any): RavenStatic;

/** Specify a callback function that allows you to apply your own filters to determine if the message should be sent to Sentry. */
setShouldSendCallback(data: any, orig?: any): RavenStatic;

Expand Down