-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
client.cr
144 lines (125 loc) · 3.89 KB
/
client.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require "base64"
require "json"
require "compress/gzip"
module Raven
# Encodes events and sends them to the Sentry server.
class Client
PROTOCOL_VERSION = 7
USER_AGENT = "raven.cr/#{Raven::VERSION}"
property configuration : Configuration
@state : State
@processors : Array(Processor)
getter transport : Transport do
case configuration.scheme
when "http", "https"
Transport::HTTP.new(configuration)
when "dummy"
Transport::Dummy.new(configuration)
else
raise "Unknown transport scheme '#{configuration.scheme}'"
end
end
def initialize(@configuration)
@state = State.new
@processors = [] of Processor
@configuration.processors.each do |klass|
@processors << klass.new(self)
end
end
def send_feedback(event_id : String, data : Hash)
unless configuration.valid?
Log.debug {
"Client#send_feedback with event id '#{event_id}' failed: #{configuration.error_messages}"
}
return false
end
transport.send_feedback(event_id, data)
end
def send_event(event : Event | Event::HashType, hint : Event::Hint? = nil)
unless configuration.valid?
Log.debug {
"Client#send_event with event '#{event}' failed: #{configuration.error_messages}"
}
return false
end
if event.is_a?(Event)
configuration.before_send.try do |before_send|
event = before_send.call(event, hint)
unless event
Log.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
return
end
Log.info { "Sending event #{event[:event_id]} to Sentry" }
content_type, encoded_data = encode(event)
begin
options = {content_type: content_type}
transport.send_event(generate_auth_header, encoded_data, **options).tap do
successful_send
end
rescue ex
failed_send ex, event
end
end
private def encode(data)
data = @processors.reduce(data) do |v, processor|
processor.process(v)
end
io = IO::Memory.new
data.to_json(io)
io.rewind
case configuration.encoding
in .gzip?
io_gzipped = IO::Memory.new
Compress::Gzip::Writer.open(io_gzipped) do |gzip|
IO.copy(io, gzip)
end
io_gzipped.rewind
{"application/octet-stream", io_gzipped}
in .json?
{"application/json", io}
end
end
private def generate_auth_header
fields = {
:sentry_version => PROTOCOL_VERSION,
:sentry_client => USER_AGENT,
:sentry_key => configuration.public_key,
}
if secret_key = configuration.secret_key
fields[:sentry_secret] = secret_key
end
"Sentry " + fields.join(", ") { |key, value| "#{key}=#{value}" }
end
private def get_message_from_exception(event)
values = event.to_any_json[:exception, :values]?.try &.as?(Array)
if ex = values.try(&.first?).try &.as?(Hash)
type, value = ex[:type]?, ex[:value]?
"#{type}: #{value}" if type && value
end
end
private def get_log_message(event)
event[:message]? || get_message_from_exception(event) || "<no message value>"
end
private def successful_send
@state.success
end
private def failed_send(ex, event)
if ex
@state.failure
Log.warn(exception: ex) { "Unable to record event with remote Sentry server" }
else
Log.warn { "Not sending event due to previous failure(s)" }
end
message = get_log_message(event)
Log.warn { "Failed to submit event: #{message}" }
configuration.transport_failure_callback.try &.call(event)
end
end
end