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

#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
59
60
61
62
63
64
65
66
67
# File 'lib/sentry/event.rb', line 53

def get_log_message(event_hash)
  message = event_hash[:message] || event_hash['message']

  return message unless message.nil? || message.empty?

  message = get_message_from_exception(event_hash)

  return message unless message.nil? || message.empty?

  message = event_hash[:transaction] || event_hash["transaction"]

  return message unless message.nil? || message.empty?

  '<no message value>'
end

.get_message_from_exception(event_hash) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/sentry/event.rb', line 69

def get_message_from_exception(event_hash)
  if exception = event_hash.dig(:exception, :values, 0)
    "#{exception[:type]}: #{exception[:value]}"
  elsif exception = event_hash.dig("exception", "values", 0)
    "#{exception["type"]}: #{exception["value"]}"
  end
end

Instance Method Details

#add_exception_interface(exception) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/sentry/event.rb', line 132

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

  @exception = Sentry::ExceptionInterface.build(exception: exception, stacktrace_builder: configuration.stacktrace_builder)
end

#add_request_interface(env) ⇒ Object



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

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

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



124
125
126
127
128
129
130
# File 'lib/sentry/event.rb', line 124

def add_threads_interface(backtrace: nil, **options)
  @threads = ThreadsInterface.build(
    backtrace: backtrace,
    stacktrace_builder: configuration.stacktrace_builder,
    **options
  )
end

#level=(new_level) ⇒ Object

needed to meet the Sentry spec



82
83
84
# File 'lib/sentry/event.rb', line 82

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

#rack_env=(env) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sentry/event.rb', line 86

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



78
79
80
# File 'lib/sentry/event.rb', line 78

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

#to_hashObject



106
107
108
109
110
111
112
113
114
# File 'lib/sentry/event.rb', line 106

def to_hash
  data = serialize_attributes
  data[:breadcrumbs] = breadcrumbs.to_hash if breadcrumbs
  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



116
117
118
# File 'lib/sentry/event.rb', line 116

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

#typeObject



102
103
104
# File 'lib/sentry/event.rb', line 102

def type
  "event"
end