Class: PeakFlowUtils::Notifier
- Inherits:
-
Object
- Object
- PeakFlowUtils::Notifier
- Defined in:
- lib/peak_flow_utils/notifier.rb
Defined Under Namespace
Classes: FailedToReportError, NotConfiguredError, NotifyMessageError
Instance Attribute Summary collapse
-
#auth_token ⇒ Object
readonly
Returns the value of attribute auth_token.
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
Class Method Summary collapse
- .configure(auth_token:) ⇒ Object
-
.current ⇒ Object
rubocop:disable Style/TrivialAccessors.
- .notify(*args, **opts, &blk) ⇒ Object
- .notify_message(*args, **opts, &blk) ⇒ Object
- .reset_parameters ⇒ Object
- .with_parameters(parameters) ⇒ Object
Instance Method Summary collapse
- #current_parameters(parameters: nil) ⇒ Object
- #current_parameters_hashes ⇒ Object
- #error_message_from_response(response) ⇒ Object
-
#initialize(auth_token:) ⇒ Notifier
constructor
A new instance of Notifier.
- #notify(error:, environment: nil, parameters: nil) ⇒ Object
- #notify_message(message, **opts) ⇒ Object
- #send_notify_request(data:, uri:) ⇒ Object
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_token ⇒ Object (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 |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
6 7 8 |
# File 'lib/peak_flow_utils/notifier.rb', line 6 def mutex @mutex end |
#parameters ⇒ Object (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 |
.current ⇒ Object
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.(*args, **opts, &blk) PeakFlowUtils::Notifier.current&.(*args, **opts, &blk) end |
.reset_parameters ⇒ Object
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_hashes ⇒ Object
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 (response) = "Couldn't report error to Peakflow (code #{response.code})" if response["content-type"]&.starts_with?("application/json") response_data = ::JSON.parse(response.body) << ": #{response_data.fetch("errors").join(". ")}" if response_data["errors"] end 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., 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 (, **opts) raise NotifyMessageError, 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, (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 |