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_logger) ⇒ App

Returns a new instance of App.



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

def initialize(fluent_logger)
  @fluent_logger = fluent_logger
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
# 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'}

  bytesize = @fluent_logger.pending_bytesize
  limit = @fluent_logger.limit
  usage_rate = bytesize / limit.to_f

  body = [
    { buffer_bytesize: bytesize,
      buffer_limit: limit,
      buffer_usage_rate: usage_rate
    }.to_json
  ]

  [status, header, body]
end