Skip to content
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

Capture sessions automatically by default #523

Merged
merged 19 commits into from
Jan 16, 2019
Merged

Conversation

Cawllec
Copy link
Contributor

@Cawllec Cawllec commented Jan 11, 2019

Goal

Enable automatic session capture by default, and more strictly enforce endpoint setting.

@@ -305,6 +305,7 @@ Metrics/ModuleLength:
Max: 125
Exclude:
- 'lib/bugsnag/helpers.rb'
- 'lib/bugsnag.rb'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note - this exclusion was originally approved in the breadcrumbs/base branch.

@@ -117,6 +117,7 @@ services:
- BUGSNAG_SESSION_ENDPOINT
- BUGSNAG_TIMEOUT
- CALLBACK_INITIATOR
- USE_DEFAULT_AUTO_CAPTURE_SESSIONS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm having trouble finding where this is set (from searching for USE_DEFAULT_AUTO_CAPTURE_SESSIONS in this pull request diff)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my bad, I'd removed some tests temporarily and didn't add them back! Pushing up the rest of the tests now.

render json: {}
end

def hundred
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would name this many or multiple or something like that so the name still makes sense if you change the number of sessions started.


##
# @return [Boolean] whether any sessions types will be delivered
attr_reader :send_sessions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a boolean setting/option/attribute with the same name as a public method in SessionTracker (send_sessions) might be slightly confusing. It might be preferable to rename this option to something like sessions_enabled or session_tracking_enabled instead.

spec/bugsnag_spec.rb Show resolved Hide resolved
expect(Bugsnag.configuration.send_sessions).to eq(false)
expect(Bugsnag.session_tracker.session_counts.size).to eq(0)
Bugsnag.start_session
# expect(Bugsnag.session_tracker.session_counts.size).to eq(0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intend to uncomment this out?

# Enable reset of session_counts between each example
module Bugsnag
class SessionTracker
attr_accessor :session_counts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can avoid opening/monkey-patching this class by having a before block call Bugsnag.session_tracker.send_sessions to flush session_counts, although you will probably need to set the delivery method to synchronous and clear queue right after the send_sessions call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, as long as the monkey-patching won't interfere with the tests I don't have a problem with it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A risk of monkey patching like this is that someone might introduce app code that uses the extra behavior (Bugsnag::SessionTracker#session_counts= in this case) and rely on the unit tests to make sure things work, which will be not as accurate as the tests introduce an extra method while the app code doesn't have this extra method.

Alternatively, you can do something similar to the global before(:each) block in spec_helper.rb and have Bugsnag.instance_variable_set(:@session_tracker, Bugsnag::SessionTracker.new) in a before block in this file (which has the benefit that anything in Bugsnag.session_tracker is reset if more instance variables are added to Bugsnag::SessionTracker later).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, and I quite like that last suggestion, more so than relying on send_sessions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you remove this monkey patch and the require 'concurrent' now that you're using Bugsnag.instance_variable_set(:@session_tracker, Bugsnag::SessionTracker.new)?

Also, I would put it in a before(:each) instead, then have an after(:all) so you have something fresh before the first test in this file also.

Then I should receive a request
And the request is a valid for the session tracking API
And the request used the "Ruby Bugsnag Notifier" notifier
And the "Bugsnag-API-Key" header equals "a35a2a72bd230ac0aa0f52715bbdc6aa"
And the sessionCount "startedAt" is a timestamp
And the sessionCount "sessionsStarted" equals 100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a little prone to non-deterministic failures because the 100 sessions could span across 2 different minutes?

Copy link
Contributor

@tobyhs tobyhs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a couple minor comments, otherwise lgtm, but I disagree with the monkey patch in session_tracker_spec.rb

Then(/^the total sessionStarted count equals (\d+)(?: for request (\d+))?$/) do |value, request_index|
session_counts = read_key_path(find_request(request_index)[:body], "sessionCounts")
total_count = session_counts.inject(0) { |count, session| count += session["sessionsStarted"] }
assert_equal(total_count, value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flip these arguments ((value, total_count)); the first parameter is expected and the second is actual.

# Enable reset of session_counts between each example
module Bugsnag
class SessionTracker
attr_accessor :session_counts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A risk of monkey patching like this is that someone might introduce app code that uses the extra behavior (Bugsnag::SessionTracker#session_counts= in this case) and rely on the unit tests to make sure things work, which will be not as accurate as the tests introduce an extra method while the app code doesn't have this extra method.

Alternatively, you can do something similar to the global before(:each) block in spec_helper.rb and have Bugsnag.instance_variable_set(:@session_tracker, Bugsnag::SessionTracker.new) in a before block in this file (which has the benefit that anything in Bugsnag.session_tracker is reset if more instance variables are added to Bugsnag::SessionTracker later).


it "should have sensible defaults for session tracking" do
expect(subject.session_endpoint).to eq("https://sessions.bugsnag.com")
describe "disable_sessions" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#disable_sessions (missing #)

@Cawllec Cawllec merged commit d8aaa21 into master Jan 16, 2019
@imjoehaines imjoehaines deleted the auto-session-tracking branch June 16, 2021 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants