Class: NewRelic::Agent::Transaction::DatastoreSegment

Inherits:
Segment show all
Defined in:
lib/new_relic/agent/transaction/datastore_segment.rb

Constant Summary collapse

UNKNOWN =
NewRelic::UNKNOWN_LOWER

Constants inherited from AbstractSegment

AbstractSegment::CALLBACK, AbstractSegment::INSPECT_IGNORE, AbstractSegment::SEGMENT

Instance Attribute Summary collapse

Attributes inherited from Segment

#custom_transaction_attributes, #unscoped_metrics

Attributes inherited from AbstractSegment

#children_time, #duration, #end_time, #exclusive_duration, #guid, #llm_event, #name, #noticed_error, #parent, #record_on_finish, #record_scoped_metric, #start_time, #starting_segment_key, #thread_id, #transaction, #transaction_name

Instance Method Summary collapse

Methods inherited from Segment

#add_agent_attribute, #add_custom_attributes, #attributes, finish, merge_untrusted_agent_attributes, #merge_untrusted_agent_attributes

Methods inherited from AbstractSegment

#all_code_information_present?, #children_time_ranges?, #code_attributes, #code_information=, #concurrent_children?, #finalize, #finish, #finished?, #inspect, #notice_error, #noticed_error_attributes, #params, #params?, #record_metrics?, #record_on_finish?, #record_scoped_metric?, #set_noticed_error, set_segment_callback, #start, #time_range, #transaction_assigned

Constructor Details

#initialize(product, operation, collection = nil, host = nil, port_path_or_id = nil, database_name = nil, start_time = nil) ⇒ DatastoreSegment

Returns a new instance of DatastoreSegment.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/new_relic/agent/transaction/datastore_segment.rb', line 19

def initialize(product, operation, collection = nil, host = nil, port_path_or_id = nil, database_name = nil, start_time = nil)
  @product = product
  @operation = operation
  @collection = collection
  @sql_statement = nil
  @nosql_statement = nil
  @query_name = nil
  @record_sql = true
  set_instance_info(host, port_path_or_id)
  @database_name = database_name&.to_s
  super(Datastores::MetricHelper.scoped_metric_for(product, operation, collection),
        nil,
        start_time)
end

Instance Attribute Details

#collectionObject (readonly)



16
17
18
# File 'lib/new_relic/agent/transaction/datastore_segment.rb', line 16

def collection
  @collection
end

#database_nameObject



17
18
19
# File 'lib/new_relic/agent/transaction/datastore_segment.rb', line 17

def database_name
  @database_name
end

#hostObject (readonly)



16
17
18
# File 'lib/new_relic/agent/transaction/datastore_segment.rb', line 16

def host
  @host
end

#nosql_statementObject (readonly)



16
17
18
# File 'lib/new_relic/agent/transaction/datastore_segment.rb', line 16

def nosql_statement
  @nosql_statement
end

#operationObject (readonly)



16
17
18
# File 'lib/new_relic/agent/transaction/datastore_segment.rb', line 16

def operation
  @operation
end

#port_path_or_idObject (readonly)



16
17
18
# File 'lib/new_relic/agent/transaction/datastore_segment.rb', line 16

def port_path_or_id
  @port_path_or_id
end

#productObject (readonly)



16
17
18
# File 'lib/new_relic/agent/transaction/datastore_segment.rb', line 16

def product
  @product
end

#query_nameObject (readonly)



16
17
18
# File 'lib/new_relic/agent/transaction/datastore_segment.rb', line 16

def query_name
  @query_name
end

#record_sqlObject



17
18
19
# File 'lib/new_relic/agent/transaction/datastore_segment.rb', line 17

def record_sql
  @record_sql
end

#sql_statementObject (readonly)



16
17
18
# File 'lib/new_relic/agent/transaction/datastore_segment.rb', line 16

def sql_statement
  @sql_statement
end

Instance Method Details

#_notice_sql(sql, config = nil, explainer = nil, binds = nil, name = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



65
66
67
68
69
70
# File 'lib/new_relic/agent/transaction/datastore_segment.rb', line 65

def _notice_sql(sql, config = nil, explainer = nil, binds = nil, name = nil)
  return unless record_sql?

  @sql_statement = Database::Statement.new(sql, config, explainer, binds, name, host, port_path_or_id, database_name)
  @query_name = @sql_statement.query_name
end

#notice_nosql_statement(nosql_statement) ⇒ Object

Note:

THERE ARE SECURITY CONCERNS WHEN CAPTURING STATEMENTS! This method will properly ignore statements when the user has turned off capturing queries, but it is not able to obfuscate arbitrary data! To prevent exposing user information embedded in captured queries, please ensure all data passed to this method is safe to transmit to New Relic.

Method for simplifying attaching non-SQL data statements to a transaction. For instance, Mongo or CQL queries, Memcached or Redis keys would all be appropriate data to attach as statements.

Data passed to this method is NOT obfuscated by New Relic, so please ensure that user information is obfuscated if the agent setting ‘transaction_tracer.record_sql` is set to `obfuscated`

Parameters:

  • nosql_statement (String)

    text of the statement to capture.



89
90
91
92
93
94
# File 'lib/new_relic/agent/transaction/datastore_segment.rb', line 89

def notice_nosql_statement(nosql_statement)
  return unless record_sql?

  @nosql_statement = Database.truncate_query(nosql_statement)
  nil
end

#notice_sql(sql) ⇒ Object



59
60
61
62
# File 'lib/new_relic/agent/transaction/datastore_segment.rb', line 59

def notice_sql(sql)
  _notice_sql(sql)
  nil
end

#record_metricsObject



96
97
98
99
# File 'lib/new_relic/agent/transaction/datastore_segment.rb', line 96

def record_metrics
  @unscoped_metrics = Datastores::MetricHelper.unscoped_metrics_for(product, operation, collection, host, port_path_or_id)
  super
end

#set_instance_info(host = nil, port_path_or_id = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/new_relic/agent/transaction/datastore_segment.rb', line 34

def set_instance_info(host = nil, port_path_or_id = nil)
  port_path_or_id = port_path_or_id.to_s if port_path_or_id
  host_present = host && !host.empty?
  ppi_present = port_path_or_id && !port_path_or_id.empty?

  host = NewRelic::Agent::Hostname.get_external(host) if host_present

  case
  when host_present && ppi_present
    @host = host
    @port_path_or_id = port_path_or_id

  when host_present && !ppi_present
    @host = host
    @port_path_or_id = UNKNOWN

  when !host_present && ppi_present
    @host = UNKNOWN
    @port_path_or_id = port_path_or_id

  else
    @host = @port_path_or_id = nil
  end
end