Skip to content

Commit

Permalink
Merge pull request #95 from theablefew/fix/instrumentation
Browse files Browse the repository at this point in the history
Refactor Stretchy instrumentation
  • Loading branch information
esmarkowski authored Mar 20, 2024
2 parents 26ce013 + fe4c377 commit 7c6e622
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 36 deletions.
29 changes: 0 additions & 29 deletions lib/rails/instrumentation/publishers.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/stretchy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
require 'active_support/all'

require_relative "stretchy/version"
require_relative "rails/instrumentation/railtie" if defined?(Rails)
require_relative "stretchy/rails/instrumentation/railtie" if defined?(Rails)
require_relative 'elasticsearch/api/namespace/machine_learning/model'

module Stretchy
Expand Down
31 changes: 31 additions & 0 deletions lib/stretchy/rails/instrumentation/publishers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Stretchy
module Rails
module Instrumentation
module Publishers

module Record

extend ActiveSupport::Concern

included do
unless method_defined?(:search_without_instrumentation!)
alias_method :search_without_instrumentation!, :search
alias_method :search, :search_with_instrumentation!
end
end

def search_with_instrumentation!(query_or_definition, options={})
ActiveSupport::Notifications.instrument "search.stretchy",
name: "Search",
klass: self.base_class.to_s,
search: {index: self.index_name, body: query_or_definition }.merge(options) do
search_without_instrumentation!(query_or_definition, options)
end
end

end

end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@ module Instrumentation

class Railtie < ::Rails::Railtie

require 'rails/instrumentation/publishers'
Stretchy.instrument!
require_relative 'publishers'

ActiveSupport::Notifications.subscribe 'search.stretchy' do |name, start, finish, id, payload|
message = [
Rainbow(" #{payload[:klass]}").bright,
"(#{(finish.to_time - start.to_time).round(2)}ms)",
Stretchy::Utils.to_curl(payload[:klass].constantize, payload[:search])
Rainbow(" #{payload[:klass]}").bright,
"(#{(finish.to_time - start.to_time).round(2)}ms)",
Stretchy::Utils.to_curl(payload[:klass].constantize, payload[:search])
].join(" ")
::Rails.logger.debug(Rainbow(message).color(:yellow))
end
end


initializer 'stretchy.instrumentation' do
Stretchy.instrument!
end

# initializer 'stretchy.set_defaults' do
# config.elasticsearch_cache_store = :redis_store
# config.elasticsearch_expire_cache_in = 15.minutes
# end

end

end
end

0 comments on commit 7c6e622

Please sign in to comment.