Class: Dispatch::Rails::EventBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/dispatch/rails/event_builder.rb

Overview

Turns a Ruby exception (+ optional Rack env / user) into the Sentry-shaped event hash the Dispatch ingest endpoint understands. In-app frames are flagged and given source context (the lines around the failing line), which is what makes the AI fix pipeline’s patches accurate.

Constant Summary collapse

CONTEXT_LINES =
5
MAX_CONTEXT_FRAMES =
12
MAX_FRAMES =
100
MAX_CAUSES =
5
MAX_PARAMS_BYTES =
8_000
SAFE_HEADERS =
%w[User-Agent Referer Accept Content-Type Host X-Request-Id].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception, handled:, env: nil, user: nil, tags: {}, level: "error", request_url: nil) ⇒ EventBuilder

Returns a new instance of EventBuilder.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dispatch/rails/event_builder.rb', line 24

def initialize(exception, handled:, env: nil, user: nil, tags: {}, level: "error", request_url: nil)
  @exception = exception
  @handled = handled
  @env = env
  @user = user
  @tags = tags || {}
  @level = level
  # Override for the event's request URL. A browser report (CSP, NEL, …) is
  # POSTed to /dispatch/reports, so the env URL is that endpoint, not the page
  # that violated the policy — the caller passes the report's document-uri here
  # so the dashboard attributes (and groups) the event to the real page.
  @request_url_override = request_url
  @source_cache = {}
  @config = Dispatch::Rails.configuration
end

Class Method Details

.call(exception, handled:, env: nil, user: nil, tags: {}, level: "error", request_url: nil) ⇒ Object



19
20
21
22
# File 'lib/dispatch/rails/event_builder.rb', line 19

def self.call(exception, handled:, env: nil, user: nil, tags: {}, level: "error", request_url: nil)
  new(exception, handled: handled, env: env, user: user, tags: tags, level: level,
                 request_url: request_url).call
end

Instance Method Details

#callObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dispatch/rails/event_builder.rb', line 40

def call
  {
    event_id: SecureRandom.uuid.delete("-"),
    timestamp: Time.now.to_f,
    platform: "ruby",
    level: @level,
    environment: @config.effective_environment,
    release: @config.release,
    server_name: hostname,
    transaction: transaction, # Sentry's name for the controller#action
    exception: { values: exception_values },
    request: request_hash,
    user: user_hash,
    tags: tags_hash,
    sdk: { name: "dispatch-rails", version: Dispatch::Rails::VERSION }
  }.compact
end