class OpenTelemetry::Instrumentation::Mongo::CommandSerializer

Serializes a Mongo command object to be added to the trace

Constants

ELLIPSES
MASK_VALUE

Attributes

collection[R]
command[R]
command_name[R]
payload[R]

Public Class Methods

new(command) click to toggle source
# File lib/opentelemetry/instrumentation/mongo/command_serializer.rb, line 17
def initialize(command)
  @command = command
  @command_name, @collection = command.first
  @collection = MASK_VALUE unless @collection.is_a?(String) || @collection.is_a?(Integer)
  @payload = {}
end

Public Instance Methods

serialize() click to toggle source
# File lib/opentelemetry/instrumentation/mongo/command_serializer.rb, line 24
def serialize
  build_payload
  payload.to_json.freeze unless payload.empty?
end

Private Instance Methods

add_map(payload, command, key) click to toggle source
# File lib/opentelemetry/instrumentation/mongo/command_serializer.rb, line 88
def add_map(payload, command, key)
  value = command[key]
  return unless value&.is_a?(Hash) && !value.empty?

  payload[key] = mask(value)
end
add_val(payload, command, key) click to toggle source
# File lib/opentelemetry/instrumentation/mongo/command_serializer.rb, line 81
def add_val(payload, command, key)
  return unless command.key?(key)

  value = command[key]
  payload[key] = value
end
build_command() click to toggle source
# File lib/opentelemetry/instrumentation/mongo/command_serializer.rb, line 38
def build_command # rubocop:disable Metrics/AbcSize
  add_val(payload, command, 'key')
  add_map(payload, command, 'query')
  add_map(payload, command, 'filter')
  add_val(payload, command, 'sort')
  add_val(payload, command, 'new')
  add_map(payload, command, 'update') if command_name == 'findAndModify'
  add_val(payload, command, 'remove')
end
build_deletes() click to toggle source
# File lib/opentelemetry/instrumentation/mongo/command_serializer.rb, line 62
def build_deletes
  deletes = command['deletes']
  return unless deletes

  delete = deletes[0]
  delete_payload = {}
  add_map(delete_payload, delete, 'q')
  add_map(delete_payload, delete, 'limit')
  payload['deletes'] = [delete_payload]
  payload['deletes'] << ELLIPSES if deletes.length > 1
end
build_payload() click to toggle source
# File lib/opentelemetry/instrumentation/mongo/command_serializer.rb, line 31
def build_payload
  build_command
  build_updates
  build_deletes
  build_pipeline
end
build_pipeline() click to toggle source
# File lib/opentelemetry/instrumentation/mongo/command_serializer.rb, line 74
def build_pipeline
  pipeline = command['pipeline']
  return unless pipeline

  payload['pipeline'] = pipeline.map(&method(:mask))
end
build_updates() click to toggle source
# File lib/opentelemetry/instrumentation/mongo/command_serializer.rb, line 48
def build_updates
  updates = command['updates']
  return unless updates

  update = updates[0]
  update_payload = {}
  add_map(update_payload, update, 'q')
  add_map(update_payload, update, 'u')
  add_val(update_payload, update, 'multi')
  add_val(update_payload, update, 'upsert')
  payload['updates'] = [update_payload]
  payload['updates'] << ELLIPSES if updates.length > 1
end
mask(hash) click to toggle source
# File lib/opentelemetry/instrumentation/mongo/command_serializer.rb, line 95
def mask(hash)
  hash.each_with_object({}) do |(k, v), h|
    h[k] = v.is_a?(Hash) ? mask(v) : MASK_VALUE
  end
end