Skip to content

Commit 4a8591b

Browse files
dcramerbenvinegar
authored andcommitted
Add environment configuration (#661)
@getsentry/javascript Fixes GH-660
1 parent 17f4261 commit 4a8591b

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/raven.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,19 @@ Raven.prototype = {
431431
return JSON.parse(JSON.stringify(this._globalContext));
432432
},
433433

434+
435+
/*
436+
* Set environment of application
437+
*
438+
* @param {string} environment Typically something like 'production'.
439+
* @return {Raven}
440+
*/
441+
setEnvironment: function(environment) {
442+
this._globalOptions.environment = environment;
443+
444+
return this;
445+
},
446+
434447
/*
435448
* Set release version of application
436449
*
@@ -1185,6 +1198,9 @@ Raven.prototype = {
11851198
data.user = this._globalContext.user;
11861199
}
11871200

1201+
// Include the environment if it's defined in globalOptions
1202+
if (globalOptions.environment) data.environment = globalOptions.environment;
1203+
11881204
// Include the release if it's defined in globalOptions
11891205
if (globalOptions.release) data.release = globalOptions.release;
11901206

test/raven.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,39 @@ describe('globals', function() {
855855
});
856856
});
857857

858+
it('should attach environment if available', function() {
859+
this.sinon.stub(Raven, 'isSetup').returns(true);
860+
this.sinon.stub(Raven, '_makeRequest');
861+
this.sinon.stub(Raven, '_getHttpData').returns({
862+
url: 'http://localhost/?a=b',
863+
headers: {'User-Agent': 'lolbrowser'}
864+
});
865+
866+
Raven._globalOptions = {
867+
projectId: 2,
868+
logger: 'javascript',
869+
maxMessageLength: 100,
870+
environment: 'abc123'
871+
};
872+
873+
Raven._send({message: 'bar'});
874+
assert.deepEqual(Raven._makeRequest.lastCall.args[0].data, {
875+
project: '2',
876+
environment: 'abc123',
877+
logger: 'javascript',
878+
platform: 'javascript',
879+
request: {
880+
url: 'http://localhost/?a=b',
881+
headers: {
882+
'User-Agent': 'lolbrowser'
883+
}
884+
},
885+
event_id: 'abc123',
886+
message: 'bar',
887+
extra: {'session:duration': 100}
888+
});
889+
});
890+
858891
it('should attach release if available', function() {
859892
this.sinon.stub(Raven, 'isSetup').returns(true);
860893
this.sinon.stub(Raven, '_makeRequest');
@@ -1759,6 +1792,19 @@ describe('Raven (public API)', function() {
17591792
});
17601793
});
17611794

1795+
describe('.setEnvironment', function() {
1796+
it('should set the globalOptions.environment attribute', function() {
1797+
Raven.setEnvironment('abc123');
1798+
assert.equal(Raven._globalOptions.environment, 'abc123');
1799+
});
1800+
1801+
it('should clear globalOptions.environment with no arguments', function() {
1802+
Raven._globalOptions.environment = 'abc123';
1803+
Raven.setEnvironment();
1804+
assert.isUndefined(Raven._globalOptions.environment);
1805+
});
1806+
});
1807+
17621808
describe('.setRelease', function() {
17631809
it('should set the globalOptions.release attribute', function() {
17641810
Raven.setRelease('abc123');

typescript/raven.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ interface RavenOptions {
1313
/** The name of the logger used by Sentry. Default: javascript */
1414
logger?: string;
1515

16+
/** The environment of the application you are monitoring with Sentry */
17+
environment?: string;
18+
1619
/** The release version of the application you are monitoring with Sentry */
1720
release?: string;
1821

0 commit comments

Comments
 (0)