Module: Elastics::Instrumentation

Defined in:
lib/elastics/instrumentation.rb,
lib/elastics/instrumentation/active_support.rb

Defined Under Namespace

Modules: ActiveSupport

Constant Summary collapse

PRETTIFIERS =
{
  ap:     ->(str) { prettify_json(str, &:awesome_inspect) },
  pp:     ->(str) { prettify_json(str, &:pretty_inspect) },
  true => ->(str) { prettify_json(str, &JSON.method(:pretty_generate)) },
}

Class Method Summary collapse

Class Method Details

.body_prettifier=(value) ⇒ Object



12
13
14
15
16
17
# File 'lib/elastics/instrumentation.rb', line 12

def body_prettifier=(value)
  @body_prettifier = case value
  when Proc, nil, false then value
  else PRETTIFIERS[value] or raise 'Invalid prettifier'
  end
end

.prettify_body(str) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/elastics/instrumentation.rb', line 19

def prettify_body(str)
  if @body_prettifier
    @body_prettifier.call(str)
  else
    str
  end
end

.prettify_json(str, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/elastics/instrumentation.rb', line 27

def prettify_json(str, &block)
  data = [JSON.parse(str)] rescue nil
  data ||= str.split("\n").map { |x| JSON.parse(x) } rescue nil
  if data
    data.map(&block).join("\n")
  else
    str
  end
end