Skip to content

Adding a way to extend global context variables #255

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

Closed
wants to merge 3 commits into from
Closed
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
15 changes: 15 additions & 0 deletions docs/usage/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ While a user is logged in, you can tell Sentry to associate errors with user dat

If at any point, the user becomes unauthenticated, you can call ``Raven.setUserContext()`` with no arguments to remove their data. *This would only really be useful in a large web app where the user logs in/out without a page reload.*

If you wish to add additional data to the existing user data, you can call ``Raven.addUserContext()``.

.. code-block:: javascript

Raven.addUserContext({
name: 'Matt'
})

Capturing a specific message
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -96,6 +104,13 @@ You can also set context variables globally to be merged in with future exceptio
Raven.setExtraContext({ foo: "bar" })
Raven.setTagsContext({ key: "value" })

If you wish to add additional data to existing context variables, you can call ``addExtraContext`` and ``addTagsContext``.

.. code-block:: javascript

Raven.addExtraContext({bar: 'foo'});
Raven.addTagsContext({key: 'value'});


Getting back an event id
~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
42 changes: 42 additions & 0 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,20 @@ var Raven = {
return Raven;
},

/**
* Add additional user information to be sent with the payload.
*
* @param {object} opts Attributes object
* @returns {Raven}
*/
addUserContext: function(opts) {
if (isObject(globalUser) && isObject(opts)) {
globalUser = objectMerge(globalUser, opts);
}

return Raven;
},

/*
* Set/clear a user to be sent along with the payload.
*
Expand All @@ -286,6 +300,20 @@ var Raven = {
return Raven;
},

/**
* Add additional attributes to extra data to be sent with payload
*
* @param {object} opts Attributes object
* @returns {Raven}
*/
addExtraContext: function(opts) {
if (isObject(opts)) {
globalOptions.extra = objectMerge(globalOptions.extra, opts);
}

return Raven;
},

/*
* Set extra attributes to be sent along with the payload.
*
Expand All @@ -298,6 +326,20 @@ var Raven = {
return Raven;
},

/**
* Add additional attributes to tags data to be sent with payload
*
* @param {object} opts Attributes object
* @returns {Raven}
*/
addTagsContext: function(opts) {
if (isObject(opts)) {
globalOptions.tags = objectMerge(globalOptions.tags, opts);
}

return Raven;
},

/*
* Set tags to be sent along with the payload.
*
Expand Down
26 changes: 25 additions & 1 deletion test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1521,12 +1521,36 @@ describe('Raven (public API)', function() {
});

it('should clear globalOptions.tags with no arguments', function() {
globalOptions = {name: 'Matt'};
globalOptions.tags = {name: 'Matt'};
Raven.setTagsContext();
assert.deepEqual(globalOptions.tags, {});
});
});

describe('.addUserContext', function() {
it('should add additional attributes to the globalUser object', function() {
globalUser = {name: "Jake"};
Raven.addUserContext({id: 1234});
assert.deepEqual(globalUser, {name: "Jake", id: 1234});
});
});

describe('.addExtraContext', function() {
it('should add additional attributes to the globalOptions.extra object', function() {
globalOptions.extra = {name: "Jake"};
Raven.addExtraContext({id: 1234});
assert.deepEqual(globalOptions.extra, {name: "Jake", id: 1234});
});
});

describe('.addTagsContext', function() {
it('should add additional attributes to the globalOptions.tags object', function() {
globalOptions.tags = {name: "Jake"};
Raven.addTagsContext({id: 1234});
assert.deepEqual(globalOptions.tags, {name: "Jake", id: 1234});
});
});

describe('.captureMessage', function() {
it('should work as advertised', function() {
this.sinon.stub(window, 'send');
Expand Down