Skip to content

Commit

Permalink
Merge pull request #46 from Sija/develop
Browse files Browse the repository at this point in the history
v1.2
  • Loading branch information
Sija authored Jan 24, 2019
2 parents e5a2de5 + 4b10e1e commit ff32385
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ errors in a certain environment, just don't set the DSN in that environment!

```bash
# Set your SENTRY_DSN environment variable.
export SENTRY_DSN=https://public:secret@example.com/project-id
export SENTRY_DSN=https://[email protected]/project-id
```

```crystal
# Or you can configure the client in the code (not recommended - keep your DSN secret!)
Raven.configure do |config|
config.dsn = "https://public:secret@example.com/project-id"
config.dsn = "https://[email protected]/project-id"
end
```

Expand Down
9 changes: 5 additions & 4 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
name: raven
version: 1.1.0
version: 1.2.0

authors:
- Sijawusz Pur Rahnama <[email protected]>

dependencies:
any_hash:
github: Sija/any_hash.cr
version: 0.2.2
version: ~> 0.2.2

development_dependencies:
timecop:
github: TobiasGSmollett/timecop.cr
github: crystal-community/timecop.cr
version: ~> 0.2.0
ameba:
github: veelenga/ameba
version: 0.8.0
version: ~> 0.9.0

targets:
crash_handler:
Expand Down
14 changes: 14 additions & 0 deletions spec/raven/breadcrumb_buffer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ describe Raven::BreadcrumbBuffer do
end
end

it "records breadcrumbs w/a splat" do
with_breadcrumb_buffer do |breadcrumbs|
breadcrumbs.record(message: "test")
breadcrumbs.members.first.message.should eq("test")
end
end

it "records breadcrumbs w/a NamedTuple" do
with_breadcrumb_buffer do |breadcrumbs|
breadcrumbs.record({message: "test"})
breadcrumbs.members.first.message.should eq("test")
end
end

it "allows peeking" do
with_breadcrumb_buffer do |breadcrumbs|
breadcrumbs.peek.should be_nil
Expand Down
2 changes: 1 addition & 1 deletion spec/raven/instance_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ end
private class InstanceTest < Raven::Instance
getter last_sent_event : Raven::Event?

def send_event(event)
def send_event(event, hint = nil)
super.tap do
@last_sent_event = event
end
Expand Down
2 changes: 1 addition & 1 deletion src/crash_handler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ module Raven
{output.to_s.chomp, error.to_s.chomp}
end

def run : Void
def run : Nil
configure!
@started_at = Time.monotonic

Expand Down
7 changes: 4 additions & 3 deletions src/raven/breadcrumb.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Raven
class Breadcrumb
include Mixin::InitializeWith

# The type of breadcrumb. The default type is `Type::DEFAULT` which indicates
# no specific handling. Other types are currently:
# - `Type::HTTP` for HTTP requests and
Expand Down Expand Up @@ -58,8 +60,9 @@ module Raven
# are unsupported by the type are rendered as a key/value table.
any_json_property :data

def initialize
def initialize(**options)
@timestamp = Time.now
initialize_with **options
end

def to_hash
Expand All @@ -74,5 +77,3 @@ module Raven
end
end
end

require "./breadcrumbs/*"
16 changes: 12 additions & 4 deletions src/raven/breadcrumb_buffer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,23 @@ module Raven
@buffer = Array(Breadcrumb?).new(size, nil)
end

def record(crumb : Breadcrumb) : Void
def record(crumb : Breadcrumb) : Nil
@buffer.shift
@buffer << crumb
end

def record(crumb : Breadcrumb? = nil) : Void
crumb ||= Breadcrumb.new
def record(**opts) : Nil
crumb = Breadcrumb.new(**opts)
yield crumb
self.record crumb
record crumb
end

def record(**opts) : Nil
record(**opts) { }
end

def record(opts : NamedTuple) : Nil
record(**opts)
end

def members : Array(Breadcrumb)
Expand Down
11 changes: 10 additions & 1 deletion src/raven/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ module Raven
transport.send_feedback(event_id, data)
end

def send_event(event : Event | Event::HashType)
def send_event(event : Event | Event::HashType, hint : Event::Hint? = nil)
if event.is_a?(Event)
configuration.before_send.try do |before_send|
event = before_send.call(event, hint)
unless event
logger.info "Discarded event because before_send returned nil"
return
end
end
end
event = event.is_a?(Event) ? event.to_hash : event
unless @state.should_try?
failed_send nil, event
Expand Down
6 changes: 3 additions & 3 deletions src/raven/client_state.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ module Raven
false
end

def failure(retry_after = nil) : Void
def failure(retry_after = nil) : Nil
@status = Status::ERROR
@retry_number += 1
@last_check = Time.now
@retry_after = retry_after
end

def success : Void
def success : Nil
reset
end

def reset : Void
def reset : Nil
@status = Status::ONLINE
@retry_number = 0
@last_check = nil
Expand Down
45 changes: 34 additions & 11 deletions src/raven/configuration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module Raven
property async : Proc(Event, Nil)?

# ditto
def async=(block : Proc(Event, _))
def async=(block : Event -> _)
@async = ->(event : Event) {
block.call(event)
nil
Expand Down Expand Up @@ -237,13 +237,37 @@ module Raven
property transport_failure_callback : Proc(Event::HashType, Nil)?

# ditto
def transport_failure_callback=(block : Proc(Event::HashType, _))
def transport_failure_callback=(block : Event::HashType -> _)
@transport_failure_callback = ->(event : Event::HashType) {
block.call(event)
nil
}
end

# Optional `Proc`, called before sending an event to the server:
#
# ```
# ->(event : Raven::Event, hint : Raven::Event::Hint?) {
# if hint.try(&.exception).try(&.message) =~ /database unavailable/i
# event.fingerprint << "database-unavailable"
# end
# event
# }
# ```
def before_send=(block : Event, Event::Hint? -> _)
@before_send = ->(event : Event, hint : Event::Hint?) {
block.call(event, hint).as(Event?)
}
end

# ditto
def before_send(&block : Event, Event::Hint? -> _)
self.before_send = block
end

# ditto
property before_send : Proc(Event, Event::Hint?, Event?)?

# Errors object - an `Array` containing error messages.
getter errors = [] of String

Expand All @@ -257,14 +281,14 @@ module Raven
@release = detect_release
@server_name = server_name_from_env

# try runtime ENV variable first
if dsn = ENV["SENTRY_DSN"]?
self.dsn = dsn
end
# then try compile-time ENV variable
# overwrites runtime if set
# try compile-time ENV variable
{% if dsn = env("SENTRY_DSN") %}
self.dsn = {{dsn}}
{% else %}
# try runtime ENV variable
if dsn = ENV["SENTRY_DSN"]?
self.dsn = dsn
end
{% end %}
end

Expand Down Expand Up @@ -314,19 +338,18 @@ module Raven
File.directory?("/etc/heroku")
end

private def heroku_dyno_metadata_message
private HEROKU_DYNO_METADATA_MESSAGE =
"You are running on Heroku but haven't enabled Dyno Metadata. " \
"For Sentry's release detection to work correctly, please run " \
"`heroku labs:enable runtime-dyno-metadata`"
end

private def detect_release_from_heroku
return unless running_on_heroku?
return if ENV["CI"]?
if commit = ENV["HEROKU_SLUG_COMMIT"]?
return commit
end
logger.warn(heroku_dyno_metadata_message)
logger.warn(HEROKU_DYNO_METADATA_MESSAGE)
nil
end

Expand Down
3 changes: 3 additions & 0 deletions src/raven/event.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ module Raven
FATAL
end

# Structure passed to `Configuration#before_send` callback.
record Hint, exception : Exception?, message : String?

# See Sentry server default limits at
# https://github.com/getsentry/sentry/blob/master/src/sentry/conf/server.py
MAX_MESSAGE_SIZE_IN_BYTES = 1024 * 8
Expand Down
16 changes: 11 additions & 5 deletions src/raven/instance.cr
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ module Raven
# event = Raven::Event.new(message: "An error")
# Raven.send_event(event)
# ```
def send_event(event)
client.send_event(event)
def send_event(event, hint = nil)
client.send_event(event, hint)
end

@last_event_id_mutex = Mutex.new
Expand All @@ -131,16 +131,22 @@ module Raven
end
Event.from(obj, configuration: configuration, context: context).tap do |event|
event.initialize_with **options
yield event
hint =
if obj.is_a?(String)
Event::Hint.new(exception: nil, message: obj)
else
Event::Hint.new(exception: obj, message: nil)
end
yield event, hint
if async = configuration.async
begin
async.call(event)
rescue ex
logger.error "Async event sending failed: #{ex.message}"
send_event(event)
send_event(event, hint)
end
else
send_event(event)
send_event(event, hint)
end
@last_event_id_mutex.synchronize do
@last_event_id = event.id
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion src/raven/integrations/kernel/spawn.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
def spawn(*, name : String? = nil, &block)
# ameba:disable Style/RedundantBegin
wrapped_block = ->{
begin
block.call
Expand Down
2 changes: 1 addition & 1 deletion src/raven/transports/dummy.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Raven
def send_event(auth_header, data, **options)
events << {
auth_header: auth_header,
data: data,
data: data.to_s,
options: options,
}.to_any_json
end
Expand Down
2 changes: 1 addition & 1 deletion src/raven/version.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Raven
VERSION = "1.1.0"
VERSION = "1.2.0"
end

0 comments on commit ff32385

Please sign in to comment.