From 65e8e080e95fc870add823bb648a3cb260408f46 Mon Sep 17 00:00:00 2001 From: Tim Cowlishaw Date: Fri, 20 Oct 2023 09:50:55 +0200 Subject: [PATCH] better sentry logging for mqtt message handler --- app/lib/mqtt_messages_handler.rb | 29 ++++++++++++++++++++++++++++- config/initializers/sentry.rb | 2 +- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/lib/mqtt_messages_handler.rb b/app/lib/mqtt_messages_handler.rb index aad8c3e7..0ce7f0fb 100644 --- a/app/lib/mqtt_messages_handler.rb +++ b/app/lib/mqtt_messages_handler.rb @@ -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 @@ -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 @@ -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..] @@ -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 diff --git a/config/initializers/sentry.rb b/config/initializers/sentry.rb index 003a0916..3644d792 100644 --- a/config/initializers/sentry.rb +++ b/config/initializers/sentry.rb @@ -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