New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve rack middlewares #32
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,22 @@ | ||
require 'rack' | ||
require 'prometheus/client/rack/collector' | ||
require 'prometheus/client/rack/exporter' | ||
require 'prometheus/middleware/collector' | ||
require 'prometheus/middleware/exporter' | ||
|
||
use Rack::Deflater, if: ->(_, _, _, body) { body.any? && body[0].length > 512 } | ||
use Prometheus::Client::Rack::Collector | ||
use Prometheus::Client::Rack::Exporter | ||
use Prometheus::Middleware::Collector | ||
use Prometheus::Middleware::Exporter | ||
|
||
run ->(_) { [200, { 'Content-Type' => 'text/html' }, ['OK']] } | ||
srand | ||
|
||
app = lambda do |_| | ||
case rand | ||
when 0..0.8 | ||
[200, { 'Content-Type' => 'text/html' }, ['OK']] | ||
when 0.8..0.95 | ||
[404, { 'Content-Type' => 'text/html' }, ['Not Found']] | ||
else | ||
raise NoMethodError, 'It is a bug!' | ||
end | ||
end | ||
|
||
run app |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# encoding: UTF-8 | ||
|
||
require 'prometheus/client' | ||
|
||
module Prometheus | ||
module Middleware | ||
# Collector is a Rack middleware that provides a sample implementation of a | ||
# HTTP tracer. | ||
# | ||
# By default metrics are registered on the global registry. Set the | ||
# `:registry` option to use a custom registry. | ||
# | ||
# The request counter metric is broken down by code, method and path by | ||
# default. Set the `:counter_label_builder` option to use a custom label | ||
# builder. | ||
# | ||
# The request duration metric is broken down by method and path by default. | ||
# Set the `:duration_label_builder` option to use a custom label builder. | ||
class Collector | ||
attr_reader :app, :registry | ||
|
||
def initialize(app, options = {}) | ||
@app = app | ||
@registry = options[:registry] || Client.registry | ||
@counter_lb = options[:counter_label_builder] || COUNTER_LB | ||
@duration_lb = options[:duration_label_builder] || DURATION_LB | ||
|
||
init_request_metrics | ||
init_exception_metrics | ||
end | ||
|
||
def call(env) # :nodoc: | ||
trace(env) { @app.call(env) } | ||
end | ||
|
||
protected | ||
|
||
COUNTER_LB = proc do |env, code| | ||
{ | ||
code: code, | ||
method: env['REQUEST_METHOD'].downcase, | ||
path: env['PATH_INFO'].to_s, | ||
} | ||
end | ||
|
||
DURATION_LB = proc do |env, _| | ||
{ | ||
method: env['REQUEST_METHOD'].downcase, | ||
path: env['PATH_INFO'].to_s, | ||
} | ||
end | ||
|
||
def init_request_metrics | ||
@requests = @registry.counter( | ||
:http_server_requests_total, | ||
'The total number of HTTP requests handled by the Rack application.', | ||
) | ||
@durations = @registry.histogram( | ||
:http_server_request_duration_seconds, | ||
'The HTTP response duration of the Rack application.', | ||
) | ||
end | ||
|
||
def init_exception_metrics | ||
@exceptions = @registry.counter( | ||
:http_server_exceptions_total, | ||
'The total number of exceptions raised by the Rack application.', | ||
) | ||
end | ||
|
||
def trace(env) | ||
start = Time.now | ||
yield.tap do |response| | ||
duration = [(Time.now - start).to_f, 0.0].max | ||
record(env, response.first.to_s, duration) | ||
end | ||
rescue => exception | ||
@exceptions.increment(exception: exception.class.name) | ||
raise | ||
end | ||
|
||
def record(env, code, duration) | ||
@requests.increment(@counter_lb.call(env, code)) | ||
@durations.observe(@duration_lb.call(env, code), duration) | ||
rescue | ||
# TODO: log unexpected exception during request recording | ||
nil | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's from rack, then the metric name should contain "rack"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rack is the de facto standard interface for ruby HTTP servers. It can be implemented by many different servers (unicorn, thin, puma, ...). I thought about including it, but concluded after some discussions that including it in the metric name is neither expected nor helpful.