Class: TingYun::Support::Serialize::JsonMarshaller

Inherits:
Marshaller
  • Object
show all
Defined in:
lib/ting_yun/support/serialize/json_marshaller.rb

Overview

Marshal collector protocol with JSON when available

Constant Summary collapse

OK_YAJL_VERSION =
TingYun::Support::VersionNumber.new("1.2.1")

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Marshaller

#default_encoder, #prepare, #return_value_for_testing

Constructor Details

#initializeJsonMarshaller

Returns a new instance of JsonMarshaller.



14
15
16
17
18
19
20
# File 'lib/ting_yun/support/serialize/json_marshaller.rb', line 14

def initialize
  TingYun::Agent.logger.debug "Using JSON marshaller (#{JSONWrapper.backend_name})"
  unless self.class.is_supported?
    TingYun::Agent.logger.warn "The JSON marshaller in use (#{JSONWrapper.backend_name}) is not recommended. Ensure the 'json' gem is available in your application for better performance."
  end
  warn_for_yajl
end

Class Method Details

.human_readable?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/ting_yun/support/serialize/json_marshaller.rb', line 67

def self.human_readable?
  true # for some definitions of 'human'
end

.is_supported?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/ting_yun/support/serialize/json_marshaller.rb', line 63

def self.is_supported?
  JSONWrapper.usable_for_collector_serialization?
end

Instance Method Details

#dump(ruby, opts = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ting_yun/support/serialize/json_marshaller.rb', line 35

def dump(ruby, opts={})
  prepared = prepare(ruby, opts)

  if opts[:skip_normalization]
    normalize_encodings = false
  else
    normalize_encodings = TingYun::Agent.config[:normalize_json_string_encodings]
  end

  JSONWrapper.dump(prepared, :normalize => normalize_encodings)
end

#formatObject



59
60
61
# File 'lib/ting_yun/support/serialize/json_marshaller.rb', line 59

def format
  'json'
end

#load(data) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ting_yun/support/serialize/json_marshaller.rb', line 47

def load(data)
  if data.nil? || data.empty?
    ::TingYun::Agent.logger.error "Empty JSON response from collector: '#{data.inspect}'"
    return nil
  end

  return_value(JSONWrapper.load(data))
rescue => e
  ::TingYun::Agent.logger.debug "#{e.class.name} : #{e.message} encountered loading collector response: #{data}"
  raise
end

#warn_for_yajlObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/ting_yun/support/serialize/json_marshaller.rb', line 24

def warn_for_yajl
  if defined?(::Yajl)
    require 'yajl/version'
    if VersionNumber.new(::Yajl::VERSION) < OK_YAJL_VERSION
      ::TingYun::Agent.logger.warn "Detected yajl-ruby version #{::Yajl::VERSION} which can cause segfaults with TingYun_rpm's thread profiling features. We strongly recommend you upgrade to the latest yajl-ruby version available."
    end
  end
rescue => err
  ::TingYun::Agent.logger.warn "Failed trying to watch for problematic yajl-ruby version.", err
end