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

Breadcrumbs: Add Breadcrumb class #503

Merged
merged 9 commits into from
Dec 6, 2018
Merged

Conversation

Cawllec
Copy link
Contributor

@Cawllec Cawllec commented Dec 4, 2018

Adds Bugsnag::Breadcrumbs::Breadcrumb class and tests in order to support Ruby Breadcrumbs feature.

@Cawllec Cawllec requested review from tobyhs, kellymason and a team December 4, 2018 15:49
self.type = type
self.meta_data = meta_data
@auto = auto
@timestamp = Time.now.utc.strftime('%Y-%m-%dT%H:%M:%S')
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we store as a time and convert to string later?

expect(breadcrumb.meta_data).to eq({:a => 1, :b => 2})
expect(breadcrumb.auto).to eq(:manual)

expect(breadcrumb.timestamp).to_not be_nil
Copy link
Contributor

Choose a reason for hiding this comment

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

There should be more constraint on this

attr_reader :auto
attr_reader :timestamp

def initialize(message, type, meta_data, auto)
Copy link
Contributor

Choose a reason for hiding this comment

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

yardoc comments?

Copy link
Contributor

Choose a reason for hiding this comment

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

If we're starting to adopt yardoc in bugsnag-ruby, there should be @param, @return, etc. comments (see https://www.rubydoc.info/gems/yard/file/docs/GettingStarted.md , but don't follow that first snippet which actually RDoc instead). Also, #initialize should probably have an # @api private as I don't think end users will need to create a Breadcrumb.

describe Bugsnag::Breadcrumbs::Breadcrumb do
describe "initialize" do
it "should assign arguments correctly" do
breadcrumb = Bugsnag::Breadcrumbs::Breadcrumb.new("my message", "test type", {:a => 1, :b => 2}, :manual)
Copy link
Contributor

Choose a reason for hiding this comment

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

is auto not a boolean?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it will be a boolean inside the class, but not the leave_breadcrumbs method. I'll update that.

self.type = type
self.meta_data = meta_data
@auto = auto
@timestamp = Time.now.utc.strftime('%Y-%m-%dT%H:%M:%S')
Copy link
Contributor

Choose a reason for hiding this comment

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

This string should probably end in Z to make it conform to a ISO 8601 time

Copy link

Choose a reason for hiding this comment

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


def to_h
{
:message => message,
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 the error reporting API takes a name instead of message (from https://bugsnagerrorreportingapi.docs.apiary.io/#reference/0/notify/send-error-reports )

require 'bugsnag/breadcrumbs/breadcrumb'

describe Bugsnag::Breadcrumbs::Breadcrumb do
describe "initialize" do
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer describe "#initialize" (with the #) so that the failure message is Bugsnag::Breadcrumbs::Breadcrumb#initialize should assign arguments correctly (similarly for the #to_h describe block)

@@ -0,0 +1,57 @@
module Bugsnag::Breadcrumbs
class Breadcrumb
attr_accessor :message
Copy link
Contributor

Choose a reason for hiding this comment

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

I know your original design doc had this as message, but it might be best to rename this attribute and the parameter of #initialize to name at this point to be consistent.

attr_reader :auto
attr_reader :timestamp

def initialize(message, type, meta_data, auto)
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're starting to adopt yardoc in bugsnag-ruby, there should be @param, @return, etc. comments (see https://www.rubydoc.info/gems/yard/file/docs/GettingStarted.md , but don't follow that first snippet which actually RDoc instead). Also, #initialize should probably have an # @api private as I don't think end users will need to create a Breadcrumb.

:name => @message,
:type => @type,
:metaData => @meta_data,
:timestamp => @timestamp.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
Copy link
Contributor

Choose a reason for hiding this comment

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

As mentioned in #503 (comment) you can use the iso8601 method, but you'll probably need to require 'time' in this file for that to work.

expect(breadcrumb.auto).to eq(true)
end

it "if false if auto argument is anything else" do
Copy link
Contributor

Choose a reason for hiding this comment

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

Replace "if" with "is"

it "is stored as a timestamp" do
breadcrumb = Bugsnag::Breadcrumbs::Breadcrumb.new(nil, nil, nil, nil)

expect(breadcrumb.timestamp).to be_within(0.5.second).of Time.now
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid the second method unless you're willing to bring in activesupport as an explicit development dependency

end
end

describe "#ignore" do
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be #ignore? (to match the actual method name called by all examples under this describe block)

# @return [Hash, nil] metadata hash containing strings, numbers, or booleans, or nil
attr_accessor :meta_data

# @return [Symbol] set to `:auto` if the breadcrumb was automatically generated
Copy link
Contributor

Choose a reason for hiding this comment

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

boolean

@auto = auto == :auto

# Store it as a timestamp for now
@timestamp = Time.now
Copy link
Contributor

Choose a reason for hiding this comment

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

Use UTC

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We convert to utc before outputting it in the hash

Copy link
Contributor

Choose a reason for hiding this comment

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

Do it here so you dont have different timezones in the library. Its a best practice to convert to UTC immediately.


describe "#type" do
it "is assigned in #initialize" do
breadcrumb = Bugsnag::Breadcrumbs::Breadcrumb.new(nil, "test type", nil, nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

its weird that you can set this to an invalid type to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As this may be overwritten by callbacks anyway, it seems better to check this in a single place (the validator) than in the breadcrumb.

Copy link
Contributor

Choose a reason for hiding this comment

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

in isolation i would have the setter validate. Its complicated by the metadata validation where that isnt possible however.

spec/breadcrumbs/breadcrumb_spec.rb Show resolved Hide resolved
:a => 1,
:b => 2
},
:timestamp => match(timestamp_regex)
Copy link
Contributor

Choose a reason for hiding this comment

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

Here you should expect it to match timestamp.iso8601 rather than just regexing imo

@Cawllec Cawllec merged commit 245978e into breadcrumbs/base Dec 6, 2018
@Cawllec Cawllec deleted the breadcrumbs/breadcrumb branch December 6, 2018 15:32
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.

4 participants