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



37
38
39
# File 'lib/ting_yun/instrumentation/support/metric_translator.rb', line 37

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

.collection_name_from_group_selector(payload) ⇒ Object



71
72
73
# File 'lib/ting_yun/instrumentation/support/metric_translator.rb', line 71

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

.collection_name_from_index(payload) ⇒ Object



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

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



75
76
77
78
79
# File 'lib/ting_yun/instrumentation/support/metric_translator.rb', line 75

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

.create_index?(name, payload) ⇒ Boolean



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

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

.group?(name, payload) ⇒ Boolean



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

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

.metrics_for(name, payload) ⇒ Object



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

def self.metrics_for(name, payload)
  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),
                                                      collection)
rescue => e
  TingYun::Agent.logger.debug("Failure during Mongo metric generation", e)
  nil
end

.rename_collection?(name, payload) ⇒ Boolean



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

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