~singpolyma/sgx-jmp

2b25bd947fc8a955983bf2129e8e30ae898463ac — Stephen Paul Weber 1 year, 10 months ago 13a472e
Sentry instrumentation to own file, better logger, instrument redis
2 files changed, 55 insertions(+), 13 deletions(-)

A lib/patches_for_sentry.rb
M sgx_jmp.rb
A lib/patches_for_sentry.rb => lib/patches_for_sentry.rb +54 -0
@@ 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

M sgx_jmp.rb => sgx_jmp.rb +1 -13
@@ 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"