Class: TensorBuzz::Api::BugReporting

Inherits:
Object
  • Object
show all
Defined in:
lib/tensorbuzz/api/bug_reporting.rb

Constant Summary collapse

CAPTURED_PARAMETERS_VARIABLE =
:@tensorbuzz_captured_parameters
CONTEXT_KEY =
:tensorbuzz_bug_reporting_context

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, transport: Transport.new(configuration)) ⇒ BugReporting

Returns a new instance of BugReporting.



60
61
62
63
# File 'lib/tensorbuzz/api/bug_reporting.rb', line 60

def initialize(configuration, transport: Transport.new(configuration))
  @configuration = configuration
  @transport = transport
end

Class Attribute Details

.currentObject (readonly)

Returns the value of attribute current.



13
14
15
# File 'lib/tensorbuzz/api/bug_reporting.rb', line 13

def current
  @current
end

Class Method Details

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

Yields:

  • (configuration)


15
16
17
18
19
20
# File 'lib/tensorbuzz/api/bug_reporting.rb', line 15

def configure
  configuration = Configuration.new
  yield configuration
  configuration.validate!
  @current = new(configuration)
end

.contextObject



55
56
57
# File 'lib/tensorbuzz/api/bug_reporting.rb', line 55

def context
  Thread.current[CONTEXT_KEY] ||= {}
end

.report(error, **options) ⇒ Object



22
23
24
# File 'lib/tensorbuzz/api/bug_reporting.rb', line 22

def report(error, **options)
  current&.report(error, **options)
end

.report_message(message, **options) ⇒ Object



26
27
28
# File 'lib/tensorbuzz/api/bug_reporting.rb', line 26

def report_message(message, **options)
  report(RuntimeError.new(message), **options)
end

.reset!Object



50
51
52
53
# File 'lib/tensorbuzz/api/bug_reporting.rb', line 50

def reset!
  @current = nil
  Thread.current[CONTEXT_KEY] = nil
end

.safely_report(error, **options) ⇒ Object



30
31
32
33
34
35
# File 'lib/tensorbuzz/api/bug_reporting.rb', line 30

def safely_report(error, **options)
  report(error, **options)
rescue StandardError => e
  current&.log_reporting_error(e)
  nil
end

.with_parameters(parameters) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tensorbuzz/api/bug_reporting.rb', line 37

def with_parameters(parameters)
  return yield unless current

  context_id = SecureRandom.hex(16)
  context[context_id] = parameters
  yield
rescue Exception => e # rubocop:disable Lint/RescueException
  current&.capture_parameters_for_error(e)
  raise
ensure
  context.delete(context_id) if context_id
end

Instance Method Details

#capture_parameters_for_error(error) ⇒ Object



105
106
107
108
109
# File 'lib/tensorbuzz/api/bug_reporting.rb', line 105

def capture_parameters_for_error(error)
  return if error.instance_variable_defined?(CAPTURED_PARAMETERS_VARIABLE)

  error.instance_variable_set(CAPTURED_PARAMETERS_VARIABLE, merged_parameters(error, nil))
end

#log_reporting_error(error) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/tensorbuzz/api/bug_reporting.rb', line 111

def log_reporting_error(error)
  logger = configuration.logger
  message = "TensorBuzz reporting failed: #{error.class}: #{error.message}"

  if logger
    logger.error(message)
  else
    warn(message)
  end
end

#report(error, environment: nil, http_method: nil, parameters: nil, remote_ip: nil, request_body: nil, url: nil, user_agent: nil) ⇒ Object



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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/tensorbuzz/api/bug_reporting.rb', line 65

def report(error, environment: nil, http_method: nil, parameters: nil, remote_ip: nil,
           request_body: nil, url: nil, user_agent: nil)
  normalized_error = normalize_error(error)
  stack_trace = Array(normalized_error.backtrace)
  file_path, line_number = location_from(stack_trace)
  merged_environment = compact_hash(
    application_environment: configuration.environment,
    release: configuration.release
  ).merge(environment || {})
  payload = {
    authToken: configuration.auth_token,
    projectId: configuration.project_id,
    hostname: configuration.hostname || Socket.gethostname,
    runtimeEnvironment: 'ruby',
    error: {
      backtrace: stack_trace,
      environment: Sanitizer.call(merged_environment),
      error_class: normalized_error.class.name,
      file_path:,
      http_method:,
      line_number:,
      message: normalized_error.message,
      parameters: Sanitizer.call(merged_parameters(normalized_error, parameters)),
      remote_ip:,
      request_body: Sanitizer.call(request_body),
      url:,
      user_agent:
    }.compact
  }
  response = transport.post(payload)

  Response.new(
    bug_report_id: response['bugReportId'],
    bug_report_instance_id: response['bugReportInstanceId'],
    project_id: response['projectId'],
    project_slug: response['projectSlug'],
    url: response['url']
  )
end