Module: Bugwatch

Defined in:
lib/bugwatch.rb,
lib/bugwatch/railtie.rb,
lib/bugwatch/version.rb,
lib/bugwatch/db_tracker.rb,
lib/bugwatch/middleware.rb,
lib/bugwatch/http_tracker.rb,
lib/bugwatch/notification.rb,
lib/bugwatch/user_context.rb,
lib/bugwatch/configuration.rb,
lib/bugwatch/error_builder.rb,
lib/bugwatch/report_builder.rb,
lib/bugwatch/db_query_buffer.rb,
lib/bugwatch/db_query_sender.rb,
lib/bugwatch/feedback_helper.rb,
lib/bugwatch/feedback_sender.rb,
lib/bugwatch/request_context.rb,
lib/bugwatch/error_subscriber.rb,
lib/bugwatch/http_call_buffer.rb,
lib/bugwatch/http_call_sender.rb,
lib/bugwatch/backtrace_cleaner.rb,
lib/bugwatch/active_job_handler.rb,
lib/bugwatch/transaction_buffer.rb,
lib/bugwatch/transaction_sender.rb,
lib/bugwatch/reported_exceptions.rb,
lib/bugwatch/breadcrumb_collector.rb,
lib/bugwatch/controller_rescue_hook.rb,
lib/bugwatch/rescued_exception_tracker.rb

Defined Under Namespace

Modules: BreadcrumbCollector, ControllerMethods, ControllerRescueHook, DbTracker, FeedbackHelper, HttpTracker, ReportedExceptions, RequestContext, UserContext Classes: ActiveJobHandler, BacktraceCleaner, Configuration, DbQueryBuffer, DbQuerySender, ErrorBuilder, ErrorSubscriber, FeedbackSender, HttpCallBuffer, HttpCallSender, Middleware, Notification, Railtie, ReportBuilder, RescuedExceptionTracker, TransactionBuffer, TransactionSender

Constant Summary collapse

VERSION =
"0.9.0"

Class Method Summary collapse

Class Method Details

.check_in(slug) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bugwatch.rb', line 77

def check_in(slug)
  return unless configuration.api_key
  return unless configuration.endpoint

  Thread.new do
    uri = URI.parse("#{configuration.endpoint.chomp("/")}/api/v1/monitors/#{slug}/check_in")

    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl       = uri.scheme == "https"
    http.open_timeout  = 3
    http.read_timeout  = 3
    http.write_timeout = 3

    request = Net::HTTP::Post.new(uri.path)
    request["X-Api-Key"]       = configuration.api_key
    request["X-BugWatch-Ruby"] = Bugwatch::VERSION

    http.request(request)
  rescue StandardError
    # Fire-and-forget: never affect the host app
  end
end

.configurationObject



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

def configuration
  @configuration ||= Configuration.new
end

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

Yields:



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

def configure
  yield configuration
end

.db_query_bufferObject



164
165
166
# File 'lib/bugwatch.rb', line 164

def db_query_buffer
  @db_query_buffer ||= DbQueryBuffer.new(config: configuration)
end

.http_call_bufferObject



168
169
170
# File 'lib/bugwatch.rb', line 168

def http_call_buffer
  @http_call_buffer ||= HttpCallBuffer.new(config: configuration)
end

.leave_breadcrumb(message, type: "manual", metadata: {}) ⇒ Object



73
74
75
# File 'lib/bugwatch.rb', line 73

def leave_breadcrumb(message, type: "manual", metadata: {})
  BreadcrumbCollector.add(message, type: type, metadata: )
end

.notify(exception, context: {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/bugwatch.rb', line 41

def notify(exception, context: {})
  return if configuration.ignore?(exception)
  return unless configuration.notify_for_release_stage?
  return if ReportedExceptions.reported?(exception)

  payload = ErrorBuilder.new(exception, RequestContext.get).build
  payload.merge!(context)
  Notification.new(payload).deliver
  ReportedExceptions.mark(exception)
end

.report(title, category: "other", severity: "warning", tags: {}) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/bugwatch.rb', line 52

def report(title, category: "other", severity: "warning", tags: {})
  return unless configuration.notify_for_release_stage?
  return if title.to_s.strip.empty?

  payload = ReportBuilder.new(title, category: category, severity: severity, tags: tags).build
  Notification.new(payload).deliver
end

.send_feedback(message, email: nil, name: nil, url: nil, issue_id: nil, metadata: {}) ⇒ Object



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

def send_feedback(message, email: nil, name: nil, url: nil, issue_id: nil, metadata: {})
  return if message.to_s.strip.empty?

  FeedbackSender.new(
    message: message, email: email, name: name,
    url: url, issue_id: issue_id, metadata: 
  ).deliver
end

.set_user(id: nil, email: nil, name: nil, **custom) ⇒ Object



69
70
71
# File 'lib/bugwatch.rb', line 69

def set_user(id: nil, email: nil, name: nil, **custom)
  UserContext.set(id: id, email: email, name: name, **custom)
end

.skip_tracking?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/bugwatch.rb', line 156

def skip_tracking?
  Thread.current[:bugwatch_skip_tracking] == true
end

.track_deploy(version:, environment: nil, description: nil, deployed_by: nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/bugwatch.rb', line 100

def track_deploy(version:, environment: nil, description: nil, deployed_by: nil)
  logger = configuration.logger

  unless configuration.api_key
    logger&.warn("[bugwatch] track_deploy skipped: configuration.api_key is not set")
    return
  end
  unless configuration.endpoint
    logger&.warn("[bugwatch] track_deploy skipped: configuration.endpoint is not set")
    return
  end

  env = environment || configuration.release_stage
  logger&.info("[bugwatch] track_deploy posting version=#{version} environment=#{env} endpoint=#{configuration.endpoint}")

  Thread.new do
    uri = URI.parse("#{configuration.endpoint.chomp("/")}/api/v1/deploys")

    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl       = uri.scheme == "https"
    http.open_timeout  = 3
    http.read_timeout  = 3
    http.write_timeout = 3

    request = Net::HTTP::Post.new(uri.path)
    request["Content-Type"]    = "application/json"
    request["X-Api-Key"]       = configuration.api_key
    request["X-BugWatch-Ruby"] = Bugwatch::VERSION

    payload = { version: version, environment: env, deployed_at: Time.now.iso8601 }
    payload[:description] = description if description
    payload[:deployed_by] = deployed_by if deployed_by

    request.body = JSON.generate(payload)

    response = http.request(request)
    status = response.code.to_i
    if status.between?(200, 299)
      logger&.info("[bugwatch] track_deploy succeeded status=#{status}")
    else
      logger&.warn("[bugwatch] track_deploy rejected status=#{status} body=#{response.body.to_s[0, 500]}")
    end
    response
  rescue StandardError => e
    logger&.error("[bugwatch] track_deploy error: #{e.class}: #{e.message}")
    e
  end
end

.transaction_bufferObject



160
161
162
# File 'lib/bugwatch.rb', line 160

def transaction_buffer
  @transaction_buffer ||= TransactionBuffer.new(config: configuration)
end

.without_trackingObject



149
150
151
152
153
154
# File 'lib/bugwatch.rb', line 149

def without_tracking
  Thread.current[:bugwatch_skip_tracking] = true
  yield
ensure
  Thread.current[:bugwatch_skip_tracking] = false
end