Class: PeakFlowUtils::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/peak_flow_utils/notifier.rb

Defined Under Namespace

Classes: FailedToReportError, NotConfiguredError, NotifyMessageError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_token:) ⇒ Notifier

Returns a new instance of Notifier.



54
55
56
57
58
# File 'lib/peak_flow_utils/notifier.rb', line 54

def initialize(auth_token:)
  @auth_token = auth_token
  @mutex = ::Mutex.new
  @parameters = ::PeakFlowUtils::InheritedLocalVar.new({})
end

Instance Attribute Details

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



6
7
8
# File 'lib/peak_flow_utils/notifier.rb', line 6

def auth_token
  @auth_token
end

#mutexObject (readonly)

Returns the value of attribute mutex.



6
7
8
# File 'lib/peak_flow_utils/notifier.rb', line 6

def mutex
  @mutex
end

#parametersObject (readonly)

Returns the value of attribute parameters.



6
7
8
# File 'lib/peak_flow_utils/notifier.rb', line 6

def parameters
  @parameters
end

Class Method Details

.configure(auth_token:) ⇒ Object



8
9
10
# File 'lib/peak_flow_utils/notifier.rb', line 8

def self.configure(auth_token:)
  @current = PeakFlowUtils::Notifier.new(auth_token: auth_token)
end

.currentObject

rubocop:disable Style/TrivialAccessors



12
13
14
# File 'lib/peak_flow_utils/notifier.rb', line 12

def self.current # rubocop:disable Style/TrivialAccessors
  @current
end

.notify(*args, **opts, &blk) ⇒ Object



16
17
18
# File 'lib/peak_flow_utils/notifier.rb', line 16

def self.notify(*args, **opts, &blk)
  PeakFlowUtils::Notifier.current&.notify(*args, **opts, &blk)
end

.notify_message(*args, **opts, &blk) ⇒ Object



20
21
22
# File 'lib/peak_flow_utils/notifier.rb', line 20

def self.notify_message(*args, **opts, &blk)
  PeakFlowUtils::Notifier.current&.notify_message(*args, **opts, &blk)
end

.reset_parametersObject



24
25
26
# File 'lib/peak_flow_utils/notifier.rb', line 24

def self.reset_parameters
  ::PeakFlowUtils::Notifier.current&.instance_variable_set(:@parameters, ::PeakFlowUtils::InheritedLocalVar.new({}))
end

.with_parameters(parameters) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/peak_flow_utils/notifier.rb', line 28

def self.with_parameters(parameters)
  return yield unless ::PeakFlowUtils::Notifier.current

  random_id = ::SecureRandom.hex(16)

  ::PeakFlowUtils::Notifier.current.mutex.synchronize do
    raise "'parameters' was nil?" if ::PeakFlowUtils::Notifier.current.parameters.value.nil?

    parameters_with = ::PeakFlowUtils::Notifier.current.parameters.value.clone
    parameters_with[random_id] = parameters

    ::PeakFlowUtils::Notifier.current.parameters.value = parameters_with
  end

  begin
    yield
  ensure
    ::PeakFlowUtils::Notifier.current.mutex.synchronize do
      parameters_without = ::PeakFlowUtils::Notifier.current.parameters.value.clone
      parameters_without.delete(random_id)

      ::PeakFlowUtils::Notifier.current.parameters.value = parameters_without
    end
  end
end

Instance Method Details

#current_parameters(parameters: nil) ⇒ Object



60
61
62
63
64
65
# File 'lib/peak_flow_utils/notifier.rb', line 60

def current_parameters(parameters: nil)
  hashes = current_parameters_hashes
  hashes << parameters if parameters

  ::PeakFlowUtils::DeepMerger.execute!(hashes: hashes)
end

#current_parameters_hashesObject



67
68
69
# File 'lib/peak_flow_utils/notifier.rb', line 67

def current_parameters_hashes
  parameters.value.values
end

#error_message_from_response(response) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/peak_flow_utils/notifier.rb', line 71

def error_message_from_response(response)
  message = "Couldn't report error to Peakflow (code #{response.code})"

  if response["content-type"]&.starts_with?("application/json")
    response_data = ::JSON.parse(response.body)
    message << ": #{response_data.fetch("errors").join(". ")}" if response_data["errors"]
  end

  message
end

#notify(error:, environment: nil, parameters: nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/peak_flow_utils/notifier.rb', line 82

def notify(error:, environment: nil, parameters: nil)
  error_parser = ::PeakFlowUtils::NotifierErrorParser.new(
    backtrace: error.backtrace,
    environment: environment,
    error: error
  )

  merged_parameters = current_parameters(parameters: parameters)

  uri = URI("https://www.peakflow.io/errors/reports")

  data = {
    auth_token: auth_token,
    error: {
      backtrace: error.backtrace,
      environment: error_parser.cleaned_environment,
      error_class: error.class.name,
      file_path: error_parser.file_path,
      line_number: error_parser.line_number,
      message: error.message,
      parameters: merged_parameters,
      remote_ip: error_parser.remote_ip,
      url: error_parser.url,
      user_agent: error_parser.user_agent
    }
  }

  send_notify_request(data: data, uri: uri)
end

#notify_message(message, **opts) ⇒ Object



112
113
114
115
116
# File 'lib/peak_flow_utils/notifier.rb', line 112

def notify_message(message, **opts)
  raise NotifyMessageError, message
rescue NotifyMessageError => e
  notify(error: e, **opts)
end

#send_notify_request(data:, uri:) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/peak_flow_utils/notifier.rb', line 118

def send_notify_request(data:, uri:)
  https = ::Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true

  request = ::Net::HTTP::Post.new(uri.path)
  request["Content-Type"] = "application/json"
  request.body = ::JSON.generate(data)

  response = https.request(request)

  raise FailedToReportError, error_message_from_response(response) unless response.code == "200"

  response_data = ::JSON.parse(response.body)

  # Data not always present so dont use fetch
  ::PeakFlowUtils::NotifierResponse.new(
    bug_report_id: response_data["bug_report_id"],
    bug_report_instance_id: response_data["bug_report_instance_id"],
    project_id: response_data["project_id"],
    project_slug: response_data["project_slug"],
    url: response_data["url"]
  )
end