-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
context.cr
69 lines (59 loc) · 1.83 KB
/
context.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
module Raven
class Context
@@mutex = Mutex.new
@@current : self?
def self.current
@@mutex.synchronize do
@@current ||= new
end
end
def self.clear!
@@mutex.synchronize do
@@current = nil
end
end
class_getter os_context : AnyHash::JSON do
{
name: Raven.sys_command("uname -s"),
version: Raven.sys_command("uname -v"),
build: Raven.sys_command("uname -r"),
kernel_version: Raven.sys_command("uname -a") || Raven.sys_command("ver"), # windows
}.to_any_json
end
class_getter runtime_context : AnyHash::JSON do
version_pattern = /^(.+?) (\d+[^\n]+)\n+LLVM: (\d+[^\n]+)\nDefault target: (.+?)$/m
unless match = Crystal::DESCRIPTION.match(version_pattern)
raise Raven::Error.new("Couldn't parse runtime version")
end
_, name, version, _llvm_version, _target = match
{
name: name,
version: version,
}.to_any_json
end
any_json_property :contexts, :extra, :tags, :user
def initialize
self.contexts = {
os: self.class.os_context,
runtime: self.class.runtime_context,
}
initialize_from_env
end
protected def initialize_from_env
{% for key in %i(user extra tags) %}
{% env_key = "SENTRY_CONTEXT_#{key.upcase.id}" %}
if %context = ENV[{{ env_key }}]?.presence
begin
if %json = JSON.parse(%context).as_h?
self.{{ key.id }}.merge!(%json)
else
raise Raven::Error.new("`{{ env_key.id }}` must contain a JSON-encoded hash")
end
rescue %e : JSON::ParseException
raise Raven::Error.new("Invalid JSON string in `{{ env_key.id }}`", cause: %e)
end
end
{% end %}
end
end
end