Class: AMQPLogging::MetricsAgent

Inherits:
Object
  • Object
show all
Defined in:
lib/amqp_logging/metrics_agent.rb

Defined Under Namespace

Modules: MetricsAgentSupport

Constant Summary collapse

DEFAULT_MAX_LINES_PER_LOGGER =
1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMetricsAgent

Returns a new instance of MetricsAgent.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/amqp_logging/metrics_agent.rb', line 10

def initialize
  @default_fields = {
    :host => Socket.gethostname.split('.').first,
    :pid => Process.pid,
    :loglines => {
      :default => []
    }
  }
  @max_lines_per_logger = DEFAULT_MAX_LINES_PER_LOGGER
  @logger_types = {}
  reset_fields
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



7
8
9
# File 'lib/amqp_logging/metrics_agent.rb', line 7

def fields
  @fields
end

#max_lines_per_loggerObject

Returns the value of attribute max_lines_per_logger.



8
9
10
# File 'lib/amqp_logging/metrics_agent.rb', line 8

def max_lines_per_logger
  @max_lines_per_logger
end

Instance Method Details

#[](fieldname) ⇒ Object



38
39
40
# File 'lib/amqp_logging/metrics_agent.rb', line 38

def [](fieldname)
  @fields[fieldname]
end

#[]=(fieldname, value) ⇒ Object



42
43
44
# File 'lib/amqp_logging/metrics_agent.rb', line 42

def []=(fieldname, value)
  @fields[fieldname] = value
end

#add_logline(severity, message, progname, logger) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/amqp_logging/metrics_agent.rb', line 46

def add_logline(severity, message, progname, logger)
  timestring = AMQPLogging.iso_time_with_microseconds
  logtype = @logger_types[logger]
  lines = @fields[:loglines][logtype]
  if !@truncated_status[logtype] && lines.size < @max_lines_per_logger
    msg = (message || progname).strip
    lines << [severity, timestring, msg]
    true
  else
    msg = "Loglines truncated to #{@max_lines_per_logger} lines (MetricsAgent#max_lines_per_logger)"
    lines[-1] = [Logger::INFO, timestring, msg]
    @truncated_status[logtype] = true
    false
  end
end

#dirty?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/amqp_logging/metrics_agent.rb', line 74

def dirty?
  @fields != @default_fields
end

#flushObject



33
34
35
36
# File 'lib/amqp_logging/metrics_agent.rb', line 33

def flush
  logger.info(@fields.to_json + "\n")
  reset_fields
end

#loggerObject



23
24
25
# File 'lib/amqp_logging/metrics_agent.rb', line 23

def logger
  @logger || (self.logger = ::Logger.new($stdout))
end

#logger=(logger) ⇒ Object



27
28
29
30
31
# File 'lib/amqp_logging/metrics_agent.rb', line 27

def logger=(logger)
  @logger = logger
  @logger.formatter = Proc.new {|_, _, msg, progname| msg || progname}
  @logger
end

#wrap_logger(logger, type = :default) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/amqp_logging/metrics_agent.rb', line 62

def wrap_logger(logger, type = :default)
  agent = self
  register_logger(logger, type)
  logger.instance_eval do
    @agent = agent
    class << self
      include MetricsAgentSupport
    end
  end
  logger
end