Skip to content

Add environment configuration #661

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 1 commit into from
Jul 26, 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
16 changes: 16 additions & 0 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,19 @@ Raven.prototype = {
return JSON.parse(JSON.stringify(this._globalContext));
},


/*
* Set environment of application
*
* @param {string} environment Typically something like 'production'.
* @return {Raven}
*/
setEnvironment: function(environment) {
this._globalOptions.environment = environment;

return this;
},

/*
* Set release version of application
*
Expand Down Expand Up @@ -1185,6 +1198,9 @@ Raven.prototype = {
data.user = this._globalContext.user;
}

// Include the environment if it's defined in globalOptions
if (globalOptions.environment) data.environment = globalOptions.environment;

// Include the release if it's defined in globalOptions
if (globalOptions.release) data.release = globalOptions.release;

Expand Down
46 changes: 46 additions & 0 deletions test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,39 @@ describe('globals', function() {
});
});

it('should attach environment if available', function() {
this.sinon.stub(Raven, 'isSetup').returns(true);
this.sinon.stub(Raven, '_makeRequest');
this.sinon.stub(Raven, '_getHttpData').returns({
url: 'http://localhost/?a=b',
headers: {'User-Agent': 'lolbrowser'}
});

Raven._globalOptions = {
projectId: 2,
logger: 'javascript',
maxMessageLength: 100,
environment: 'abc123'
};

Raven._send({message: 'bar'});
assert.deepEqual(Raven._makeRequest.lastCall.args[0].data, {
project: '2',
environment: 'abc123',
logger: 'javascript',
platform: 'javascript',
request: {
url: 'http://localhost/?a=b',
headers: {
'User-Agent': 'lolbrowser'
}
},
event_id: 'abc123',
message: 'bar',
extra: {'session:duration': 100}
});
});

it('should attach release if available', function() {
this.sinon.stub(Raven, 'isSetup').returns(true);
this.sinon.stub(Raven, '_makeRequest');
Expand Down Expand Up @@ -1759,6 +1792,19 @@ describe('Raven (public API)', function() {
});
});

describe('.setEnvironment', function() {
it('should set the globalOptions.environment attribute', function() {
Raven.setEnvironment('abc123');
assert.equal(Raven._globalOptions.environment, 'abc123');
});

it('should clear globalOptions.environment with no arguments', function() {
Raven._globalOptions.environment = 'abc123';
Raven.setEnvironment();
assert.isUndefined(Raven._globalOptions.environment);
});
});

describe('.setRelease', function() {
it('should set the globalOptions.release attribute', function() {
Raven.setRelease('abc123');
Expand Down
3 changes: 3 additions & 0 deletions typescript/raven.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ interface RavenOptions {
/** The name of the logger used by Sentry. Default: javascript */
logger?: string;

/** The environment of the application you are monitoring with Sentry */
environment?: string;

/** The release version of the application you are monitoring with Sentry */
release?: string;

Expand Down