marginalia 1.7.0 → 1.10.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: da6b70d12c22d141f4283d2e99b5550afaf8da14
4
- data.tar.gz: fb2a34d6aee5b051fc9611ba792f4c14a50b2e9b
2
+ SHA256:
3
+ metadata.gz: 8dba7f58a767008578f36af3a2bedc77139f11b62025e1dcd022c1ebd3658c3c
4
+ data.tar.gz: 9d4728cbc7b673514cce7076a60c28592646fbc68ac59d8310c7ebe3aa71eac6
5
5
  SHA512:
6
- metadata.gz: af483022b150c72a218aa51dadb6db0865272cd81faa53db211b1b08e81a0b18b560908e92f553e93eb8b0d47029bd927f6fe6e61c811ab11e9d4b63339bed9d
7
- data.tar.gz: e688057959cfd654dc027f03b1a8720ad21c90113d5478d18ed6897d5e4ab0a21297e703cfca85275e5d4495bce0e354acd9612f97f395cad6b1c70cdb39c8fa
6
+ metadata.gz: 35dfdc97e0745484719266a9c616c257072dfbdf0ee8ff6b4c7055d051510a562bf12d863eae670c382f14eda7ba895f4a988baa031ea43904fd9a2a31fd7c0e
7
+ data.tar.gz: 3fbfdeacc57e3a8e78490e991527610a3249c0b3e748de7410a427e9e085e6b420a12d916b0c15d87a7240743a81e9be14727508743d25d7cc82d850924ad126
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.2.3
1
+ 2.6.6
data/.travis.yml CHANGED
@@ -1,10 +1,21 @@
1
1
  language: ruby
2
2
  sudo: false
3
3
 
4
+ services:
5
+ - mysql
6
+ - postgresql
7
+
4
8
  rvm:
5
9
  - 2.2
6
10
  - 2.3
7
11
  - 2.4
12
+ - 2.5
13
+ - 2.6
14
+ - 2.7
15
+
16
+ services:
17
+ - mysql
18
+ - postgresql
8
19
 
9
20
  script: "bundle exec rake db:reset test:all"
10
21
 
@@ -14,3 +25,10 @@ gemfile:
14
25
  - gemfiles/5.0.gemfile
15
26
  - gemfiles/5.1.gemfile
16
27
  - gemfiles/5.2.gemfile
28
+
29
+ matrix:
30
+ exclude:
31
+ - rvm: 2.7
32
+ gemfile: gemfiles/4.2.gemfile
33
+ - rvm: 2.7
34
+ gemfile: gemfiles/4.2.api.gemfile
data/README.md CHANGED
@@ -56,6 +56,8 @@ Optionally, you can set the application name shown in the log like so in an init
56
56
  For Rails 3 applications, the name will default to your Rails application name.
57
57
  For Rails 2 applications, "rails" is used as the default application name.
58
58
 
59
+ #### Components
60
+
59
61
  You can also configure the components of the comment that will be appended,
60
62
  by setting `Marginalia::Comment.components`. By default, this is set to:
61
63
 
@@ -100,7 +102,17 @@ With ActiveRecord >= 3.2.19:
100
102
 
101
103
  Pull requests for other included comment components are welcome.
102
104
 
103
- ## Inline query annotations
105
+ #### Prepend comments
106
+
107
+ By default marginalia appends the comments at the end of the query. Certain databases, such as MySQL will truncate
108
+ the query text. This is the case for slow query logs and the results of querying some InnoDB internal tables where the
109
+ length of the query is more than 1024 bytes.
110
+
111
+ In order to not lose the marginalia comments from your logs, you can prepend the comments using this option:
112
+
113
+ Marginalia::Comment.prepend_comment = true
114
+
115
+ #### Inline query annotations
104
116
 
105
117
  In addition to the request or job-level component-based annotations,
106
118
  Marginalia may be used to add inline annotations to specific queries using a
data/lib/marginalia.rb CHANGED
@@ -49,41 +49,61 @@ module Marginalia
49
49
  Marginalia::Comment.update_adapter!(self)
50
50
  comment = Marginalia::Comment.construct_comment
51
51
  if comment.present? && !sql.include?(comment)
52
- sql = "#{sql} /*#{comment}*/"
52
+ sql = if Marginalia::Comment.prepend_comment
53
+ "/*#{comment}*/ #{sql}"
54
+ else
55
+ "#{sql} /*#{comment}*/"
56
+ end
53
57
  end
54
58
  inline_comment = Marginalia::Comment.construct_inline_comment
55
- if inline_comment.present?
56
- sql = "#{sql} /*#{inline_comment}*/"
59
+ if inline_comment.present? && !sql.include?(inline_comment)
60
+ sql = if Marginalia::Comment.prepend_comment
61
+ "/*#{inline_comment}*/ #{sql}"
62
+ else
63
+ "#{sql} /*#{inline_comment}*/"
64
+ end
57
65
  end
66
+
58
67
  sql
59
68
  end
60
69
 
61
- def execute_with_marginalia(sql, name = nil)
62
- execute_without_marginalia(annotate_sql(sql), name)
70
+ def execute_with_marginalia(sql, *args)
71
+ execute_without_marginalia(annotate_sql(sql), *args)
63
72
  end
73
+ ruby2_keywords :execute_with_marginalia if respond_to?(:ruby2_keywords, true)
64
74
 
65
- def exec_query_with_marginalia(sql, name = 'SQL', binds = [])
66
- exec_query_without_marginalia(annotate_sql(sql), name, binds)
75
+ def exec_query_with_marginalia(sql, *args)
76
+ exec_query_without_marginalia(annotate_sql(sql), *args)
67
77
  end
78
+ ruby2_keywords :exec_query_with_marginalia if respond_to?(:ruby2_keywords, true)
68
79
 
69
80
  if ActiveRecord::VERSION::MAJOR >= 5
70
- def exec_query_with_marginalia(sql, name = 'SQL', binds = [], options = {})
81
+ def exec_query_with_marginalia(sql, *args, **options)
71
82
  options[:prepare] ||= false
72
- exec_query_without_marginalia(annotate_sql(sql), name, binds, options)
83
+ exec_query_without_marginalia(annotate_sql(sql), *args, **options)
73
84
  end
74
85
  end
75
86
 
76
- def exec_delete_with_marginalia(sql, name = 'SQL', binds = [])
77
- exec_delete_without_marginalia(annotate_sql(sql), name, binds)
87
+ def exec_delete_with_marginalia(sql, *args)
88
+ exec_delete_without_marginalia(annotate_sql(sql), *args)
78
89
  end
90
+ ruby2_keywords :exec_delete_with_marginalia if respond_to?(:ruby2_keywords, true)
79
91
 
80
- def exec_update_with_marginalia(sql, name = 'SQL', binds = [])
81
- exec_update_without_marginalia(annotate_sql(sql), name, binds)
92
+ def exec_update_with_marginalia(sql, *args)
93
+ exec_update_without_marginalia(annotate_sql(sql), *args)
82
94
  end
95
+ ruby2_keywords :exec_update_with_marginalia if respond_to?(:ruby2_keywords, true)
83
96
 
97
+ if ActiveRecord::VERSION::MAJOR >= 5
84
98
  def execute_and_clear_with_marginalia(sql, *args, &block)
85
99
  execute_and_clear_without_marginalia(annotate_sql(sql), *args, &block)
86
100
  end
101
+ ruby2_keywords :execute_and_clear_with_marginalia if respond_to?(:ruby2_keywords, true)
102
+ else
103
+ def execute_and_clear_with_marginalia(sql, *args, &block)
104
+ execute_and_clear_without_marginalia(annotate_sql(sql), *args, &block)
105
+ end
106
+ end
87
107
  end
88
108
 
89
109
  module ActionControllerInstrumentation
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'socket'
2
4
 
3
5
  module Marginalia
4
6
  module Comment
5
- mattr_accessor :components, :lines_to_ignore
7
+ mattr_accessor :components, :lines_to_ignore, :prepend_comment
6
8
  Marginalia::Comment.components ||= [:application, :controller, :action]
7
9
 
8
10
  def self.update!(controller = nil)
@@ -18,11 +20,11 @@ module Marginalia
18
20
  end
19
21
 
20
22
  def self.construct_comment
21
- ret = ''
23
+ ret = String.new
22
24
  self.components.each do |c|
23
25
  component_value = self.send(c)
24
26
  if component_value.present?
25
- ret << "#{c.to_s}:#{component_value.to_s},"
27
+ ret << "#{c}:#{component_value},"
26
28
  end
27
29
  end
28
30
  ret.chop!
@@ -102,11 +104,14 @@ module Marginalia
102
104
  end
103
105
 
104
106
  def self.sidekiq_job
105
- marginalia_job["class"] if marginalia_job
107
+ marginalia_job["class"] if marginalia_job && marginalia_job.respond_to?(:[])
106
108
  end
107
109
 
110
+ DEFAULT_LINES_TO_IGNORE_REGEX = %r{\.rvm|/ruby/gems/|vendor/|marginalia|rbenv|monitor\.rb.*mon_synchronize}
111
+
108
112
  def self.line
109
- Marginalia::Comment.lines_to_ignore ||= /\.rvm|gem|vendor\/|marginalia|rbenv/
113
+ Marginalia::Comment.lines_to_ignore ||= DEFAULT_LINES_TO_IGNORE_REGEX
114
+
110
115
  last_line = caller.detect do |line|
111
116
  line !~ Marginalia::Comment.lines_to_ignore
112
117
  end
data/marginalia.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.authors = ["Noah Lorang", "Nick Quaranto", "Taylor Weibley"]
3
- gem.email = ["noah@37signals.com", "arthurnn@github.com"]
3
+ gem.email = ["arthurnn@github.com"]
4
4
  gem.homepage = "https://github.com/basecamp/marginalia"
5
5
 
6
6
  gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
@@ -8,7 +8,7 @@ Gem::Specification.new do |gem|
8
8
  gem.test_files = `git ls-files -- {test}/*`.split("\n")
9
9
  gem.name = "marginalia"
10
10
  gem.require_paths = ["lib"]
11
- gem.version = "1.7.0"
11
+ gem.version = "1.10.1"
12
12
  gem.license = "MIT"
13
13
 
14
14
  gem.add_runtime_dependency "actionpack", ">= 2.3"
@@ -121,6 +121,7 @@ class MarginaliaTest < MiniTest::Test
121
121
  @queries << args.last[:sql]
122
122
  end
123
123
  @env = Rack::MockRequest.env_for('/')
124
+ ActiveJob::Base.queue_adapter = :inline
124
125
  end
125
126
 
126
127
  def test_double_annotate
@@ -215,6 +216,16 @@ class MarginaliaTest < MiniTest::Test
215
216
  assert_match %r{/\*line:.*lib/marginalia/comment.rb:[0-9]+}, @queries.first
216
217
  end
217
218
 
219
+ def test_default_lines_to_ignore_regex
220
+ line = "/gems/a_gem/lib/a_gem.rb:1:in `some_method'"
221
+ call_stack = [line] + caller
222
+
223
+ assert_match(
224
+ call_stack.detect { |line| line !~ Marginalia::Comment::DEFAULT_LINES_TO_IGNORE_REGEX },
225
+ line
226
+ )
227
+ end
228
+
218
229
  def test_hostname_and_pid
219
230
  Marginalia::Comment.components = [:hostname, :pid]
220
231
  PostsController.action(:driver_only).call(@env)
@@ -282,6 +293,15 @@ class MarginaliaTest < MiniTest::Test
282
293
  Post.first
283
294
  refute_match %{job:PostsJob}, @queries.last
284
295
  end
296
+
297
+ def test_active_job_with_sidekiq
298
+ Marginalia::Comment.components = [:job, :sidekiq_job]
299
+ PostsJob.perform_later
300
+ assert_match %{job:PostsJob}, @queries.first
301
+
302
+ Post.first
303
+ refute_match %{job:PostsJob}, @queries.last
304
+ end
285
305
  end
286
306
 
287
307
  def test_sidekiq_job
@@ -290,13 +310,14 @@ class MarginaliaTest < MiniTest::Test
290
310
 
291
311
  # Test harness does not run Sidekiq middlewares by default so include testing middleware.
292
312
  Sidekiq::Testing.server_middleware do |chain|
293
- chain.add Marginalia::SidekiqInstrumentation::Middleware
313
+ chain.add Marginalia::SidekiqInstrumentation::Middleware
294
314
  end
295
315
 
296
316
  Sidekiq::Testing.fake!
297
317
  PostsSidekiqJob.perform_async
298
318
  PostsSidekiqJob.drain
299
319
  assert_match %{sidekiq_job:PostsSidekiqJob}, @queries.first
320
+
300
321
  Post.first
301
322
  refute_match %{sidekiq_job:PostsSidekiqJob}, @queries.last
302
323
  end
@@ -341,6 +362,22 @@ class MarginaliaTest < MiniTest::Test
341
362
  assert_match %r{/\*; DROP TABLE USERS;\*/$}, @queries.last
342
363
  end
343
364
 
365
+ def test_inline_annotations_are_deduped
366
+ Marginalia.with_annotation("foo") do
367
+ ActiveRecord::Base.connection.execute "select id from posts /*foo*/"
368
+ end
369
+ assert_match %r{select id from posts /\*foo\*/ /\*application:rails\*/$}, @queries.first
370
+ end
371
+
372
+ def test_add_comments_to_beginning_of_query
373
+ Marginalia::Comment.prepend_comment = true
374
+
375
+ ActiveRecord::Base.connection.execute "select id from posts"
376
+ assert_match %r{/\*application:rails\*/ select id from posts$}, @queries.first
377
+ ensure
378
+ Marginalia::Comment.prepend_comment = nil
379
+ end
380
+
344
381
  def teardown
345
382
  Marginalia.application_name = nil
346
383
  Marginalia::Comment.lines_to_ignore = nil
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: marginalia
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.0
4
+ version: 1.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Noah Lorang
8
8
  - Nick Quaranto
9
9
  - Taylor Weibley
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-03-15 00:00:00.000000000 Z
13
+ date: 2021-02-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: actionpack
@@ -140,7 +140,6 @@ dependencies:
140
140
  version: '0'
141
141
  description: Attach comments to your ActiveRecord queries.
142
142
  email:
143
- - noah@37signals.com
144
143
  - arthurnn@github.com
145
144
  executables: []
146
145
  extensions: []
@@ -169,7 +168,7 @@ homepage: https://github.com/basecamp/marginalia
169
168
  licenses:
170
169
  - MIT
171
170
  metadata: {}
172
- post_install_message:
171
+ post_install_message:
173
172
  rdoc_options: []
174
173
  require_paths:
175
174
  - lib
@@ -184,9 +183,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
184
183
  - !ruby/object:Gem::Version
185
184
  version: '0'
186
185
  requirements: []
187
- rubyforge_project:
188
- rubygems_version: 2.4.5.1
189
- signing_key:
186
+ rubygems_version: 3.0.3
187
+ signing_key:
190
188
  specification_version: 4
191
189
  summary: Attach comments to your ActiveRecord queries.
192
190
  test_files: []