Class: Appsignal::EventFormatter::MongoRubyDriver::QueryFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/appsignal/event_formatter/mongo_ruby_driver/query_formatter.rb

Constant Summary collapse

ALLOWED =
{
  "find" => {
    "find"   => :allow,
    "filter" => :sanitize_document
  },
  "count" => {
    "count" => :allow,
    "query" => :sanitize_document
  },
  "distinct" => {
    "distinct" => :allow,
    "key"      => :allow,
    "query"    => :sanitize_document
  },
  "insert" => {
    "insert"    => :allow,
    "documents" => :deny_array,
    "ordered"   => :allow
  },
  "update" => {
    "update"  => :allow,
    "updates" => :sanitize_bulk,
    "ordered" => :allow
  },
  "findandmodify" => {
    "findandmodify" => :allow,
    "query"         => :sanitize_document,
    "update"        => :deny_array,
    "new"           => :allow
  },
  "delete" => {
    "delete" => :allow,
    "deletes" => :sanitize_bulk,
    "ordered" => :allow
  },
  "bulk" => {
    "q"      => :sanitize_document,
    "u"      => :deny_array,
    "limit"  => :allow,
    "multi"  => :allow,
    "upsert" => :allow
  }
}

Class Method Summary collapse

Class Method Details

.apply_strategy(strategy, val) ⇒ Object

Applies strategy on hash values based on keys


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/appsignal/event_formatter/mongo_ruby_driver/query_formatter.rb', line 66

def self.apply_strategy(strategy, val)
  case strategy
  when :allow      then val
  when :deny       then '?'
  when :deny_array then '[?]'
  when :sanitize_document
    Appsignal::Utils.sanitize(val, true, :mongodb)
  when :sanitize_bulk
    if val.length > 1
      [
        self.format(:bulk, val.first),
        "[...]"
      ]
    else
      val.map { |v| self.format(:bulk, v) }
    end
  else '?'
  end
end

.format(strategy, command) ⇒ Object

Format command based on given strategy


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/appsignal/event_formatter/mongo_ruby_driver/query_formatter.rb', line 50

def self.format(strategy, command)
  # Stop processing if command is not a hash
  return {} unless command.is_a?(Hash)

  # Get the strategy and stop if it's not present
  strategies = ALLOWED[strategy.to_s]
  return {} unless strategies

  {}.tap do |hsh|
    command.each do |key, val|
      hsh[key] = self.apply_strategy(strategies[key], val)
    end
  end
end