From 2b25bd947fc8a955983bf2129e8e30ae898463ac Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 26 Jul 2022 21:00:40 -0500 Subject: [PATCH] Sentry instrumentation to own file, better logger, instrument redis --- lib/patches_for_sentry.rb | 54 +++++++++++++++++++++++++++++++++++++++ sgx_jmp.rb | 14 +--------- 2 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 lib/patches_for_sentry.rb diff --git a/lib/patches_for_sentry.rb b/lib/patches_for_sentry.rb new file mode 100644 index 0000000..7f91dd1 --- /dev/null +++ b/lib/patches_for_sentry.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +module SentryOugai + def super_add(severity, data) + super + return unless Sentry.get_current_hub + + level = Sentry::Breadcrumb::SentryLogger::LEVELS.fetch(severity, nil) + Sentry.add_breadcrumb(Sentry::Breadcrumb.new( + level: level, + category: data[:category] || "logger", + message: data[:msg], + data: data.reject { |k, _| [:msg, :category].include?(k) }, + type: severity >= 3 ? "error" : level + )) + end +end + +LOG.extend SentryOugai + +module SentryEMHiredis + def respond_to_missing?(*) + super + end + + def record_span(description) + transaction = Sentry.get_current_scope.get_transaction + return yield unless transaction&.sampled + + span = transaction.start_child( + op: "db.redis.command", + description: description + ) + + df = yield + df.callback { span.finish } + df + end + + def method_missing(sym, *args) + EM::Hiredis.logger.debug( + "Sending Redis command", + category: "db.redis.command", + commands: [{ + command: sym.to_s.upcase, + key: args.first, + arguments: args[1..-1].join(" ") + }] + ) + record_span("#{sym} #{args.join(' ')}") { super } + end +end + +EM::Hiredis::Client.include SentryEMHiredis diff --git a/sgx_jmp.rb b/sgx_jmp.rb index 607bb76..841b99a 100644 --- a/sgx_jmp.rb +++ b/sgx_jmp.rb @@ -38,19 +38,6 @@ Sentry.init do |config| config.breadcrumbs_logger = [:sentry_logger] end -module SentryOugai - class SentryLogger - include Sentry::Breadcrumb::SentryLogger - include Singleton - end - - def _log(severity, message=nil, ex=nil, data=nil, &block) - super - SentryLogger.instance.add_breadcrumb(severity, message || ex.to_s, &block) - end -end -LOG.extend SentryOugai - CONFIG = Dhall::Coder .new(safe: Dhall::Coder::JSON_LIKE + [Symbol, Proc]) .load( @@ -95,6 +82,7 @@ require_relative "lib/em" require_relative "lib/form_to_h" require_relative "lib/low_balance" require_relative "lib/port_in_order" +require_relative "lib/patches_for_sentry" require_relative "lib/payment_methods" require_relative "lib/paypal_done" require_relative "lib/postgres" -- 2.45.2