-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2796 from newrelic/opensearch-instrumentation
OpenSearch instrumentation
- Loading branch information
Showing
10 changed files
with
413 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
require_relative 'opensearch/instrumentation' | ||
require_relative 'opensearch/chain' | ||
require_relative 'opensearch/prepend' | ||
|
||
DependencyDetection.defer do | ||
named :opensearch | ||
|
||
depends_on do | ||
defined?(OpenSearch) | ||
end | ||
|
||
executes do | ||
NewRelic::Agent.logger.info('Installing opensearch-ruby instrumentation') | ||
|
||
if use_prepend? | ||
prepend_instrument OpenSearch::Transport::Client, NewRelic::Agent::Instrumentation::OpenSearch::Prepend | ||
else | ||
chain_instrument NewRelic::Agent::Instrumentation::OpenSearch::Chain | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
module NewRelic::Agent::Instrumentation | ||
module OpenSearch::Chain | ||
def self.instrument! | ||
::OpenSearch::Transport::Client.class_eval do | ||
include NewRelic::Agent::Instrumentation::OpenSearch | ||
|
||
alias_method(:perform_request_without_tracing, :perform_request) | ||
|
||
def perform_request(*args) | ||
perform_request_with_tracing(*args) do | ||
perform_request_without_tracing(*args) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
66 changes: 66 additions & 0 deletions
66
lib/new_relic/agent/instrumentation/opensearch/instrumentation.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
module NewRelic::Agent::Instrumentation | ||
module OpenSearch | ||
PRODUCT_NAME = 'OpenSearch' | ||
OPERATION = 'perform_request' | ||
OPERATION_PATTERN = %r{/lib/opensearch/api/(?!.+#{OPERATION})} | ||
INSTANCE_METHOD_PATTERN = /:in (?:`|')(?:.+#)?([^']+)'\z/ | ||
INSTRUMENTATION_NAME = NewRelic::Agent.base_name(name) | ||
|
||
def perform_request_with_tracing(_method, _path, params = NewRelic::EMPTY_HASH, body = nil, _headers = nil, _opts = nil, &_block) | ||
return yield unless NewRelic::Agent::Tracer.tracing_enabled? | ||
|
||
segment = NewRelic::Agent::Tracer.start_datastore_segment( | ||
product: PRODUCT_NAME, | ||
operation: nr_operation || OPERATION, | ||
host: nr_hosts[:host], | ||
port_path_or_id: nr_hosts[:port], | ||
database_name: nr_cluster_name | ||
) | ||
begin | ||
NewRelic::Agent::Tracer.capture_segment_error(segment) { yield } | ||
ensure | ||
if segment | ||
segment.notice_nosql_statement(nr_reported_query(body || params)) | ||
segment.finish | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
# See Elasticsearch instrumentation for explanation on Ruby 3.4 changes to match instance method | ||
def nr_operation | ||
location = caller_locations.detect { |loc| loc.to_s.match?(OPERATION_PATTERN) } | ||
return unless location && location.to_s =~ INSTANCE_METHOD_PATTERN | ||
|
||
Regexp.last_match(1) | ||
end | ||
|
||
def nr_reported_query(query) | ||
return unless NewRelic::Agent.config[:'opensearch.capture_queries'] | ||
return query unless NewRelic::Agent.config[:'opensearch.obfuscate_queries'] | ||
|
||
NewRelic::Agent::Datastores::NosqlObfuscator.obfuscate_statement(query) | ||
end | ||
|
||
def nr_cluster_name | ||
return @nr_cluster_name if defined?(@nr_cluster_name) | ||
return if nr_hosts.empty? | ||
|
||
NewRelic::Agent.disable_all_tracing do | ||
@nr_cluster_name ||= perform_request('GET', '/').body['cluster_name'] | ||
end | ||
rescue StandardError => e | ||
NewRelic::Agent.logger.error('Failed to get cluster name for OpenSearch', e) | ||
nil | ||
end | ||
|
||
def nr_hosts | ||
@nr_hosts ||= (transport.hosts.first || NewRelic::EMPTY_HASH) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
module NewRelic::Agent::Instrumentation | ||
module OpenSearch::Prepend | ||
include NewRelic::Agent::Instrumentation::OpenSearch | ||
|
||
def perform_request(*args) | ||
perform_request_with_tracing(*args) { super } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
suite_condition('Skip OpenSearch on GitHub Actions, service not currently available') do | ||
ENV['CI'] != 'true' | ||
end | ||
|
||
instrumentation_methods :chain, :prepend | ||
|
||
OPENSEARCH_VERSIONS = [ | ||
[nil, 2.5], | ||
['3.4.0', 2.5], | ||
['2.1.0', 2.4] | ||
] | ||
|
||
def gem_list(opensearch_version = nil) | ||
<<~RB | ||
gem 'opensearch-ruby'#{opensearch_version} | ||
RB | ||
end | ||
|
||
create_gemfiles(OPENSEARCH_VERSIONS) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
development: | ||
error_collector: | ||
enabled: true | ||
apdex_t: 0.5 | ||
monitor_mode: true | ||
license_key: bootstrap_newrelic_admin_license_key_000 | ||
instrumentation: | ||
opensearch: <%= $instrumentation_method %> | ||
app_name: test | ||
log_level: debug | ||
host: 127.0.0.1 | ||
api_host: 127.0.0.1 | ||
transaction_trace: | ||
record_sql: obfuscated | ||
enabled: true | ||
stack_trace_threshold: 0.5 | ||
transaction_threshold: 1.0 | ||
capture_params: false |
Oops, something went wrong.