Module: Errsight

Defined in:
lib/errsight.rb,
lib/errsight/hub.rb,
lib/errsight/scope.rb,
lib/errsight/client.rb,
lib/errsight/logger.rb,
lib/errsight/railtie.rb,
lib/errsight/sidekiq.rb,
lib/errsight/version.rb,
lib/errsight/backtrace.rb,
lib/errsight/middleware.rb,
lib/errsight/configuration.rb,
lib/errsight/source_context.rb,
lib/errsight/capture_middleware.rb,
lib/errsight/integrations/active_job.rb,
lib/errsight/integrations/active_record.rb,
lib/errsight/integrations/rails_error_reporter.rb

Defined Under Namespace

Modules: Backtrace, Integrations, Sidekiq, SourceContext Classes: CaptureMiddleware, Client, Configuration, ConfigurationError, Error, Hub, Logger, Middleware, Railtie, Scope

Constant Summary collapse

VERSION =
"0.2.1"

Class Method Summary collapse

Class Method Details

.add_breadcrumb(**kwargs) ⇒ Object



60
# File 'lib/errsight.rb', line 60

def add_breadcrumb(**kwargs);  current_scope.add_breadcrumb(**kwargs); end

.capture_exception(exception, metadata: {}, fingerprint: nil, user: nil, tags: nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/errsight.rb', line 91

def capture_exception(exception, metadata: {}, fingerprint: nil, user: nil, tags: nil)
  return unless exception.is_a?(Exception)

   = .merge(exception_class: exception.class.to_s)
  causes = walk_exception_causes(exception)
  [:exception_causes] = causes if causes.any?

  frames = build_frames(exception)
  [:exception_frames] = frames if frames.any?

  log(
    level: :error,
    message: "#{exception.class}: #{exception.message}",
    backtrace: exception.backtrace&.join("\n"),
    metadata: ,
    fingerprint: fingerprint,
    user: user,
    tags: tags
  )
end

.clear_breadcrumbsObject



61
# File 'lib/errsight.rb', line 61

def clear_breadcrumbs;         current_scope.clear_breadcrumbs; end

.clear_tagsObject



59
# File 'lib/errsight.rb', line 59

def clear_tags;                current_scope.clear_tags; end

.clear_userObject



56
# File 'lib/errsight.rb', line 56

def clear_user;                current_scope.clear_user; end

.clientObject



29
30
31
# File 'lib/errsight.rb', line 29

def client
  @client ||= Client.new(configuration)
end

.configurationObject



19
20
21
# File 'lib/errsight.rb', line 19

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



23
24
25
26
27
# File 'lib/errsight.rb', line 23

def configure
  yield configuration
  configuration.validate!
  configuration
end

.current_scopeObject



37
38
39
# File 'lib/errsight.rb', line 37

def current_scope
  hub.current_scope
end

.hubObject



33
34
35
# File 'lib/errsight.rb', line 33

def hub
  Hub.current
end

.log(level:, message:, backtrace: nil, environment: nil, metadata: {}, occurred_at: nil, fingerprint: nil, user: nil, tags: nil, release: nil) ⇒ Object



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
# File 'lib/errsight.rb', line 63

def log(level:, message:, backtrace: nil, environment: nil, metadata: {},
        occurred_at: nil, fingerprint: nil, user: nil, tags: nil, release: nil)
  return unless configuration.enabled?
  return if level_below_threshold?(level)

  scope = current_scope
  event = {
    ingestion_id: SecureRandom.uuid,
    level: level.to_s,
    message: message.to_s,
    backtrace: backtrace,
    environment: environment || configuration.environment,
    metadata: ,
    occurred_at: (occurred_at || Time.now).iso8601(3),
    release: release || configuration.release,
    user: user || scope.user,
    tags: merge_tags(scope.tags, tags),
    breadcrumbs: scope.breadcrumbs
  }
  event[:fingerprint] = fingerprint if fingerprint
  event.compact!

  event = run_before_send(event)
  return if event.nil?

  client.enqueue(event)
end

.set_tag(key, value) ⇒ Object



57
# File 'lib/errsight.rb', line 57

def set_tag(key, value);       current_scope.set_tag(key, value); end

.set_tags(tags) ⇒ Object



58
# File 'lib/errsight.rb', line 58

def set_tags(tags);            current_scope.set_tags(tags); end

.set_user(user) ⇒ Object

Public scope mutators delegate to the current (top-of-stack) scope. Pre-scope-stack callers wrote directly to Thread.current and relied on the request middleware to clear; those reads bled across requests on long-lived Puma/Sidekiq threads. The stack guarantees cleanup at the block boundary regardless of caller hygiene.



55
# File 'lib/errsight.rb', line 55

def set_user(user);            current_scope.set_user(user); end

.with_scope(scope = nil, &block) ⇒ Object

Push a fresh scope for the duration of the block, then pop on exit. Used by request and job middleware so user/tags/breadcrumbs set during one unit of work don’t leak to the next one handled by the same thread. Pass an explicit Scope to install a pre-built one (e.g. rehydrated from a Sidekiq job payload).



46
47
48
# File 'lib/errsight.rb', line 46

def with_scope(scope = nil, &block)
  hub.with_scope(scope, &block)
end