Skip to content

Commit

Permalink
Add req count & req latency metrics for UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Artemmkin committed Oct 30, 2017
1 parent 67ba2da commit e443f6a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ui/config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ require './ui_app'
require 'rack'
require 'prometheus/middleware/collector'
require 'prometheus/middleware/exporter'
require './middleware.rb'

use Metrics
use Rack::Deflater, if: ->(_, _, _, body) { body.any? && body[0].length > 512 }
use Prometheus::Middleware::Collector
use Prometheus::Middleware::Exporter
Expand Down
23 changes: 23 additions & 0 deletions ui/middleware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'prometheus/client'

class Metrics

def initialize app
@app = app
prometheus = Prometheus::Client.registry
@request_count = Prometheus::Client::Counter.new(:ui_request_count, 'App Request Count')
@request_latency = Prometheus::Client::Histogram.new(:ui_request_latency_seconds, 'Request latency')
prometheus.register(@request_latency)
prometheus.register(@request_count)
end

def call env
request_started_on = Time.now
@status, @headers, @response = @app.call(env)
request_ended_on = Time.now
@request_latency.observe({ path: env['REQUEST_PATH'] }, request_ended_on - request_started_on)
@request_count.increment({ method: env['REQUEST_METHOD'], path: env['REQUEST_PATH'], http_status: @status })
[@status, @headers, @response]
end

end

0 comments on commit e443f6a

Please sign in to comment.