patches_for_sentry.rb

 1# frozen_string_literal: true
 2
 3module SentryOugai
 4	def super_add(severity, data)
 5		super
 6		return unless Sentry.get_current_hub
 7
 8		level = Sentry::Breadcrumb::SentryLogger::LEVELS.fetch(severity, nil)
 9		Sentry.add_breadcrumb(Sentry::Breadcrumb.new(
10			level: level,
11			category: data[:category] || "logger",
12			message: data[:msg],
13			data: data.reject { |k, _| [:msg, :category].include?(k) },
14			type: severity >= 3 ? "error" : level
15		))
16	end
17end
18
19LOG.extend SentryOugai
20
21module SentryEMHiredis
22	def respond_to_missing?(*)
23		super
24	end
25
26	def record_span(description)
27		transaction = Sentry.get_current_scope.get_transaction
28		return yield unless transaction&.sampled
29
30		span = transaction.start_child(
31			op: "db.redis.command",
32			description: description
33		)
34
35		df = yield
36		df.callback { span.finish }
37		df
38	end
39
40	def method_missing(sym, *args)
41		EM::Hiredis.logger.debug(
42			"Sending Redis command",
43			category: "db.redis.command",
44			commands: [{
45				command: sym.to_s.upcase,
46				key: args.first,
47				arguments: args[1..-1].join(" ")
48			}]
49		)
50		record_span("#{sym} #{args.join(' ')}") { super }
51	end
52end
53
54EM::Hiredis::Client.include SentryEMHiredis
55
56module SentryIQManager
57	def record_span(description)
58		transaction = Sentry.get_current_scope.get_transaction
59		return yield unless transaction&.sampled
60
61		span = transaction.start_child(
62			op: "xmpp.iq",
63			description: description
64		)
65
66		yield.then { span.finish }
67	end
68
69	def stanza_description(stanza)
70		node = stanza.respond_to?(:node) ? stanza.node : stanza.child&.name
71		"iq #{stanza.type} #{stanza.to} #{node}"
72	end
73
74	def write(stanza)
75		# Outgoing IQ already logged by blather
76		record_span(stanza_description(stanza)) { super }
77	end
78end
79
80IQ_MANAGER.extend SentryIQManager