Class: Sentry::Event

Inherits:
Object show all
Defined in:
lib/sentry/event.rb

Direct Known Subclasses

TransactionEvent

Constant Summary collapse

ATTRIBUTES =
%i(
  event_id level timestamp
  release environment server_name modules
  message user tags contexts extra
  fingerprint breadcrumbs backtrace transaction
  platform sdk type
)
MAX_MESSAGE_SIZE_IN_BYTES =
1024 * 8

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration:, integration_meta: nil, message: nil) ⇒ Event

Returns a new instance of Event.



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
# File 'lib/sentry/event.rb', line 25

def initialize(configuration:, integration_meta: nil, message: nil)
  # this needs to go first because some setters rely on configuration
  @configuration = configuration

  # Set some simple default values
  @event_id      = SecureRandom.uuid.delete("-")
  @timestamp     = Sentry.utc_now.iso8601
  @platform      = :ruby
  @sdk           = integration_meta || Sentry.sdk_meta

  @user          = {}
  @extra         = {}
  @contexts      = {}
  @tags          = {}

  @fingerprint = []

  @server_name = configuration.server_name
  @environment = configuration.environment
  @release = configuration.release
  @modules = configuration.gem_specs if configuration.send_modules

  @message = (message || "").byteslice(0..MAX_MESSAGE_SIZE_IN_BYTES)

  self.level = :error
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



23
24
25
# File 'lib/sentry/event.rb', line 23

def configuration
  @configuration
end

#exceptionObject (readonly)

Returns the value of attribute exception.



23
24
25
# File 'lib/sentry/event.rb', line 23

def exception
  @exception
end

#requestObject (readonly)

Returns the value of attribute request.



23
24
25
# File 'lib/sentry/event.rb', line 23

def request
  @request
end

#stacktraceObject (readonly)

Returns the value of attribute stacktrace.



23
24
25
# File 'lib/sentry/event.rb', line 23

def stacktrace
  @stacktrace
end

#threadsObject (readonly)

Returns the value of attribute threads.



23
24
25
# File 'lib/sentry/event.rb', line 23

def threads
  @threads
end

Class Method Details

.get_log_message(event_hash) ⇒ Object



53
54
55
56
57
58
# File 'lib/sentry/event.rb', line 53

def get_log_message(event_hash)
  message = event_hash[:message] || event_hash['message']
  message = get_message_from_exception(event_hash) if message.nil? || message.empty?
  message = '<no message value>' if message.nil? || message.empty?
  message
end

.get_message_from_exception(event_hash) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/sentry/event.rb', line 60

def get_message_from_exception(event_hash)
  (
    event_hash &&
    event_hash[:exception] &&
    event_hash[:exception][:values] &&
    event_hash[:exception][:values][0] &&
    "#{event_hash[:exception][:values][0][:type]}: #{event_hash[:exception][:values][0][:value]}"
  )
end

Instance Method Details

#add_exception_interface(exc) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/sentry/event.rb', line 123

def add_exception_interface(exc)
  if exc.respond_to?(:sentry_context)
    @extra.merge!(exc.sentry_context)
  end

  @exception = Sentry::ExceptionInterface.new.tap do |exc_int|
    exceptions = Sentry::Utils::ExceptionCauseChain.exception_to_array(exc).reverse
    backtraces = Set.new
    exc_int.values = exceptions.map do |e|
      SingleExceptionInterface.new.tap do |int|
        int.type = e.class.to_s
        int.value = e.message.byteslice(0..MAX_MESSAGE_SIZE_IN_BYTES)
        int.module = e.class.to_s.split('::')[0...-1].join('::')
        int.thread_id = Thread.current.object_id

        int.stacktrace =
          if e.backtrace && !backtraces.include?(e.backtrace.object_id)
            backtraces << e.backtrace.object_id
            initialize_stacktrace_interface(e.backtrace)
          end
      end
    end
  end
end

#add_request_interface(env) ⇒ Object



114
115
116
# File 'lib/sentry/event.rb', line 114

def add_request_interface(env)
  @request = Sentry::RequestInterface.from_rack(env)
end

#add_threads_interface(backtrace: nil, **options) ⇒ Object



118
119
120
121
# File 'lib/sentry/event.rb', line 118

def add_threads_interface(backtrace: nil, **options)
  @threads = ThreadsInterface.new(**options)
  @threads.stacktrace = initialize_stacktrace_interface(backtrace) if backtrace
end

#initialize_stacktrace_interface(backtrace) ⇒ Object



148
149
150
151
152
153
154
155
156
157
# File 'lib/sentry/event.rb', line 148

def initialize_stacktrace_interface(backtrace)
  StacktraceInterface.new(
    backtrace: backtrace,
    project_root: configuration.project_root.to_s,
    app_dirs_pattern: configuration.app_dirs_pattern,
    linecache: configuration.linecache,
    context_lines: configuration.context_lines,
    backtrace_cleanup_callback: configuration.backtrace_cleanup_callback
  )
end

#level=(new_level) ⇒ Object

needed to meet the Sentry spec



75
76
77
# File 'lib/sentry/event.rb', line 75

def level=(new_level) # needed to meet the Sentry spec
  @level = new_level.to_s == "warn" ? :warning : new_level
end

#rack_env=(env) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sentry/event.rb', line 79

def rack_env=(env)
  unless request || env.empty?
    env = env.dup

    add_request_interface(env)

    if configuration.send_default_pii
      user[:ip_address] = calculate_real_ip_from_rack(env)
    end

    if request_id = Utils::RequestId.read_from(env)
      tags[:request_id] = request_id
    end
  end
end

#timestamp=(time) ⇒ Object



71
72
73
# File 'lib/sentry/event.rb', line 71

def timestamp=(time)
  @timestamp = time.is_a?(Time) ? time.to_f : time
end

#to_hashObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/sentry/event.rb', line 99

def to_hash
  data = serialize_attributes
  data[:breadcrumbs] = breadcrumbs.to_hash if breadcrumbs
  data[:stacktrace] = stacktrace.to_hash if stacktrace
  data[:request] = request.to_hash if request
  data[:exception] = exception.to_hash if exception
  data[:threads] = threads.to_hash if threads

  data
end

#to_json_compatibleObject



110
111
112
# File 'lib/sentry/event.rb', line 110

def to_json_compatible
  JSON.parse(JSON.generate(to_hash))
end

#typeObject



95
96
97
# File 'lib/sentry/event.rb', line 95

def type
  "event"
end