module LogjamAgent

Patch Sinatra's render logic to compute corrected view times.

Constants

LZ4_COMPRESSION
NO_COMPRESSION
SNAPPY_COMPRESSION
VERSION
ZLIB_COMPRESSION

Public Class Methods

add_forwarder(type, *args) click to toggle source
# File lib/logjam_agent.rb, line 303
def self.add_forwarder(type, *args)
  case type
  when :zmq then Forwarders.add(ZMQForwarder.new(*args))
  when :amqp then ArgumentError.new("logjam amqp transport no longer supported")
  else raise ArgumentError.new("unkown logjam transport: '#{type}'")
  end
end
auto_detect_exception(exception_class) click to toggle source
# File lib/logjam_agent.rb, line 206
def self.auto_detect_exception(exception_class)
  # ignore Exception classes created with Class.new (timeout.rb, my old friend)
  if (class_name = exception_class.to_s) =~ /^[\w:]+$/
    exception_classes << class_name unless exception_classes.include?(class_name)
  end
end
auto_detect_logged_exceptions() click to toggle source
# File lib/logjam_agent.rb, line 224
  def self.auto_detect_logged_exceptions
    return if @_exception_auto_detection_initialized
    determine_loaded_exception_classes
    Exception.class_eval <<-"EOS"
      def self.inherited(subclass)
        ::LogjamAgent.auto_detect_exception(subclass)
        ::LogjamAgent.reset_exception_matcher
      end
    EOS
    @_exception_auto_detection_initialized = true
  end
compression_method=(compression_method) click to toggle source
# File lib/logjam_agent.rb, line 130
def self.compression_method=(compression_method)
  case compression_method
  when SNAPPY_COMPRESSION
    begin
      require "snappy"
      @@compression_method = SNAPPY_COMPRESSION
    rescue LoadError
      # do nothing
    end
  when LZ4_COMPRESSION
    begin
      require "ruby-lz4"
      @@compression_method = LZ4_COMPRESSION
    rescue LoadError
      # do nothing
    end
  when NO_COMPRESSION, ZLIB_COMPRESSION
    @@compression_method = compression_method
  else
    raise ArgumentError.new("unknown compression method")
  end
end
decode_payload(data) click to toggle source
# File lib/logjam_agent.rb, line 277
def self.decode_payload(data)
  case compression_method
  when SNAPPY_COMPRESSION
    Snappy.inflate(data)
  when LZ4_COMPRESSION
    uncompressed_size = data[0..3].unpack("N")
    buf = String.new("", capacity: uncompressed_size)
    LZ4::Raw.decompress(data[4..-1], uncompressed_size, dest: buf).first
  when ZLIB_COMPRESSION
    ActiveSupport::Gzip.decompress(data)
  else
    data
  end
end
determine_loaded_exception_classes() click to toggle source
# File lib/logjam_agent.rb, line 217
def self.determine_loaded_exception_classes
  ObjectSpace.each_object(Class) do |klass|
    auto_detect_exception(klass) if klass != Exception && klass.ancestors.include?(Exception)
  end
  reset_exception_matcher
end
disable!() click to toggle source
# File lib/logjam_agent.rb, line 88
def self.disable!
  self.disabled = true
end
enable!() click to toggle source
# File lib/logjam_agent.rb, line 92
def self.enable!
  self.disabled = false
end
encode_payload(data) click to toggle source
# File lib/logjam_agent.rb, line 260
def self.encode_payload(data)
  json = json_encode_payload(data)
  case compression_method
  when SNAPPY_COMPRESSION
    Snappy.deflate(json)
  when LZ4_COMPRESSION
    n = data.byte_size
    max_compressed_size = n + n/256 + 16
    buf = String.new([n].pack("N"), capacity: max_compressed_size + 4)
    LZ4::Raw.compress(json, input_size: n, dest: buf, max_ouput_size: max_compressed_size).first
  when ZLIB_COMPRESSION
    ActiveSupport::Gzip.compress(json)
  else
    json
  end
end
event(label, extra_fields = {}) click to toggle source
# File lib/logjam_agent.rb, line 292
def self.event(label, extra_fields = {})
  fields = {
    :label      => label,
    :started_at => Time.now.iso8601,
    :host       => hostname,
    :uuid       => generate_uuid
  }
  fields.merge!(extra_fields)
  forwarder.forward(fields, :routing_key => events_routing_key, :sync => true)
end
generate_uuid() click to toggle source
# File lib/logjam_agent.rb, line 239
def self.generate_uuid
  UUID4R::uuid(4).gsub('-','')
end
get_hostname() click to toggle source
# File lib/logjam_agent.rb, line 79
def self.get_hostname
  name = Socket.gethostname
  host = name.split('.').first
  Addrinfo.getaddrinfo(host, nil, nil, :STREAM, nil, Socket::AI_CANONNAME).first.canonname rescue name
end
ip_obfuscator(ip) click to toggle source

TODO: ipv6 obfuscation

# File lib/logjam_agent.rb, line 106
def self.ip_obfuscator(ip)
  obfuscate_ips ? ip.to_s.sub(/\d+\z/, 'XXX') : ip
end
json_encode_payload(data) click to toggle source
# File lib/logjam_agent.rb, line 251
def self.json_encode_payload(data)
  Oj.dump(data, :mode => :compat)
end
log_to_log_device?(severity, msg) click to toggle source
# File lib/logjam_agent.rb, line 195
def self.log_to_log_device?(severity, msg)
  return false if severity < log_device_log_level
  if override_global_ignore_lines?
    msg !~ request.log_device_ignored_lines
  else
    !(log_device_ignored_lines && msg =~ log_device_ignored_lines)
  end
rescue
  true
end
max_logged_size_for(key) click to toggle source
# File lib/logjam_agent.rb, line 175
def self.max_logged_size_for(key)
  if key == 'HTTP_COOKIE'.freeze
    max_logged_cookie_size
  else
    max_logged_param_size
  end
end
reset_exception_matcher() click to toggle source
# File lib/logjam_agent.rb, line 213
def self.reset_exception_matcher
  self.exception_matcher = Regexp.new(self.exception_classes.map{|e| Regexp.escape(e)}.join("|"))
end

Private Class Methods

events_routing_key() click to toggle source
# File lib/logjam_agent.rb, line 313
def self.events_routing_key
  "events.#{application_name}.#{environment_name}"
end
forwarder() click to toggle source
# File lib/logjam_agent.rb, line 317
def self.forwarder
  @forwarder ||= Forwarders.get(application_name, environment_name)
end
override_global_ignore_lines?() click to toggle source
# File lib/logjam_agent.rb, line 321
def self.override_global_ignore_lines?
  request && request.log_device_ignored_lines
end