oboe 2.7.6.2 → 2.7.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,320 @@
1
+ require 'minitest_helper'
2
+
3
+ unless defined?(JRUBY_VERSION)
4
+
5
+ if ENV.key?('TRAVIS_MYSQL_PASS')
6
+ MYSQL_DB = Sequel.connect("mysql://root:#{ENV['TRAVIS_MYSQL_PASS']}@127.0.0.1:3306/travis_ci_test")
7
+ else
8
+ MYSQL_DB = Sequel.connect('mysql://root@127.0.0.1:3306/travis_ci_test')
9
+ end
10
+
11
+ unless MYSQL_DB.table_exists?(:items)
12
+ MYSQL_DB.create_table :items do
13
+ primary_key :id
14
+ String :name
15
+ Float :price
16
+ end
17
+ end
18
+
19
+ describe "Oboe::Inst::Sequel (mysql)" do
20
+ before do
21
+ clear_all_traces
22
+
23
+ # These are standard entry/exit KVs that are passed up with all sequel operations
24
+ @entry_kvs = {
25
+ 'Layer' => 'sequel',
26
+ 'Label' => 'entry',
27
+ 'Database' => 'travis_ci_test',
28
+ 'RemoteHost' => '127.0.0.1',
29
+ 'RemotePort' => '3306' }
30
+
31
+ @exit_kvs = { 'Layer' => 'sequel', 'Label' => 'exit' }
32
+ @collect_backtraces = Oboe::Config[:sequel][:collect_backtraces]
33
+ @sanitize_sql = Oboe::Config[:sanitize_sql]
34
+ end
35
+
36
+ after do
37
+ Oboe::Config[:sequel][:collect_backtraces] = @collect_backtraces
38
+ Oboe::Config[:sanitize_sql] = @sanitize_sql
39
+ end
40
+
41
+ it 'Stock sequel should be loaded, defined and ready' do
42
+ defined?(::Sequel).wont_match nil
43
+ end
44
+
45
+ it 'sequel should have oboe methods defined' do
46
+ # Sequel::Database
47
+ ::Sequel::Database.method_defined?(:run_with_oboe).must_equal true
48
+
49
+ # Sequel::Dataset
50
+ ::Sequel::Dataset.method_defined?(:execute_with_oboe).must_equal true
51
+ ::Sequel::Dataset.method_defined?(:execute_ddl_with_oboe).must_equal true
52
+ ::Sequel::Dataset.method_defined?(:execute_dui_with_oboe).must_equal true
53
+ ::Sequel::Dataset.method_defined?(:execute_insert_with_oboe).must_equal true
54
+ end
55
+
56
+ it "should obey :collect_backtraces setting when true" do
57
+ Oboe::Config[:sequel][:collect_backtraces] = true
58
+
59
+ Oboe::API.start_trace('sequel_test', '', {}) do
60
+ MYSQL_DB.run('select 1')
61
+ end
62
+
63
+ traces = get_all_traces
64
+ layer_has_key(traces, 'sequel', 'Backtrace')
65
+ end
66
+
67
+ it "should obey :collect_backtraces setting when false" do
68
+ Oboe::Config[:sequel][:collect_backtraces] = false
69
+
70
+ Oboe::API.start_trace('sequel_test', '', {}) do
71
+ MYSQL_DB.run('select 1')
72
+ end
73
+
74
+ traces = get_all_traces
75
+ layer_doesnt_have_key(traces, 'sequel', 'Backtrace')
76
+ end
77
+
78
+ it 'should trace MYSQL_DB.run insert' do
79
+ Oboe::API.start_trace('sequel_test', '', {}) do
80
+ MYSQL_DB.run("insert into items (name, price) values ('blah', '12')")
81
+ end
82
+
83
+ traces = get_all_traces
84
+
85
+ traces.count.must_equal 4
86
+ validate_outer_layers(traces, 'sequel_test')
87
+
88
+ validate_event_keys(traces[1], @entry_kvs)
89
+ traces[1]['Query'].must_equal "insert into items (name, price) values ('blah', '12')"
90
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
91
+ validate_event_keys(traces[2], @exit_kvs)
92
+ end
93
+
94
+ it 'should trace MYSQL_DB.run select' do
95
+ Oboe::API.start_trace('sequel_test', '', {}) do
96
+ MYSQL_DB.run("select 1")
97
+ end
98
+
99
+ traces = get_all_traces
100
+
101
+ traces.count.must_equal 4
102
+ validate_outer_layers(traces, 'sequel_test')
103
+
104
+ validate_event_keys(traces[1], @entry_kvs)
105
+ traces[1]['Query'].must_equal "select 1"
106
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
107
+ validate_event_keys(traces[2], @exit_kvs)
108
+ end
109
+
110
+ it 'should trace a dataset insert and count' do
111
+ items = MYSQL_DB[:items]
112
+ items.count
113
+
114
+ Oboe::API.start_trace('sequel_test', '', {}) do
115
+ items.insert(:name => 'abc', :price => 2.514461383352462)
116
+ items.count
117
+ end
118
+
119
+ traces = get_all_traces
120
+
121
+ traces.count.must_equal 6
122
+ validate_outer_layers(traces, 'sequel_test')
123
+
124
+ validate_event_keys(traces[1], @entry_kvs)
125
+ if RUBY_VERSION < "1.9"
126
+ traces[1]['Query'].must_equal "INSERT INTO `items` (`price`, `name`) VALUES (2.51446138335246, 'abc')"
127
+ else
128
+ traces[1]['Query'].must_equal "INSERT INTO `items` (`name`, `price`) VALUES ('abc', 2.514461383352462)"
129
+ end
130
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
131
+ traces[2]['Layer'].must_equal "sequel"
132
+ traces[2]['Label'].must_equal "exit"
133
+ traces[3]['Query'].downcase.must_equal "select count(*) as `count` from `items` limit 1"
134
+ validate_event_keys(traces[4], @exit_kvs)
135
+ end
136
+
137
+ it 'should trace a dataset insert and obey query privacy' do
138
+ Oboe::Config[:sanitize_sql] = true
139
+ items = MYSQL_DB[:items]
140
+ items.count
141
+
142
+ Oboe::API.start_trace('sequel_test', '', {}) do
143
+ items.insert(:name => 'abc', :price => 2.514461383352462)
144
+ end
145
+
146
+ traces = get_all_traces
147
+
148
+ traces.count.must_equal 4
149
+ validate_outer_layers(traces, 'sequel_test')
150
+
151
+ validate_event_keys(traces[1], @entry_kvs)
152
+ if RUBY_VERSION < "1.9"
153
+ traces[1]['Query'].must_equal "INSERT INTO `items` (`price`, `name`) VALUES (?, ?)"
154
+ else
155
+ traces[1]['Query'].must_equal "INSERT INTO `items` (`name`, `price`) VALUES (?, ?)"
156
+ end
157
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
158
+ validate_event_keys(traces[2], @exit_kvs)
159
+ end
160
+
161
+ it 'should trace a dataset filter' do
162
+ items = MYSQL_DB[:items]
163
+ items.count
164
+
165
+ Oboe::API.start_trace('sequel_test', '', {}) do
166
+ items.filter(:name => 'abc').all
167
+ end
168
+
169
+ traces = get_all_traces
170
+
171
+ traces.count.must_equal 4
172
+ validate_outer_layers(traces, 'sequel_test')
173
+
174
+ validate_event_keys(traces[1], @entry_kvs)
175
+ traces[1]['Query'].must_equal "SELECT * FROM `items` WHERE (`name` = 'abc')"
176
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
177
+ validate_event_keys(traces[2], @exit_kvs)
178
+ end
179
+
180
+ it 'should trace create table' do
181
+ # Drop the table if it already exists
182
+ MYSQL_DB.drop_table(:fake) if MYSQL_DB.table_exists?(:fake)
183
+
184
+ Oboe::API.start_trace('sequel_test', '', {}) do
185
+ MYSQL_DB.create_table :fake do
186
+ primary_key :id
187
+ String :name
188
+ Float :price
189
+ end
190
+ end
191
+
192
+ traces = get_all_traces
193
+
194
+ traces.count.must_equal 4
195
+ validate_outer_layers(traces, 'sequel_test')
196
+
197
+ validate_event_keys(traces[1], @entry_kvs)
198
+ traces[1]['Query'].must_equal "CREATE TABLE `fake` (`id` integer PRIMARY KEY AUTO_INCREMENT, `name` varchar(255), `price` double precision)"
199
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
200
+ validate_event_keys(traces[2], @exit_kvs)
201
+ end
202
+
203
+ it 'should trace add index' do
204
+ # Drop the table if it already exists
205
+ MYSQL_DB.drop_table(:fake) if MYSQL_DB.table_exists?(:fake)
206
+
207
+ Oboe::API.start_trace('sequel_test', '', {}) do
208
+ MYSQL_DB.create_table :fake do
209
+ primary_key :id
210
+ String :name
211
+ Float :price
212
+ end
213
+ end
214
+
215
+ traces = get_all_traces
216
+
217
+ traces.count.must_equal 4
218
+ validate_outer_layers(traces, 'sequel_test')
219
+
220
+ validate_event_keys(traces[1], @entry_kvs)
221
+ traces[1]['Query'].must_equal "CREATE TABLE `fake` (`id` integer PRIMARY KEY AUTO_INCREMENT, `name` varchar(255), `price` double precision)"
222
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
223
+ validate_event_keys(traces[2], @exit_kvs)
224
+ end
225
+
226
+ it 'should capture and report exceptions' do
227
+ begin
228
+ Oboe::API.start_trace('sequel_test', '', {}) do
229
+ MYSQL_DB.run("this is bad sql")
230
+ end
231
+ rescue
232
+ # Do nothing - we're testing exception logging
233
+ end
234
+
235
+ traces = get_all_traces
236
+
237
+ traces.count.must_equal 5
238
+ validate_outer_layers(traces, 'sequel_test')
239
+
240
+ validate_event_keys(traces[1], @entry_kvs)
241
+ traces[1]['Query'].must_equal "this is bad sql"
242
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
243
+ traces[2]['Layer'].must_equal "sequel"
244
+ traces[2]['Label'].must_equal "error"
245
+ traces[2].has_key?('Backtrace').must_equal true
246
+ traces[2]['ErrorClass'].must_equal "Sequel::DatabaseError"
247
+ validate_event_keys(traces[3], @exit_kvs)
248
+ end
249
+
250
+ it 'should trace placeholder queries with bound vars' do
251
+ items = MYSQL_DB[:items]
252
+ items.count
253
+
254
+ Oboe::API.start_trace('sequel_test', '', {}) do
255
+ ds = items.where(:name=>:$n)
256
+ ds.call(:select, :n=>'abc')
257
+ ds.call(:delete, :n=>'cba')
258
+ end
259
+
260
+ traces = get_all_traces
261
+
262
+ traces.count.must_equal 6
263
+ validate_outer_layers(traces, 'sequel_test')
264
+
265
+ validate_event_keys(traces[1], @entry_kvs)
266
+ traces[1]['Query'].must_equal "SELECT * FROM `items` WHERE (`name` = 'abc')"
267
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
268
+ traces[3]['Query'].must_equal "DELETE FROM `items` WHERE (`name` = 'cba')"
269
+ traces[3].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
270
+ validate_event_keys(traces[2], @exit_kvs)
271
+ end
272
+
273
+ it 'should trace prepared statements' do
274
+ ds = MYSQL_DB[:items].filter(:name=>:$n)
275
+ ps = ds.prepare(:select, :select_by_name)
276
+
277
+ Oboe::API.start_trace('sequel_test', '', {}) do
278
+ ps.call(:n=>'abc')
279
+ end
280
+
281
+ traces = get_all_traces
282
+
283
+ traces.count.must_equal 4
284
+ validate_outer_layers(traces, 'sequel_test')
285
+
286
+ validate_event_keys(traces[1], @entry_kvs)
287
+ traces[1]['Query'].must_equal "select_by_name"
288
+ if RUBY_VERSION < "1.9"
289
+ traces[1]['QueryArgs'].must_equal "abc"
290
+ else
291
+ traces[1]['QueryArgs'].must_equal "[\"abc\"]"
292
+ end
293
+ traces[1]['IsPreparedStatement'].must_equal "true"
294
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
295
+ validate_event_keys(traces[2], @exit_kvs)
296
+ end
297
+
298
+ it 'should trace prep\'d stmnts and obey query privacy' do
299
+ Oboe::Config[:sanitize_sql] = true
300
+ ds = MYSQL_DB[:items].filter(:name=>:$n)
301
+ ps = ds.prepare(:select, :select_by_name)
302
+
303
+ Oboe::API.start_trace('sequel_test', '', {}) do
304
+ ps.call(:n=>'abc')
305
+ end
306
+
307
+ traces = get_all_traces
308
+
309
+ traces.count.must_equal 4
310
+ validate_outer_layers(traces, 'sequel_test')
311
+
312
+ validate_event_keys(traces[1], @entry_kvs)
313
+ traces[1]['Query'].must_equal "select_by_name"
314
+ traces[1]['QueryArgs'].must_equal nil
315
+ traces[1]['IsPreparedStatement'].must_equal "true"
316
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
317
+ validate_event_keys(traces[2], @exit_kvs)
318
+ end
319
+ end
320
+ end
@@ -0,0 +1,324 @@
1
+ require 'minitest_helper'
2
+
3
+ unless defined?(JRUBY_VERSION)
4
+
5
+ if ENV.key?('TRAVIS_PSQL_PASS')
6
+ PG_DB = Sequel.connect("postgres://postgres:#{ENV['TRAVIS_PSQL_PASS']}@127.0.0.1:5432/travis_ci_test")
7
+ else
8
+ PG_DB = Sequel.connect('postgres://postgres@127.0.0.1:5432/travis_ci_test')
9
+ end
10
+
11
+ unless PG_DB.table_exists?(:items)
12
+ PG_DB.create_table :items do
13
+ primary_key :id
14
+ String :name
15
+ Float :price
16
+ end
17
+ end
18
+
19
+ describe "Oboe::Inst::Sequel (postgres)" do
20
+ before do
21
+ clear_all_traces
22
+
23
+ # These are standard entry/exit KVs that are passed up with all sequel operations
24
+ @entry_kvs = {
25
+ 'Layer' => 'sequel',
26
+ 'Label' => 'entry',
27
+ 'Database' => 'travis_ci_test',
28
+ 'RemoteHost' => '127.0.0.1',
29
+ 'RemotePort' => '5432' }
30
+
31
+ @exit_kvs = { 'Layer' => 'sequel', 'Label' => 'exit' }
32
+ @collect_backtraces = Oboe::Config[:sequel][:collect_backtraces]
33
+ @sanitize_sql = Oboe::Config[:sanitize_sql]
34
+ end
35
+
36
+ after do
37
+ Oboe::Config[:sequel][:collect_backtraces] = @collect_backtraces
38
+ Oboe::Config[:sanitize_sql] = @sanitize_sql
39
+ end
40
+
41
+ it 'Stock sequel should be loaded, defined and ready' do
42
+ defined?(::Sequel).wont_match nil
43
+ end
44
+
45
+ it 'sequel should have oboe methods defined' do
46
+ # Sequel::Database
47
+ ::Sequel::Database.method_defined?(:run_with_oboe).must_equal true
48
+
49
+ # Sequel::Dataset
50
+ ::Sequel::Dataset.method_defined?(:execute_with_oboe).must_equal true
51
+ ::Sequel::Dataset.method_defined?(:execute_ddl_with_oboe).must_equal true
52
+ ::Sequel::Dataset.method_defined?(:execute_dui_with_oboe).must_equal true
53
+ ::Sequel::Dataset.method_defined?(:execute_insert_with_oboe).must_equal true
54
+ end
55
+
56
+ it "should obey :collect_backtraces setting when true" do
57
+ Oboe::Config[:sequel][:collect_backtraces] = true
58
+
59
+ Oboe::API.start_trace('sequel_test', '', {}) do
60
+ PG_DB.run('select 1')
61
+ end
62
+
63
+ traces = get_all_traces
64
+ layer_has_key(traces, 'sequel', 'Backtrace')
65
+ end
66
+
67
+ it "should obey :collect_backtraces setting when false" do
68
+ Oboe::Config[:sequel][:collect_backtraces] = false
69
+
70
+ Oboe::API.start_trace('sequel_test', '', {}) do
71
+ PG_DB.run('select 1')
72
+ end
73
+
74
+ traces = get_all_traces
75
+ layer_doesnt_have_key(traces, 'sequel', 'Backtrace')
76
+ end
77
+
78
+ it 'should trace PG_DB.run insert' do
79
+ Oboe::API.start_trace('sequel_test', '', {}) do
80
+ PG_DB.run("insert into items (name, price) values ('blah', '12')")
81
+ end
82
+
83
+ traces = get_all_traces
84
+
85
+ traces.count.must_equal 4
86
+ validate_outer_layers(traces, 'sequel_test')
87
+
88
+ validate_event_keys(traces[1], @entry_kvs)
89
+ traces[1]['Query'].must_equal "insert into items (name, price) values ('blah', '12')"
90
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
91
+ validate_event_keys(traces[2], @exit_kvs)
92
+ end
93
+
94
+ it 'should trace PG_DB.run select' do
95
+ Oboe::API.start_trace('sequel_test', '', {}) do
96
+ PG_DB.run("select 1")
97
+ end
98
+
99
+ traces = get_all_traces
100
+
101
+ traces.count.must_equal 4
102
+ validate_outer_layers(traces, 'sequel_test')
103
+
104
+ validate_event_keys(traces[1], @entry_kvs)
105
+ traces[1]['Query'].must_equal "select 1"
106
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
107
+ validate_event_keys(traces[2], @exit_kvs)
108
+ end
109
+
110
+ it 'should trace a dataset insert and count' do
111
+ items = PG_DB[:items]
112
+ # Preload the primary key to avoid breaking tests with the seemingly
113
+ # random lookup (random due to random test order)
114
+ PG_DB.primary_key(:items)
115
+
116
+ Oboe::API.start_trace('sequel_test', '', {}) do
117
+ items.insert(:name => 'abc', :price => 2.514461383352462)
118
+ items.count
119
+ end
120
+
121
+ traces = get_all_traces
122
+
123
+ traces.count.must_equal 6
124
+ validate_outer_layers(traces, 'sequel_test')
125
+
126
+ validate_event_keys(traces[1], @entry_kvs)
127
+ if RUBY_VERSION < "1.9"
128
+ traces[1]['Query'].must_equal "INSERT INTO \"items\" (\"price\", \"name\") VALUES (2.51446138335246, 'abc') RETURNING \"id\""
129
+ else
130
+ traces[1]['Query'].must_equal "INSERT INTO \"items\" (\"name\", \"price\") VALUES ('abc', 2.514461383352462) RETURNING \"id\""
131
+ end
132
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
133
+ traces[2]['Layer'].must_equal "sequel"
134
+ traces[2]['Label'].must_equal "exit"
135
+ traces[3]['Query'].downcase.must_equal "select count(*) as \"count\" from \"items\" limit 1"
136
+ validate_event_keys(traces[4], @exit_kvs)
137
+ end
138
+
139
+ it 'should trace a dataset insert and obey query privacy' do
140
+ Oboe::Config[:sanitize_sql] = true
141
+ items = PG_DB[:items]
142
+ # Preload the primary key to avoid breaking tests with the seemingly
143
+ # random lookup (random due to random test order)
144
+ PG_DB.primary_key(:items)
145
+
146
+ Oboe::API.start_trace('sequel_test', '', {}) do
147
+ items.insert(:name => 'abc', :price => 2.514461383352462)
148
+ end
149
+
150
+ traces = get_all_traces
151
+
152
+ traces.count.must_equal 4
153
+ validate_outer_layers(traces, 'sequel_test')
154
+
155
+ validate_event_keys(traces[1], @entry_kvs)
156
+ if RUBY_VERSION < "1.9"
157
+ traces[1]['Query'].must_equal "INSERT INTO \"items\" (\"price\", \"name\") VALUES (?, ?) RETURNING \"id\""
158
+ else
159
+ traces[1]['Query'].must_equal "INSERT INTO \"items\" (\"name\", \"price\") VALUES (?, ?) RETURNING \"id\""
160
+ end
161
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
162
+ validate_event_keys(traces[2], @exit_kvs)
163
+ end
164
+
165
+ it 'should trace a dataset filter' do
166
+ items = PG_DB[:items]
167
+ items.count
168
+
169
+ Oboe::API.start_trace('sequel_test', '', {}) do
170
+ items.filter(:name => 'abc').all
171
+ end
172
+
173
+ traces = get_all_traces
174
+
175
+ traces.count.must_equal 4
176
+ validate_outer_layers(traces, 'sequel_test')
177
+
178
+ validate_event_keys(traces[1], @entry_kvs)
179
+ traces[1]['Query'].must_equal "SELECT * FROM \"items\" WHERE (\"name\" = 'abc')"
180
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
181
+ validate_event_keys(traces[2], @exit_kvs)
182
+ end
183
+
184
+ it 'should trace create table' do
185
+ # Drop the table if it already exists
186
+ PG_DB.drop_table(:fake) if PG_DB.table_exists?(:fake)
187
+
188
+ Oboe::API.start_trace('sequel_test', '', {}) do
189
+ PG_DB.create_table :fake do
190
+ primary_key :id
191
+ String :name
192
+ Float :price
193
+ end
194
+ end
195
+
196
+ traces = get_all_traces
197
+
198
+ traces.count.must_equal 4
199
+ validate_outer_layers(traces, 'sequel_test')
200
+
201
+ validate_event_keys(traces[1], @entry_kvs)
202
+ traces[1]['Query'].must_equal "CREATE TABLE \"fake\" (\"id\" serial PRIMARY KEY, \"name\" text, \"price\" double precision)"
203
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
204
+ validate_event_keys(traces[2], @exit_kvs)
205
+ end
206
+
207
+ it 'should trace add index' do
208
+ # Drop the table if it already exists
209
+ PG_DB.drop_table(:fake) if PG_DB.table_exists?(:fake)
210
+
211
+ Oboe::API.start_trace('sequel_test', '', {}) do
212
+ PG_DB.create_table :fake do
213
+ primary_key :id
214
+ String :name
215
+ Float :price
216
+ end
217
+ end
218
+
219
+ traces = get_all_traces
220
+
221
+ traces.count.must_equal 4
222
+ validate_outer_layers(traces, 'sequel_test')
223
+
224
+ validate_event_keys(traces[1], @entry_kvs)
225
+ traces[1]['Query'].must_equal "CREATE TABLE \"fake\" (\"id\" serial PRIMARY KEY, \"name\" text, \"price\" double precision)"
226
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
227
+ validate_event_keys(traces[2], @exit_kvs)
228
+ end
229
+
230
+ it 'should capture and report exceptions' do
231
+ begin
232
+ Oboe::API.start_trace('sequel_test', '', {}) do
233
+ PG_DB.run("this is bad sql")
234
+ end
235
+ rescue
236
+ # Do nothing - we're testing exception logging
237
+ end
238
+
239
+ traces = get_all_traces
240
+
241
+ traces.count.must_equal 5
242
+ validate_outer_layers(traces, 'sequel_test')
243
+
244
+ validate_event_keys(traces[1], @entry_kvs)
245
+ traces[1]['Query'].must_equal "this is bad sql"
246
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
247
+ traces[2]['Layer'].must_equal "sequel"
248
+ traces[2]['Label'].must_equal "error"
249
+ traces[2].has_key?('Backtrace').must_equal true
250
+ traces[2]['ErrorClass'].must_equal "Sequel::DatabaseError"
251
+ validate_event_keys(traces[3], @exit_kvs)
252
+ end
253
+
254
+ it 'should trace placeholder queries with bound vars' do
255
+ items = PG_DB[:items]
256
+ items.count
257
+
258
+ Oboe::API.start_trace('sequel_test', '', {}) do
259
+ ds = items.where(:name=>:$n)
260
+ ds.call(:select, :n=>'abc')
261
+ ds.call(:delete, :n=>'cba')
262
+ end
263
+
264
+ traces = get_all_traces
265
+
266
+ traces.count.must_equal 6
267
+ validate_outer_layers(traces, 'sequel_test')
268
+
269
+ validate_event_keys(traces[1], @entry_kvs)
270
+ traces[1]['Query'].must_equal "SELECT * FROM \"items\" WHERE (\"name\" = $1)"
271
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
272
+ traces[3]['Query'].must_equal "DELETE FROM \"items\" WHERE (\"name\" = $1)"
273
+ traces[3].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
274
+ validate_event_keys(traces[2], @exit_kvs)
275
+ end
276
+
277
+ it 'should trace prepared statements' do
278
+ ds = PG_DB[:items].filter(:name=>:$n)
279
+ ps = ds.prepare(:select, :select_by_name)
280
+
281
+ Oboe::API.start_trace('sequel_test', '', {}) do
282
+ ps.call(:n=>'abc')
283
+ end
284
+
285
+ traces = get_all_traces
286
+
287
+ traces.count.must_equal 4
288
+ validate_outer_layers(traces, 'sequel_test')
289
+
290
+ validate_event_keys(traces[1], @entry_kvs)
291
+ traces[1]['Query'].must_equal "select_by_name"
292
+ if RUBY_VERSION < "1.9"
293
+ traces[1]['QueryArgs'].must_equal "abc"
294
+ else
295
+ traces[1]['QueryArgs'].must_equal "[\"abc\"]"
296
+ end
297
+ traces[1]['IsPreparedStatement'].must_equal "true"
298
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
299
+ validate_event_keys(traces[2], @exit_kvs)
300
+ end
301
+
302
+ it 'should trace prep\'d stmnts and obey query privacy' do
303
+ Oboe::Config[:sanitize_sql] = true
304
+ ds = PG_DB[:items].filter(:name=>:$n)
305
+ ps = ds.prepare(:select, :select_by_name)
306
+
307
+ Oboe::API.start_trace('sequel_test', '', {}) do
308
+ ps.call(:n=>'abc')
309
+ end
310
+
311
+ traces = get_all_traces
312
+
313
+ traces.count.must_equal 4
314
+ validate_outer_layers(traces, 'sequel_test')
315
+
316
+ validate_event_keys(traces[1], @entry_kvs)
317
+ traces[1]['Query'].must_equal "select_by_name"
318
+ traces[1]['QueryArgs'].must_equal nil
319
+ traces[1]['IsPreparedStatement'].must_equal "true"
320
+ traces[1].has_key?('Backtrace').must_equal Oboe::Config[:sequel][:collect_backtraces]
321
+ validate_event_keys(traces[2], @exit_kvs)
322
+ end
323
+ end
324
+ end