module NewRelic::Agent::Instrumentation::OpenSearch

Constants

INSTANCE_METHOD_PATTERN
INSTRUMENTATION_NAME
OPERATION
OPERATION_PATTERN
PRODUCT_NAME

Public Instance Methods

perform_request_with_tracing(_method, _path, params = NewRelic::EMPTY_HASH, body = nil, _headers = nil, _opts = nil) { || ... } click to toggle source
# File lib/new_relic/agent/instrumentation/opensearch/instrumentation.rb, line 13
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 Instance Methods

nr_cluster_name() click to toggle source
# File lib/new_relic/agent/instrumentation/opensearch/instrumentation.rb, line 50
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
nr_hosts() click to toggle source
# File lib/new_relic/agent/instrumentation/opensearch/instrumentation.rb, line 62
def nr_hosts
  @nr_hosts ||= (transport.hosts.first || NewRelic::EMPTY_HASH)
end
nr_operation() click to toggle source

See Elasticsearch instrumentation for explanation on Ruby 3.4 changes to match instance method

# File lib/new_relic/agent/instrumentation/opensearch/instrumentation.rb, line 36
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
nr_reported_query(query) click to toggle source
# File lib/new_relic/agent/instrumentation/opensearch/instrumentation.rb, line 43
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