Module: NewRelic::Agent::Instrumentation::Memcache::Tracer

Defined in:
lib/new_relic/agent/instrumentation/memcache/instrumentation.rb

Constant Summary collapse

SLASH =
'/'
UNKNOWN =
'unknown'
LOCALHOST =
'localhost'
MULTIGET_METRIC_NAME =
'get_multi_request'
MEMCACHED =
'Memcached'

Instance Method Summary collapse

Instance Method Details

#assign_instance_to(segment, server) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/new_relic/agent/instrumentation/memcache/instrumentation.rb', line 74

def assign_instance_to(segment, server)
  host = port_path_or_id = nil
  if server.hostname.start_with?(SLASH)
    host = LOCALHOST
    port_path_or_id = server.hostname
  else
    host = server.hostname
    port_path_or_id = server.port
  end
  segment.set_instance_info(host, port_path_or_id)
rescue => e
  ::NewRelic::Agent.logger.debug("Failed to retrieve memcached instance info: #{e.message}")
  segment.set_instance_info(UNKNOWN, UNKNOWN)
end

#get_multi_with_newrelic_tracing(method_name) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/new_relic/agent/instrumentation/memcache/instrumentation.rb', line 45

def get_multi_with_newrelic_tracing(method_name)
  segment = NewRelic::Agent::Tracer.start_segment(
    name: "Ruby/Memcached/Dalli/#{method_name}"
  )

  begin
    NewRelic::Agent::Tracer.capture_segment_error(segment) { yield }
  ensure
    ::NewRelic::Agent::Transaction::Segment.finish(segment)
  end
end

#send_multiget_with_newrelic_tracing(keys) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/new_relic/agent/instrumentation/memcache/instrumentation.rb', line 57

def send_multiget_with_newrelic_tracing(keys)
  segment = ::NewRelic::Agent::Tracer.start_datastore_segment(
    product: MEMCACHED,
    operation: MULTIGET_METRIC_NAME
  )

  begin
    assign_instance_to(segment, self)
    NewRelic::Agent::Tracer.capture_segment_error(segment) { yield }
  ensure
    if ::NewRelic::Agent.config[:capture_memcache_keys]
      segment.notice_nosql_statement("#{MULTIGET_METRIC_NAME} #{keys.inspect}")
    end
    ::NewRelic::Agent::Transaction::Segment.finish(segment)
  end
end

#server_for_key_with_newrelic_tracingObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/new_relic/agent/instrumentation/memcache/instrumentation.rb', line 30

def server_for_key_with_newrelic_tracing
  yield.tap do |server|
    begin
      if txn = ::NewRelic::Agent::Tracer.current_transaction
        segment = txn.current_segment
        if ::NewRelic::Agent::Transaction::DatastoreSegment === segment
          assign_instance_to(segment, server)
        end
      end
    rescue => e
      ::NewRelic::Agent.logger.warn("Unable to set instance info on datastore segment: #{e.message}")
    end
  end
end

#with_newrelic_tracing(operation, *args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/new_relic/agent/instrumentation/memcache/instrumentation.rb', line 14

def with_newrelic_tracing(operation, *args)
  segment = NewRelic::Agent::Tracer.start_datastore_segment(
    product: MEMCACHED,
    operation: operation
  )

  begin
    NewRelic::Agent::Tracer.capture_segment_error(segment) { yield }
  ensure
    if NewRelic::Agent.config[:capture_memcache_keys]
      segment.notice_nosql_statement("#{operation} #{args.first.inspect}")
    end
    ::NewRelic::Agent::Transaction::Segment.finish(segment)
  end
end