Skip to content

Commit

Permalink
add support for elasticsearch database
Browse files Browse the repository at this point in the history
  • Loading branch information
prateeksen committed Jan 10, 2024
1 parent 6633f52 commit 7ea2cf5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/newrelic_security/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module NewRelic::Security
SQLITE = 'SQLITE'
MYSQL = 'MYSQL'
POSTGRES = 'POSTGRES'
ES = 'ES'
ISO_8859_1 = 'ISO-8859-1'
UTF_8 = 'UTF-8'
RAILS = 'rails'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module NewRelic::Security
module Instrumentation
module Elasticsearch
module Chain

def self.instrument!
::Elastic::Transport.Client.class_eval do
include NewRelic::Security::Instrumentation::Elasticsearch

alias_method :perform_request_without_security, :perform_request

def perform_request(*args)
retval = nil
event = perform_request_on_enter(*args) { retval = perform_request_without_security(*args) }
perform_request_on_exit(event) { return retval }
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require_relative 'prepend'
require_relative 'chain'

module NewRelic::Security
module Instrumentation
module Elasticsearch

def perform_request_on_enter(*args)
event = nil
NewRelic::Security::Agent.logger.debug "OnEnter : #{self.class}.#{__method__}"
hash = {}
hash[:method] = args[0]
hash[:path] = args[1]
hash[:params] = args[2]
hash[:body] = args[3].to_json
hash[:headers] = args[4]
event = NewRelic::Security::Agent::Control::Collector.collect(NOSQL_DB_COMMAND, [hash], ES) unless NewRelic::Security::Instrumentation::InstrumentationUtils.sql_filter_events?(hash[:sql])
rescue => exception
NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
yield
return event
end

def perform_request_on_exit(event)
NewRelic::Security::Agent.logger.debug "OnExit : #{self.class}.#{__method__}"
NewRelic::Security::Agent::Utils.create_exit_event(event)
rescue => exception
NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
yield
end

end
end
end

NewRelic::Security::Instrumentation::InstrumentationLoader.install_instrumentation(:elasticsearch, ::Elastic::Transport::Client, ::NewRelic::Security::Instrumentation::Elasticsearch)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module NewRelic::Security
module Instrumentation
module Elasticsearch
module Prepend
include NewRelic::Security::Instrumentation::Elasticsearch

def perform_request(*args)
retval = nil
event = perform_request_on_enter(*args) { retval = super }
perform_request_on_exit(event) { return retval }
end

end
end
end
end

0 comments on commit 7ea2cf5

Please sign in to comment.