Module: LogjamAgent

Extended by:
Obfuscation, RequestHandling, SelectiveLogging
Defined in:
lib/logjam_agent/sinatra.rb,
lib/logjam_agent/util.rb,
lib/logjam_agent/railtie.rb,
lib/logjam_agent/request.rb,
lib/logjam_agent/sinatra.rb,
lib/logjam_agent/version.rb,
lib/logjam_agent/receiver.rb,
lib/logjam_agent/forwarders.rb,
lib/logjam_agent/middleware.rb,
lib/logjam_agent/obfuscation.rb,
lib/logjam_agent/rack/logger.rb,
lib/logjam_agent/zmq_forwarder.rb,
lib/logjam_agent/request_handling.rb,
lib/logjam_agent/selective_logging.rb,
lib/logjam_agent/logging_attributes.rb,
lib/logjam_agent/syslog_like_formatter.rb,
lib/logjam_agent/actionpack/lib/action_dispatch/middleware/remote_ip.rb,
lib/logjam_agent/buffered_logger.rb,
lib/logjam_agent.rb

Overview

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

Defined Under Namespace

Modules: ActionDispatch, ComputeRenderTimes, Forwarders, LoggingAttributes, Obfuscation, Rack, RequestHandling, SelectiveLogging, Sinatra, Util Classes: BufferedLogger, CallerTimeoutExceeded, ConsoleFormatter, ForwardingError, ForwardingWarning, Middleware, NegativeWaitTime, Railtie, Receiver, Request, SyslogLikeFormatter, ZMQForwarder

Constant Summary collapse

VERSION =
"0.39.1"
NO_COMPRESSION =
0
ZLIB_COMPRESSION =
1
SNAPPY_COMPRESSION =
2
LZ4_COMPRESSION =
3

Constants included from Obfuscation

Obfuscation::KEY_RE, Obfuscation::PAIR_RE, Obfuscation::VAL_RE

Class Method Summary collapse

Methods included from Obfuscation

cookie_obfuscator, filter_pairs, ip_obfuscator, obfuscate_cookie

Methods included from RequestHandling

finish_request, request, request=, start_request

Methods included from SelectiveLogging

logdevice_only, logdevice_only?, logjam_log_selector, logjam_log_selector=, logjam_only, logjam_only?

Class Method Details

.add_forwarder(type, *args) ⇒ Object



270
271
272
273
274
275
276
# File 'lib/logjam_agent.rb', line 270

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) ⇒ Object



173
174
175
176
177
178
# File 'lib/logjam_agent.rb', line 173

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_exceptionsObject



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/logjam_agent.rb', line 191

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) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/logjam_agent.rb', line 91

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) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/logjam_agent.rb', line 244

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_classesObject



184
185
186
187
188
189
# File 'lib/logjam_agent.rb', line 184

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!Object



67
68
69
# File 'lib/logjam_agent.rb', line 67

def self.disable!
  self.disabled = true
end

.enable!Object



71
72
73
# File 'lib/logjam_agent.rb', line 71

def self.enable!
  self.disabled = false
end

.encode_payload(data) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/logjam_agent.rb', line 227

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 = {}) ⇒ Object



259
260
261
262
263
264
265
266
267
268
# File 'lib/logjam_agent.rb', line 259

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

.get_hostnameObject



58
59
60
61
62
# File 'lib/logjam_agent.rb', line 58

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

.log_to_log_device?(severity, msg) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
163
164
165
166
167
168
# File 'lib/logjam_agent.rb', line 159

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) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/logjam_agent.rb', line 139

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_matcherObject



180
181
182
# File 'lib/logjam_agent.rb', line 180

def self.reset_exception_matcher
  self.exception_matcher = Regexp.new(self.exception_classes.map{|e| Regexp.escape(e)}.join("|"))
end