Skip to content

Commit

Permalink
Merge pull request #9 from mostlycached/allow_tag_prefix
Browse files Browse the repository at this point in the history
Allow SqlTagger to be able to set a custom tag prefix
  • Loading branch information
kcen committed Jun 28, 2016
2 parents 99182f2 + 7064d94 commit 8691ca9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
19 changes: 18 additions & 1 deletion lib/sql_tagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class SqlTagger
File.join(File.dirname(__FILE__), '..', 'VERSION')
).chomp.freeze

# @return [String, #call] a string or a proc which resolves to a string
# to prefix the call trace tag
attr_accessor :custom_tag_prefix

# @return [Regexp]
# regular expression used to match stack strings we should skip (usually
# because such stack strings aren't specific enough, like stack strings
Expand All @@ -33,7 +37,7 @@ def tag(sql)
caller(2).each do |string|
next if @exclusion_cache.member?(string)
if string !~ @exclusion_pattern
return "/* #{string} */ #{sql}"
return "/* #{custom_tag_prefix_string} #{string} */ #{sql}"
else
@exclusion_cache.add(string)
end
Expand Down Expand Up @@ -101,4 +105,17 @@ def included(base)
end
end
end

private

# Returns a freshly resolved custom tag prefix string
#
# @return [String] string to prefix the call trace tag
def custom_tag_prefix_string
if @custom_tag_prefix.respond_to?(:call)
@custom_tag_prefix.call
else
@custom_tag_prefix
end
end
end
25 changes: 22 additions & 3 deletions spec/sql_tagger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
end

it 'skips stack strings that match @exclusion_pattern' do
expect(sql_tagger.tag(sql)).to eq("/* #{valid_stack_string} */ #{sql}")
expect(sql_tagger.tag(sql)).to eq("/* #{valid_stack_string} */ #{sql}")
end

it 'returns the 1st stack string that does not match @exclusion_pattern' do
caller_result.push(
'/home/app/myapp/lib/document.rb:788',
'/home/app/myapp/runner.rb:29'
)
expect(sql_tagger.tag(sql)).to eq("/* #{valid_stack_string} */ #{sql}")
expect(sql_tagger.tag(sql)).to eq("/* #{valid_stack_string} */ #{sql}")
end

it 'adds skipped stack strings into @exclusion_cache' do
Expand All @@ -45,7 +45,26 @@
correct_string = '/home/myapp/i.rb:2890'
caller_result.push(correct_string)
sql_tagger.exclusion_cache.add(valid_stack_string)
expect(sql_tagger.tag(sql)).to eq("/* #{correct_string} */ #{sql}")
expect(sql_tagger.tag(sql)).to eq("/* #{correct_string} */ #{sql}")
end

context 'when @custom_tag_prefix is set as a proc' do
it 'prefixes the tag with a freshly resolved @custom_tag_prefix proc' do
string_stack = ['a', 'b']
sql_tagger.custom_tag_prefix = proc { string_stack.pop }
expect(sql_tagger.tag(sql)).
to eq("/* b #{valid_stack_string} */ #{sql}")
expect(sql_tagger.tag(sql)).
to eq("/* a #{valid_stack_string} */ #{sql}")
end
end

context 'when @custom_tag_prefix is set as a string' do
it 'prefixes the tag with the set @custom_tag_prefix' do
sql_tagger.custom_tag_prefix = 'a'
expect(sql_tagger.tag(sql)).
to eq("/* a #{valid_stack_string} */ #{sql}")
end
end
end

Expand Down

0 comments on commit 8691ca9

Please sign in to comment.