Class: FluentLoggerStatistics::App

Inherits:
Object
  • Object
show all
Includes:
Rack::Utils
Defined in:
lib/fluent_logger_statistics/app.rb

Constant Summary collapse

ACCEPT_METHODS =
['GET'].freeze

Instance Method Summary collapse

Constructor Details

#initialize(fluent_loggers) ⇒ App

Returns a new instance of App.



5
6
7
# File 'lib/fluent_logger_statistics/app.rb', line 5

def initialize(fluent_loggers)
  @fluent_loggers = fluent_loggers
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fluent_logger_statistics/app.rb', line 11

def call(env)
  unless ACCEPT_METHODS.include?(env['REQUEST_METHOD'])
    return [404, {'Content-Type' => 'text/plain'}, []]
  end

  status = 200
  header = {'Content-Type' => 'application/json'}

  stats = @fluent_loggers.map{|k,v|
    bytesize = v.pending_bytesize
    limit_bytesize = v.limit
    usage_rate = bytesize / limit_bytesize.to_f
    [k, {
      buffer_bytesize: bytesize,
      buffer_limit: limit_bytesize,
      buffer_usage_rate: usage_rate
    }]
  }.to_h

  body = [ stats.to_json ]

  [status, header, body]
end