Skip to content

Commit

Permalink
Merge pull request #266 from fablabbcn/qol/better-sentry-logging
Browse files Browse the repository at this point in the history
Better sentry logging in MQTT message handler
  • Loading branch information
oscgonfer authored Oct 26, 2023
2 parents 7a1e18e + 65e8e08 commit f2e7828
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
42 changes: 29 additions & 13 deletions app/lib/mqtt_messages_handler.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
class MqttMessagesHandler
def self.handle_topic(topic, message)
crumb = Sentry::Breadcrumb.new(
category: "MqttMessagesHandler.handle_topic",
message: "Handling topic #{topic}",
data: { topic: topic, message: message }
)
Sentry.add_breadcrumb(crumb)

return if topic.nil?

# The following do NOT need a device
Expand All @@ -20,13 +27,14 @@ def self.handle_topic(topic, message)
elsif topic.to_s.include?('readings')
handle_readings(device, message)
elsif topic.to_s.include?('info')
begin
json_info = JSON.parse(message)
rescue Exception => e
e.message << "\nmessage: #{message}"
raise e
end
device.update hardware_info: json_info
json_message = JSON.parse(message)
crumb = Sentry::Breadcrumb.new(
category: "MqttMessagesHandler.handle_topic",
message: "Parsing info message",
data: { topic: topic, message: message, json: json_message }
)
Sentry.add_breadcrumb(crumb)
device.update hardware_info: json_message
end
end

Expand All @@ -46,6 +54,12 @@ def self.handle_readings(device, message)

# takes a raw packet and converts into JSON
def self.parse_raw_readings(message)
crumb = Sentry::Breadcrumb.new(
category: "MqttMessagesHandler.parse_raw_readings",
message: "Parsing raw readings",
data: { message: message }
)
Sentry.add_breadcrumb(crumb)
clean_tm = message[1..-2].split(",")[0].gsub("t:", "").strip
raw_readings = message[1..-2].split(",")[1..]

Expand All @@ -57,12 +71,14 @@ def self.parse_raw_readings(message)
reading['data'].first['sensors'] << { 'id' => raw_id, 'value' => raw_value }
end

begin
JSON[reading]
rescue Exception => e
e.message << "\nreading: #{reading}"
raise e
end
crumb = Sentry::Breadcrumb.new(
category: "MqttMessagesHandler.parse_raw_readings",
message: "Readings data constructed",
data: { message: message, reading: reading }
)
Sentry.add_breadcrumb(crumb)

JSON[reading]
end

def self.handle_hello(orphan_device)
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/sentry.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Sentry.init do |config|
config.dsn = ENV['RAVEN_DSN_URL']
config.breadcrumbs_logger = [:active_support_logger, :http_logger]
config.breadcrumbs_logger = [:sentry_logger, :active_support_logger, :http_logger]
end

0 comments on commit f2e7828

Please sign in to comment.