-
Notifications
You must be signed in to change notification settings - Fork 97
/
moped.rb
79 lines (67 loc) · 2.38 KB
/
moped.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
module ScoutApm
module Instruments
class Moped
attr_reader :context
def initialize(context)
@context = context
@installed = false
end
def logger
context.logger
end
def installed?
@installed
end
def install(prepend:)
if defined?(::Moped)
@installed = true
logger.info "Instrumenting Moped. Prepend: #{prepend}"
if prepend
::Moped::Node.send(:include, ScoutApm::Tracer)
::Moped::Node.send(:prepend, MopedInstrumentationPrepend)
else
::Moped::Node.class_eval do
include ScoutApm::Tracer
def process_with_scout_instruments(operation, &callback)
if operation.respond_to?(:collection)
collection = operation.collection
name = "Process/#{collection}/#{operation.class.to_s.split('::').last}"
self.class.instrument("MongoDB", name, :annotate_layer => { :query => scout_sanitize_log(operation.log_inspect) }) do
process_without_scout_instruments(operation, &callback)
end
end
end
alias_method :process_without_scout_instruments, :process
alias_method :process, :process_with_scout_instruments
# replaces values w/ ?
def scout_sanitize_log(log)
return nil if log.length > 1000 # safeguard - don't sanitize large SQL statements
log.gsub(/(=>")((?:[^"]|"")*)"/) do
$1 + '?' + '"'
end
end
end
end
end
end
end
module MopedInstrumentationPrepend
def process(operation, &callback)
if operation.respond_to?(:collection)
collection = operation.collection
name = "Process/#{collection}/#{operation.class.to_s.split('::').last}"
self.class.instrument("MongoDB", name, :annotate_layer => { :query => scout_sanitize_log(operation.log_inspect) }) do
super(operation, &callback)
end
end
end
# replaces values w/ ?
def scout_sanitize_log(log)
return nil if log.length > 1000 # safeguard - don't sanitize large SQL statements
log.gsub(/(=>")((?:[^"]|"")*)"/) do
$1 + '?' + '"'
end
end
end
end
end