Skip to content

Commit

Permalink
better sentry logging for mqtt message handler
Browse files Browse the repository at this point in the history
  • Loading branch information
timcowlishaw committed Oct 20, 2023
1 parent 9256c45 commit 65e8e08
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
29 changes: 28 additions & 1 deletion 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,7 +27,14 @@ def self.handle_topic(topic, message)
elsif topic.to_s.include?('readings')
handle_readings(device, message)
elsif topic.to_s.include?('info')
device.update hardware_info: JSON.parse(message)
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 @@ -40,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 @@ -51,6 +71,13 @@ def self.parse_raw_readings(message)
reading['data'].first['sensors'] << { 'id' => raw_id, 'value' => raw_value }
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

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 65e8e08

Please sign in to comment.