Class: JrubyRackMetrics::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/jruby-rack-metrics.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Monitor

Returns a new instance of Monitor.



16
17
18
19
20
21
22
23
# File 'lib/jruby-rack-metrics.rb', line 16

def initialize(app, opts = {})
  @app = app
  @options = default_options.merge(opts)
  @timing_unit = TimeUnit::NANOSECONDS
  if @options[:jmx_enabled]
    JmxReporter.startDefault(metrics_registry)
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/jruby-rack-metrics.rb', line 14

def options
  @options
end

Instance Method Details

#call(env = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jruby-rack-metrics.rb', line 35

def call(env = nil)
  if env.nil?
    @app.call(env)
  else
    start_time = System.nanoTime()
    begin
      status, headers, body = @app.call(env)
    ensure
      elapsed = System.nanoTime() - start_time
      # some web servers give us the full url, some only the path part
      uri = URI.parse(env['REQUEST_URI'])
      if defined? uri.path && !uri.path.nil?
        if uri.path == "/"
          group = "_root"
        else
          group = uri.path.gsub(/[\/|\s|,|;|#|!|:]/, "_")
          group = group[1..-1] if group.start_with?("_")
        end
        type = env['REQUEST_METHOD'].downcase
        name = (status || 500).to_s
        metric_name = MetricName.new(group, type, name)
        metrics_registry.newTimer(metric_name,
                                  @options[:default_duration_unit],
                                  @options[:default_rate_unit]).update(elapsed, @timing_unit)
      end
    end
  end
end

#default_optionsObject



25
26
27
28
29
# File 'lib/jruby-rack-metrics.rb', line 25

def default_options
  { :default_duration_unit => TimeUnit::MILLISECONDS,
    :default_rate_unit => TimeUnit::SECONDS,
    :jmx_enabled => false }
end

#metrics_registryObject



31
32
33
# File 'lib/jruby-rack-metrics.rb', line 31

def metrics_registry
  @options[:metrics_registry] ||= Metrics.defaultRegistry
end