Sha256: fbb01f6786228dea25e7e95d0a0bd9022dc1a7919ac7057a4038aa39da2d6ab9

Contents?: true

Size: 1.66 KB

Versions: 17

Compression:

Stored size: 1.66 KB

Contents

module Datadog
  module Contrib
    # MongoDB module includes classes and functions to instrument MongoDB clients
    module MongoDB
      module_function

      # skipped keys are related to command names, since they are already
      # extracted by the query_builder
      SKIP_KEYS = [:_id].freeze
      PLACEHOLDER = '?'.freeze

      # returns a formatted and normalized query
      def query_builder(command_name, database_name, command)
        # always skip the command name
        skip = SKIP_KEYS | [command_name.to_s]

        result = {
          operation: command_name,
          database: database_name,
          collection: command.values.first
        }

        command.each do |key, value|
          result[key] = quantize_statement(value, skip) unless skip.include?(key)
        end

        result
      end

      # removes the values from the given query; this quantization recursively
      # replace elements available in a given query, so that Arrays, Hashes and so
      # on are compacted. It ensures a low cardinality so that it can be used
      # as a Span resource.
      def quantize_statement(statement, skip = [])
        case statement
        when Hash
          statement.each_with_object({}) do |(key, value), quantized|
            quantized[key] = quantize_value(value, skip) unless skip.include?(key)
          end
        else
          quantize_value(statement, skip)
        end
      end

      def quantize_value(value, skip = [])
        case value
        when Hash
          quantize_statement(value, skip)
        when Array
          quantize_value(value.first, skip)
        else
          PLACEHOLDER
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
ddtrace-0.12.1 lib/ddtrace/contrib/mongodb/parsers.rb
ddtrace-0.13.0.beta1 lib/ddtrace/contrib/mongodb/parsers.rb
ddtrace-0.12.0 lib/ddtrace/contrib/mongodb/parsers.rb
ddtrace-0.12.0.rc1 lib/ddtrace/contrib/mongodb/parsers.rb
ddtrace-0.11.4 lib/ddtrace/contrib/mongodb/parsers.rb
ddtrace-0.11.3 lib/ddtrace/contrib/mongodb/parsers.rb
ddtrace-0.12.0.beta2 lib/ddtrace/contrib/mongodb/parsers.rb
ddtrace-0.12.0.beta1 lib/ddtrace/contrib/mongodb/parsers.rb
ddtrace-0.11.2 lib/ddtrace/contrib/mongodb/parsers.rb
ddtrace-0.11.1 lib/ddtrace/contrib/mongodb/parsers.rb
ddtrace-0.11.0 lib/ddtrace/contrib/mongodb/parsers.rb
ddtrace-0.11.0.beta2 lib/ddtrace/contrib/mongodb/parsers.rb
ddtrace-0.11.0.beta1 lib/ddtrace/contrib/mongodb/parsers.rb
ddtrace-0.10.0 lib/ddtrace/contrib/mongodb/parsers.rb
ddtrace-0.9.2 lib/ddtrace/contrib/mongodb/parsers.rb
ddtrace-0.9.1 lib/ddtrace/contrib/mongodb/parsers.rb
ddtrace-0.9.0 lib/ddtrace/contrib/mongodb/parsers.rb