diff --git a/src/raven.js b/src/raven.js index 5d0807b6eff3..fc2390938fd2 100644 --- a/src/raven.js +++ b/src/raven.js @@ -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 * @@ -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; diff --git a/test/raven.test.js b/test/raven.test.js index a8c7006beb14..0e9dddafe660 100644 --- a/test/raven.test.js +++ b/test/raven.test.js @@ -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'); @@ -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'); diff --git a/typescript/raven.d.ts b/typescript/raven.d.ts index 99f3fbc02772..6351afb4a2f0 100644 --- a/typescript/raven.d.ts +++ b/typescript/raven.d.ts @@ -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;