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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/pg/instrumentation.rb', line 23
def patch_methods
::PG::Connection.class_eval do
alias_method :initialize_original, :initialize
alias_method :async_exec_original, :async_exec
alias_method :exec_original, :exec
alias_method :exec_params_original, :exec_params
alias_method :prepare_original, :prepare
alias_method :exec_prepared_original, :exec_prepared
def initialize(*args)
return initialize_original(*args) if args.empty?
hash_arg = args.first.is_a?( Hash ) ? args.first : {}
db_name = hash_arg.fetch(:dbname, nil)
db_user = hash_arg.fetch(:user, nil)
host = hash_arg.fetch(:host, nil)
port = hash_arg.fetch(:port, nil)
@shared_tags = {
'span.kind' => 'client',
'component' => 'pg',
'db.type' => 'pg'}
@shared_tags['db.instance'] = db_name if db_name
@shared_tags['db.user'] = db_user if db_user
@shared_tags['peer.hostname'] = host if host
@shared_tags['peer.port'] = port if port
@shared_tags['peer.address'] = "pg://#{host}:#{port}" if host && port
operation_name = 'pg.initialize'
scope = ::PG::Instrumentation.tracer.start_active_span(operation_name, tags: @shared_tags)
initialize_original(*args)
rescue => e
log_error(scope.span, e) if scope
ensure
scope.close if scope
end
def async_exec(*args)
tags = @shared_tags.dup
sql = args.first.to_s[0, 1024]
tags['db.statement'] = sql
default_op_name = 'pg.query'
operation_name = get_operation_name(sql, default_op_name)
scope = ::PG::Instrumentation.tracer.start_active_span(operation_name, tags: tags)
async_exec_original(*args)
rescue => e
log_error(scope.span, e) if scope
ensure
scope.close if scope
end
def exec(*args)
tags = @shared_tags.dup
sql = args.first.to_s[0, 1024]
tags['db.statement'] = sql
default_op_name = 'pg.query'
operation_name = get_operation_name(sql, default_op_name)
scope = ::PG::Instrumentation.tracer.start_active_span(operation_name, tags: tags)
exec_original(*args)
rescue => e
log_error(scope.span, e) if scope
ensure
scope.close if scope
end
def exec_params(*args)
tags = @shared_tags.dup
sql = args.first.to_s[0, 1024]
tags['db.statement'] = sql
default_op_name = 'pg.query'
operation_name = get_operation_name(sql, default_op_name)
scope = ::PG::Instrumentation.tracer.start_active_span(operation_name, tags: tags)
exec_params_original(*args)
rescue => e
log_error(scope.span, e) if scope
ensure
scope.close if scope
end
def prepare(*args)
tags = @shared_tags.dup
sql = args[1].to_str[0, 1024]
tags['db.statement'] = sql
tags['prepared.statement.name'] = args.first
default_op_name = 'pg.prepare'
operation_name = get_operation_name(sql, default_op_name)
scope = ::PG::Instrumentation.tracer.start_active_span(operation_name, tags: tags)
prepare_original(*args)
rescue => e
log_error(scope.span, e) if scope
ensure
scope.close if scope
end
def exec_prepared(*args)
tags = @shared_tags.dup
tags['prepared.statement.name'] = args.first
tags['prepared.statement.input'] = args[1][0, 21]
operation_name = 'pg.exec_prepared'
scope = ::PG::Instrumentation.tracer.start_active_span(operation_name, tags: tags)
exec_prepared_original(*args)
rescue => e
log_error(scope.span, e) if scope
ensure
scope.close if scope
end
def get_operation_name(sql, default)
sql_split = sql.split(' ')
candidate = sql_split[0].upcase if sql_split.length > 1
return candidate if !candidate.nil? && !candidate.empty?
return default
rescue
end
def log_error(span, error)
span.set_tag('error', true)
span.log_kv(key: 'message',
value: error.message,
:error_kind => error.class.to_s,
:error_object => error,
:error_stack => error.backtrace.join("\n"))
raise error
end
end end
|