Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

Commit

Permalink
Refine session tracking
Browse files Browse the repository at this point in the history
- migrate option name `sessionTrackingEnabled`->`autoCaptureSessions`
 for consistency with other notifiers
- session reporting now respects the `notifyReleaseStages` and will
 only send if error reports will
  • Loading branch information
bengourley committed Jan 9, 2018
1 parent 29f7b0d commit a502064
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/bugsnag.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Bugsnag.configure = function(options) {
});
}

if (Configuration.sessionTrackingEnabled) {
if (Configuration.sessionTrackingEnabled || Configuration.autoCaptureSessions) {
Bugsnag._sessionDelegate = createSessionDelegate(Configuration);
}
};
Expand Down Expand Up @@ -314,7 +314,7 @@ Bugsnag.onBeforeNotify = function (callback) {
};

Bugsnag.startSession = function () {
if (!Configuration.sessionTrackingEnabled) return null;
if (!Configuration.autoCaptureSessions) return null;
return Bugsnag._sessionDelegate.startSession();
};

Expand Down
4 changes: 2 additions & 2 deletions lib/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var Configuration = {
metaData: {},
logger: new Logger(),
sendCode: true,
sessionTrackingEnabled: false,
autoCaptureSessions: false,
sessionEndpoint: "https://sessions.bugsnag.com",

beforeNotifyCallbacks: [],
Expand Down Expand Up @@ -71,7 +71,7 @@ var Configuration = {
Configuration.appVersion = Utils.getPackageVersion(Utils.fullPath(options.packageJSON));
}
Configuration.sendCode = options.sendCode || Configuration.sendCode;
Configuration.sessionTrackingEnabled = options.sessionTrackingEnabled || Configuration.sessionTrackingEnabled;
Configuration.autoCaptureSessions = options.autoCaptureSessions || options.sessionTrackingEnabled || Configuration.autoCaptureSessions;
Configuration.sessionEndpoint = options.sessionEndpoint || Configuration.sessionEndpoint;
}
}
Expand Down
6 changes: 6 additions & 0 deletions lib/sessions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ const Utils = require('../utils')
const Backoff = require('backo')

module.exports = (Configuration) => {
const shouldSend =
Configuration.notifyReleaseStages === null ||
Configuration.notifyReleaseStages.indexOf(Configuration.releaseStage) !== -1
const sessions = new SessionTracker()
sessions.on('summary', sendSessionSummary)
sessions.start()

function sendSessionSummary (sessions) {
if (!sessions.length) return
if (!shouldSend) {
return Configuration.logger.warn("Current release stage prevented session report from being sent.")
}

const payload = {
notifier: notifier,
Expand Down
39 changes: 35 additions & 4 deletions test/sessions/delegate.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict'

const proxyquire = require('proxyquire').noCallThru().noPreserveCache()

const Emitter = require('events')

describe('session delegate', () => {
it('should', done => {
it('should send the session report', done => {
class TrackerMock extends Emitter {
start () {
this.emit('summary', [
Expand All @@ -25,8 +24,40 @@ describe('session delegate', () => {
}
})
createSessionDelegate({
logger: { info: () => {} },
sessionEndpoint: 'blah'
logger: { info: () => {}, warn: () => {} },
sessionEndpoint: 'blah',
notifyReleaseStages: null
}).startSession({})
})

it('should not send the session report when releaseStage is not in notifyReleaseStages', done => {
class TrackerMock extends Emitter {
start () {
this.emit('summary', [
{ startedAt: '2017-12-12T13:54:00.000Z', sessionsStarted: 123 }
])
}
stop () {}
track () {}
}
const createSessionDelegate = proxyquire('../../lib/sessions', {
'./tracker': TrackerMock,
'request': (opts) => {
true.should.equal(false)
done()
}
})
createSessionDelegate({
logger: {
info: () => {},
warn: (msg) => {
msg.should.equal('Current release stage prevented session report from being sent.')
setTimeout(done, 150)
}
},
sessionEndpoint: 'blah',
releaseStage: 'qa',
notifyReleaseStages: [ 'production' ]
}).startSession({})
})
})

0 comments on commit a502064

Please sign in to comment.