Browse files

Merge pull request #22 from niko/master

Added instrumentation for the Picky search engine
  • Loading branch information...
2 parents 73f8a71 + da63144 commit b9d6cbe024a242c3c87b8d673ee446451e8849c1 @samg samg committed Dec 22, 2011
Showing with 103 additions and 0 deletions.
  1. +6 −0 README.md
  2. +41 −0 lib/rpm_contrib/instrumentation/picky.rb
  3. +1 −0 rpm_contrib.gemspec
  4. +55 −0 test/test_picky.rb
View
6 README.md
@@ -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.
View
41 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
View
1 rpm_contrib.gemspec
@@ -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",
View
55 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.