Module: TingYun::Instrumentation::Support::MetricTranslator

Defined in:
lib/ting_yun/instrumentation/support/metric_translator.rb

Constant Summary collapse

MONGODB =
'MongoDB'.freeze

Class Method Summary collapse

Class Method Details

.collection_in_selector?(payload) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/ting_yun/instrumentation/support/metric_translator.rb', line 40

def self.collection_in_selector?(payload)
  payload[:collection] == '$cmd'
end

.collection_name_from_group_selector(payload) ⇒ Object



74
75
76
# File 'lib/ting_yun/instrumentation/support/metric_translator.rb', line 74

def self.collection_name_from_group_selector(payload)
  payload[:selector]["group"]["ns"]
end

.collection_name_from_index(payload) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ting_yun/instrumentation/support/metric_translator.rb', line 56

def self.collection_name_from_index(payload)
  if payload[:documents]
    if payload[:documents].is_a?(Array)
      # mongo gem versions pre 1.10.0
      document = payload[:documents].first
    else
      # mongo gem versions 1.10.0 and later
      document = payload[:documents]
    end

    if document && document[:ns]
      return document[:ns].split('.').last
    end
  end

  'system.indexes'
end

.collection_name_from_rename_selector(payload) ⇒ Object



78
79
80
81
82
# File 'lib/ting_yun/instrumentation/support/metric_translator.rb', line 78

def self.collection_name_from_rename_selector(payload)
  parts = payload[:selector][:renameCollection].split('.')
  parts.shift
  parts.join('.')
end

.create_index?(name, payload) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/ting_yun/instrumentation/support/metric_translator.rb', line 44

def self.create_index?(name, payload)
  name == :insert && payload[:collection] == "system.indexes"
end

.group?(name, payload) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ting_yun/instrumentation/support/metric_translator.rb', line 48

def self.group?(name, payload)
  name == :group
end

.metrics_for(name, payload, host_port) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ting_yun/instrumentation/support/metric_translator.rb', line 13

def self.metrics_for(name, payload, host_port)
  payload ||= {}

  return nil  if collection_in_selector?(payload)

  collection = payload[:collection]

  if create_index?(name, payload)
    collection = self.collection_name_from_index(payload)
  elsif group?(name, payload)
    collection = collection_name_from_group_selector(payload)
  elsif rename_collection?(name, payload)
    collection = collection_name_from_rename_selector(payload)
  end

  TingYun::Agent::Datastore::MetricHelper.metrics_for(MONGODB,
                                                      TingYun::Agent::Datastore::Mongo.transform_operation(name),
                                                      host_port[0],
                                                      host_port[1],
                                                      payload[:database],
                                                      collection)
rescue => e
  TingYun::Agent.logger.debug("Failure during Mongo metric generation", e)
  nil
end

.rename_collection?(name, payload) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/ting_yun/instrumentation/support/metric_translator.rb', line 52

def self.rename_collection?(name, payload)
  name == :renameCollection
end