Skip to content

Commit

Permalink
Merge pull request newrelic#22 from niko/master
Browse files Browse the repository at this point in the history
Added instrumentation for the Picky search engine
  • Loading branch information
samg committed Dec 22, 2011
2 parents 73f8a71 + da63144 commit b9d6cbe
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -100,6 +100,12 @@ No special configuration required for Paperclip visibility.

You can disable it by setting `disable_paperclip` to true in your newrelic.yml file.

### Picky

The gem will instrument the [Picky semantic search engine](http://florianhanke.com/picky/) so it should be visible in transaction traces and the web transactions page.

You can disable it with `disable_picky` in your newrelic.yml file.

### MongoDB

Our instrumentation works on the underlying 'Mongo' library.
Expand Down
41 changes: 41 additions & 0 deletions lib/rpm_contrib/instrumentation/picky.rb
@@ -0,0 +1,41 @@
if defined?(::Picky)

class Picky::NewRelic
def self.obfuscate_tokens tokens
tokens.map { |t|
o = 'xxx'
o += '~' if t.similar?
o += '*' if t.partial?
o = t.qualifiers.sort.join(',') + ':' + o if t.qualifiers && t.qualifiers.respond_to?(:join)
o
}.sort.join(' ')
end
end

end

DependencyDetection.defer do
@name = :picky

depends_on do
defined?(::Picky) && !NewRelic::Control.instance['disable_picky']
end

executes do
NewRelic::Agent.logger.debug 'Installing Picky instrumentation'
end

executes do
::Picky::Search.class_eval do
include NewRelic::Agent::MethodTracer

def execute_with_newrelic_trace *args
metrics = "Custom/Picky/search: #{Picky::NewRelic.obfuscate_tokens args[0]}"
self.class.trace_execution_scoped(metrics){ execute_without_newrelic_trace(*args) }
end

alias_method :execute_without_newrelic_trace, :execute
alias_method :execute, :execute_with_newrelic_trace
end
end
end
1 change: 1 addition & 0 deletions rpm_contrib.gemspec
Expand Up @@ -39,6 +39,7 @@ Gem::Specification.new do |s|
"lib/rpm_contrib/instrumentation/kyototycoon.rb",
"lib/rpm_contrib/instrumentation/mongo.rb",
"lib/rpm_contrib/instrumentation/paperclip.rb",
"lib/rpm_contrib/instrumentation/picky.rb",
"lib/rpm_contrib/instrumentation/redis.rb",
"lib/rpm_contrib/instrumentation/resque.rb",
"lib/rpm_contrib/instrumentation/riak_client.rb",
Expand Down
55 changes: 55 additions & 0 deletions test/test_picky.rb
@@ -0,0 +1,55 @@
require 'picky'

require "#{File.dirname(__FILE__)}/helper"

class NewRelic::Agent::PickyIntrumentationTest < Test::Unit::TestCase

def tokens_for *tokens
tokens.map{|t|
token = 'whatever'

token.extend Module.new{
define_method(:'partial?'){ t[:partial] }
define_method(:'similar?'){ t[:similar] }
define_method(:'qualifiers'){ t[:qualifiers] }
}

token
}
end

def test_obfuscate_tokens
tokens = tokens_for({})
assert_equal 'xxx', Picky::NewRelic.obfuscate_tokens(tokens)

tokens = tokens_for({:similar => true})
assert_equal 'xxx~', Picky::NewRelic.obfuscate_tokens(tokens)

tokens = tokens_for({:partial => true})
assert_equal 'xxx*', Picky::NewRelic.obfuscate_tokens(tokens)

tokens = tokens_for({:qualifiers => [:haha]})
assert_equal 'haha:xxx', Picky::NewRelic.obfuscate_tokens(tokens)

tokens = tokens_for( {:partial => true}, {:similar => true} )
assert_equal 'xxx* xxx~', Picky::NewRelic.obfuscate_tokens(tokens)

tokens = tokens_for( {:similar => true}, {:partial => true} )
assert_equal 'xxx* xxx~', Picky::NewRelic.obfuscate_tokens(tokens)

tokens = tokens_for( {:partial => true}, {:partial => true} )
assert_equal 'xxx* xxx*', Picky::NewRelic.obfuscate_tokens(tokens)

tokens = tokens_for(
{:similar => true, :qualifiers => [:bla]},
{:partial => true, :qualifiers => [:bla, :blub]}
)
assert_equal 'bla,blub:xxx* bla:xxx~', Picky::NewRelic.obfuscate_tokens(tokens)

tokens = tokens_for(
{:similar => true, :qualifiers => [:bla]},
{:partial => true, :qualifiers => [:blub, :bla]}
)
assert_equal 'bla,blub:xxx* bla:xxx~', Picky::NewRelic.obfuscate_tokens(tokens)
end
end

0 comments on commit b9d6cbe

Please sign in to comment.