Skip to content

Commit

Permalink
Merge pull request #52 from Sija/develop
Browse files Browse the repository at this point in the history
v1.5
  • Loading branch information
Sija authored Aug 7, 2019
2 parents 9ac8553 + c0939ca commit 6a4afdd
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 100 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
language: crystal

crystal:
- latest
- nightly

matrix:
allow_failures:
- crystal: nightly

install:
- shards install

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ end

And, while not necessary if using `SENTRY_DSN`, you can also provide an
`environments` setting. Raven will only capture events when
`SENTRY_CURRENT_ENV` or `KEMAL_ENV` matches an environment on the list.
`SENTRY_ENVIRONMENT` matches an environment on the list.

```crystal
Raven.configure do |config|
Expand Down
10 changes: 5 additions & 5 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
name: raven
version: 1.4.0
version: 1.5.0

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

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

development_dependencies:
timecop:
github: crystal-community/timecop.cr
version: ~> 0.2.0
version: ~> 0.3.0
ameba:
github: veelenga/ameba
github: crystal-ameba/ameba
version: ~> 0.10.0

targets:
crash_handler:
main: src/crash_handler.cr

crystal: 0.29.0
crystal: 0.30.0

license: MIT
24 changes: 15 additions & 9 deletions spec/raven/configuration_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ end

# Make sure we reset the env in case something leaks in
def with_clean_env
sentry_vars = ->{ ENV.to_h.select { |key, _| key =~ /^(SENTRY_)|KEMAL_ENV/ } }
sentry_vars = ->{ ENV.to_h.select { |key, _| key =~ /^SENTRY_/ } }
previous_vars = sentry_vars.call
begin
previous_vars.each do |key, _|
Expand Down Expand Up @@ -108,23 +108,29 @@ describe Raven::Configuration do
end
end

it "uses `SENTRY_CURRENT_ENV` env variable" do
it "uses `SENTRY_ENVIRONMENT` env variable" do
with_clean_env do
ENV["SENTRY_CURRENT_ENV"] = "set-with-sentry-current-env"
ENV["KEMAL_ENV"] = "set-with-kemal-env"
ENV["SENTRY_ENVIRONMENT"] = "set-with-sentry-environment"

configuration = Raven::Configuration.new
configuration.current_environment.should eq("set-with-sentry-current-env")
configuration.current_environment.should eq("set-with-sentry-environment")
end
end
end

context "being initialized without a release" do
pending "defaults to nil" do
with_configuration do |configuration|
configuration.release.should be_nil
end
end

it "uses `KEMAL_ENV` env variable" do
it "uses `SENTRY_RELEASE` env variable" do
with_clean_env do
ENV["SENTRY_CURRENT_ENV"] = nil
ENV["KEMAL_ENV"] = "set-with-kemal-env"
ENV["SENTRY_RELEASE"] = "v1"

configuration = Raven::Configuration.new
configuration.current_environment.should eq("set-with-kemal-env")
configuration.release.should eq("v1")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/raven/event_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe Raven::Event do

it "prioritizes event context" do
with_event_hash({{key.id}}: event_context, clear: false) do |hash|
hash[:{{key.id}}].should eq({
hash[{{key}}].should eq({
"context_event_key" => "event_value",
"context_key" => "context_value",
"event_key" => "event_value",
Expand Down
17 changes: 5 additions & 12 deletions spec/raven/processors/sanitize_data_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ describe Raven::Processor::SanitizeData do
}

result = processor.process(data)

result["ccnumba"].should eq("4242424242424242")
result["ccnumba_int"].should eq(4242424242424242)
end
Expand Down Expand Up @@ -238,16 +239,11 @@ describe Raven::Processor::SanitizeData do
}

result = processor.process(data)
result = result.to_any_json

query_string = result["sentry.interfaces.Http", "data", "query_string"].as(String).split('&')
query_string.should contain("foo=bar")
query_string.should contain("foo=fubar")
query_string.should contain("foo=barfoo")
result.should eq(data)
end

it "handles url encoded keys and values" do
encoded_query_string = "Bio%204%24=cA%24%7C-%7C+M%28%29n3%5E"
encoded_query_string = "Bio+4%24=cA%24%7C-%7C+M%28%29n3%5E"
data = {
"sentry.interfaces.Http" => {
"data" => {
Expand All @@ -257,9 +253,7 @@ describe Raven::Processor::SanitizeData do
}

result = processor.process(data)
result = result.to_any_json

result["sentry.interfaces.Http", "data", "query_string"].should eq(encoded_query_string)
result.should eq(data)
end
end

Expand All @@ -271,7 +265,6 @@ describe Raven::Processor::SanitizeData do
}

result = processor.process(data)

result.should eq({:millis_since_epoch => "1507671610403"})
result.should eq(data)
end
end
10 changes: 5 additions & 5 deletions src/crash_handler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ module Raven

private def capture_with_options(**options)
yield
rescue e : Raven::Error
raise e # Don't capture Raven errors
rescue e : Exception
capture_with_options(e, **options) { }
raise e
rescue ex : Raven::Error
raise ex # Don't capture Raven errors
rescue ex : Exception
capture_with_options(ex, **options) { }
raise ex
end

private def capture_crystal_exception(klass, msg, backtrace, **options)
Expand Down
35 changes: 20 additions & 15 deletions src/raven/backtrace_line.cr
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,27 @@ module Raven

# Parses a single line of a given backtrace, where *unparsed_line* is
# the raw line from `caller` or some backtrace.
# Returns the parsed backtrace line.
def self.parse(unparsed_line : String) : Line
if CALLSTACK_PATTERNS.values.any? &.match(unparsed_line)
file = $~["file"]?
file = nil if file.try(&.blank?)
method = $~["method"]?
method = nil if method.try(&.blank?)
number = $~["line"]?.try(&.to_i)
column = $~["col"]?.try(&.to_i)
else
raise ArgumentError.new("Error parsing line: #{unparsed_line.inspect}")
end
#
# Returns the parsed backtrace line on success or `nil` otherwise.
def self.parse?(unparsed_line : String) : Line?
return unless CALLSTACK_PATTERNS.values.any? &.match(unparsed_line)

file = $~["file"]?
file = nil if file.try(&.blank?)
method = $~["method"]?
method = nil if method.try(&.blank?)
number = $~["line"]?.try(&.to_i?)
column = $~["col"]?.try(&.to_i?)

new(file, number, column, method)
end

# ditto
def self.parse(unparsed_line : String) : Line
parse?(unparsed_line) || \
raise ArgumentError.new("Error parsing line: #{unparsed_line.inspect}")
end

def initialize(@file, @number, @column, @method)
end

Expand Down Expand Up @@ -113,7 +119,7 @@ module Raven
def shard_name : String?
relative_path
.try(&.match(configuration.modules_path_pattern))
.try(&.[]("name"))
.try(&.["name"])
end

def in_app? : Bool
Expand All @@ -124,9 +130,8 @@ module Raven
context_lines = configuration.context_lines

return unless context_lines && context_lines > 0
return unless (filename = @file)
return unless (lineno = @number) && lineno > 0
return unless File.readable?(filename)
return unless (filename = @file) && File.readable?(filename)

lines = File.read_lines(filename)
lineidx = lineno - 1
Expand Down
10 changes: 5 additions & 5 deletions src/raven/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ module Raven
transport.send_event(generate_auth_header, encoded_data, **options).tap do
successful_send
end
rescue e
failed_send e, event
rescue ex
failed_send ex, event
end
end

Expand Down Expand Up @@ -104,11 +104,11 @@ module Raven
@state.success
end

private def failed_send(e, event)
private def failed_send(ex, event)
@state.failure
if e
if ex
logger.error "Unable to record event with remote Sentry server \
(#{e.class} - #{e.message}): #{e.backtrace[0..10].join('\n')}"
(#{ex.class} - #{ex.message}): #{ex.backtrace[0..10].join('\n')}"
else
logger.error "Not sending event due to previous failure(s)"
end
Expand Down
17 changes: 12 additions & 5 deletions src/raven/configuration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module Raven
# Number of lines of code context to capture, or `nil` for none.
property context_lines : Int32? = 5

# Defaults to `SENTRY_CURRENT_ENV` or `KEMAL_ENV` `ENV` variables if set,
# Defaults to `SENTRY_ENVIRONMENT` variable if set,
# `"default"` otherwise.
property current_environment : String?

Expand Down Expand Up @@ -334,7 +334,10 @@ module Raven
end

def detect_release : String?
detect_release_from_git || detect_release_from_capistrano || detect_release_from_heroku
detect_release_from_env ||
detect_release_from_git ||
detect_release_from_capistrano ||
detect_release_from_heroku
end

private def running_on_heroku?
Expand Down Expand Up @@ -369,6 +372,10 @@ module Raven
Raven.sys_command_compiled("git rev-parse HEAD")
end

private def detect_release_from_env
ENV["SENTRY_RELEASE"]?
end

private def heroku_dyno_name
return unless running_on_heroku?
ENV["DYNO"]?
Expand All @@ -385,7 +392,7 @@ module Raven
end

private def current_environment_from_env
ENV["SENTRY_CURRENT_ENV"]? || ENV["KEMAL_ENV"]? || "default"
ENV["SENTRY_ENVIRONMENT"]? || "default"
end

def capture_allowed?
Expand Down Expand Up @@ -444,9 +451,9 @@ module Raven
valid = true
if dsn
{% for key in REQUIRED_OPTIONS %}
unless {{ "self.#{key.id}".id }}
unless self.{{ key.id }}
valid = false
@errors << "No :{{ key.id }} specified"
@errors << "No {{ key }} specified"
end
{% end %}
else
Expand Down
8 changes: 3 additions & 5 deletions src/raven/event.cr
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module Raven
def self.from(exc : Exception, **options)
{% for key in %i(user tags extra) %}
exc_context = exc.@__raven_{{ key.id }}
if options_context = options[:{{ key.id }}]?
if options_context = options[{{ key }}]?
options = options.merge({
{{ key.id }}: exc_context.try(&.merge(options_context)) || options_context
})
Expand Down Expand Up @@ -130,7 +130,7 @@ module Raven

protected def self.add_exception_interface(event, exc)
exceptions = [exc] of Exception
context = Set(UInt64).new({exc.object_id})
context = Set(UInt64){exc.object_id}
backtraces = Set(UInt64).new

while exc = exc.cause
Expand Down Expand Up @@ -264,8 +264,6 @@ module Raven
data.to_h
end

def to_json(json : JSON::Builder)
to_hash.to_json(json)
end
delegate :to_json, to: to_hash
end
end
20 changes: 10 additions & 10 deletions src/raven/instance.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ module Raven
# # ...
#
#
# rescue e
# @other_raven.capture(e)
# rescue ex
# @other_raven.capture(ex)
# end
# end
# ```
Expand Down Expand Up @@ -162,8 +162,8 @@ module Raven
# ```
# begin
# # ...
# rescue e
# Raven.capture e
# rescue ex
# Raven.capture ex
# end
#
# Raven.capture "boo!"
Expand Down Expand Up @@ -210,11 +210,11 @@ module Raven
# ```
def capture(**options, &block)
yield
rescue e : Raven::Error
raise e # Don't capture Raven errors
rescue e : Exception
capture(e, **options)
raise e
rescue ex : Raven::Error
raise ex # Don't capture Raven errors
rescue ex : Exception
capture(ex, **options)
raise ex
end

# Provides extra context to the exception prior to it being handled by
Expand All @@ -238,7 +238,7 @@ module Raven
# ```
def annotate_exception(exc, **options)
{% for key in %i(user tags extra) %}
if v = options[:{{ key.id }}]?
if v = options[{{ key }}]?
exc.__raven_{{ key.id }}.merge!(v)
end
{% end %}
Expand Down
Loading

0 comments on commit 6a4afdd

Please sign in to comment.