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