Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better sentry logging in MQTT message handler #266

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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