forked from yabeda-rb/yabeda-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit: declare and collect web request metrics
- Loading branch information
0 parents
commit 25c2ab4
Showing
15 changed files
with
317 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/.bundle/ | ||
/.yardoc | ||
/_yardoc/ | ||
/coverage/ | ||
/doc/ | ||
/pkg/ | ||
/spec/reports/ | ||
/tmp/ | ||
Gemfile.lock | ||
|
||
# rspec failure tracking | ||
.rspec_status |
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,3 @@ | ||
--format documentation | ||
--color | ||
--require spec_helper |
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,46 @@ | ||
--- | ||
require: | ||
- rubocop-rspec | ||
|
||
AllCops: | ||
TargetRubyVersion: 2.3 | ||
|
||
Metrics/BlockLength: | ||
Exclude: | ||
- "Gemfile" | ||
- "spec/**/*" | ||
|
||
Style/BracesAroundHashParameters: | ||
EnforcedStyle: context_dependent | ||
|
||
Style/StringLiterals: | ||
EnforcedStyle: double_quotes | ||
|
||
# Allow to use let! | ||
RSpec/LetSetup: | ||
Enabled: false | ||
|
||
RSpec/MultipleExpectations: | ||
Enabled: false | ||
|
||
Bundler/OrderedGems: | ||
Enabled: false | ||
|
||
Style/TrailingCommaInArguments: | ||
Description: 'Checks for trailing comma in argument lists.' | ||
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-params-comma' | ||
Enabled: true | ||
EnforcedStyleForMultiline: consistent_comma | ||
|
||
Style/TrailingCommaInArrayLiteral: | ||
Description: 'Checks for trailing comma in array literals.' | ||
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas' | ||
Enabled: true | ||
EnforcedStyleForMultiline: consistent_comma | ||
|
||
Style/TrailingCommaInHashLiteral: | ||
Description: 'Checks for trailing comma in hash literals.' | ||
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas' | ||
Enabled: true | ||
EnforcedStyleForMultiline: consistent_comma | ||
|
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,5 @@ | ||
sudo: false | ||
language: ruby | ||
rvm: | ||
- 2.5.1 | ||
before_install: gem install bundler -v 1.16.1 |
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,16 @@ | ||
# frozen_string_literal: true | ||
|
||
source "https://rubygems.org" | ||
|
||
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } | ||
|
||
# Specify your gem's dependencies in evil-metrics-rails.gemspec | ||
gemspec | ||
|
||
group :development, :test do | ||
gem "pry" | ||
gem "pry-byebug", platform: :mri | ||
|
||
gem "rubocop" | ||
gem "rubocop-rspec" | ||
end |
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2018 Andrey Novikov | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,55 @@ | ||
# Evil::Metrics::[Rails] | ||
|
||
Built-in metrics for out-of-the box [Rails] applications monitoring | ||
|
||
if your monitoring system already collects Rails metrics (e.g. NewRelic) then you don't need this gem. | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
```ruby | ||
gem 'evil-metrics-rails' | ||
# Then add monitoring system adapter, e.g.: | ||
# gem 'evil-metrics-prometheus' | ||
``` | ||
|
||
And then execute: | ||
|
||
$ bundle | ||
|
||
## Metrics | ||
|
||
- Total web requests received: `rails_requests_total` | ||
- Web request duration: `rails_request_duration` (in seconds) | ||
- Views rendering duration: `rails_view_runtime` (in seconds) | ||
- DB request duration: `rails_db_runtime` (in seconds) | ||
|
||
|
||
## Hooks | ||
|
||
- `on_controller_action`: Allows to collect | ||
|
||
```ruby | ||
Evil::Metrics::Rails.on_controller_action do |event, labels| | ||
next unless event.payload[:ext_service_runtime] | ||
time_in_seconds = event.payload[:ext_service_runtime] / 1000.0 | ||
rails_ext_service_runtime.measure(labels, time_in_seconds) | ||
end | ||
``` | ||
|
||
## Development | ||
|
||
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. | ||
|
||
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). | ||
|
||
## Contributing | ||
|
||
Bug reports and pull requests are welcome on GitHub at https://github.com/evil-metrics/evil-metrics-rails. | ||
|
||
## License | ||
|
||
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). | ||
|
||
[Rails]: https://rubyonrails.org "Ruby on Rails MVC web-application framework optimized for programmer happiness" |
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,8 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
task default: :spec |
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,11 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require "bundler/setup" | ||
require "evil/metrics/rails" | ||
|
||
# You can add fixtures and/or initialization code here to make experimenting | ||
# with your gem easier. You can also use a different console, if you like. | ||
|
||
require "pry" | ||
Pry.start |
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,8 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
set -vx | ||
|
||
bundle install | ||
|
||
# Do any other automated setup that you need to do here |
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,27 @@ | ||
# frozen_string_literal: true | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "evil-metrics-rails" | ||
spec.version = "0.1.0" | ||
spec.authors = ["Andrey Novikov"] | ||
spec.email = ["[email protected]"] | ||
|
||
spec.summary = "Extensible metrics for monitoring Ruby on Rails application" | ||
spec.description = "Easy collecting your Rails apps metrics" | ||
spec.homepage = "https://github.com/evil-metrics/evil-metrics-rails" | ||
spec.license = "MIT" | ||
|
||
spec.files = `git ls-files -z`.split("\x0").reject do |f| | ||
f.match(%r{^(test|spec|features)/}) | ||
end | ||
spec.bindir = "exe" | ||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | ||
spec.require_paths = ["lib"] | ||
|
||
spec.add_dependency "evil-metrics" | ||
spec.add_dependency "rails" | ||
|
||
spec.add_development_dependency "bundler", "~> 1.16" | ||
spec.add_development_dependency "rake", "~> 10.0" | ||
spec.add_development_dependency "rspec", "~> 3.0" | ||
end |
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,63 @@ | ||
# frozen_string_literal: true | ||
|
||
require "evil/metrics" | ||
require "evil/metrics/rails/railtie" | ||
|
||
module Evil | ||
module Metrics | ||
module Rails | ||
LONG_RUNNING_REQUEST_BUCKETS = [ | ||
0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, # standard | ||
30, 60, 120, 300, 600, # Sometimes requests may be really long-running | ||
].freeze | ||
|
||
class << self | ||
def controller_handlers | ||
@controller_handlers ||= [] | ||
end | ||
|
||
def on_controller_action(&block) | ||
controller_handlers << block | ||
end | ||
|
||
def install! | ||
Evil::Metrics.configure do | ||
group :rails | ||
|
||
counter :requests_total, comment: "A counter of the total number of HTTP requests rails processed." | ||
histogram :request_duration, unit: :seconds, buckets: LONG_RUNNING_REQUEST_BUCKETS, | ||
comment: "A histogram of the response latency." | ||
histogram :view_runtime, unit: :seconds, buckets: LONG_RUNNING_REQUEST_BUCKETS, | ||
comment: "A histogram of the view rendering time." | ||
histogram :db_runtime, unit: :seconds, buckets: LONG_RUNNING_REQUEST_BUCKETS, | ||
comment: "A histogram of the activerecord execution time." | ||
|
||
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args| | ||
event = ActiveSupport::Notifications::Event.new(*args) | ||
labels = { | ||
controller: event.payload[:params]["controller"], | ||
action: event.payload[:params]["action"], | ||
status: event.payload[:status], | ||
format: event.payload[:format], | ||
method: event.payload[:method].downcase, | ||
} | ||
|
||
rails_requests_total.increment(labels) | ||
rails_request_duration.measure(labels, Evil::Metrics::Rails.ms2s(event.duration)) | ||
rails_view_runtime.measure(labels, Evil::Metrics::Rails.ms2s(event.payload[:view_runtime])) | ||
rails_db_runtime.measure(labels, Evil::Metrics::Rails.ms2s(event.payload[:db_runtime])) | ||
|
||
Evil::Metrics::Rails.controller_handlers.each do |handler| | ||
handler.call(event, labels) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def ms2s(ms) | ||
(ms.to_f / 1000).round(3) | ||
end | ||
end | ||
end | ||
end | ||
end |
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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module Evil | ||
module Metrics | ||
module Rails | ||
class Railtie < ::Rails::Railtie # :nodoc: | ||
config.after_initialize do | ||
next unless ::Rails.const_defined?(:Server) | ||
|
||
::Evil::Metrics::Rails.install! | ||
end | ||
end | ||
end | ||
end | ||
end |
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Evil::Metrics::Rails do | ||
it "has a version number" do | ||
expect(Evil::Metrics::Rails::VERSION).not_to be nil | ||
end | ||
|
||
it "does something useful" do | ||
expect(false).to eq(true) | ||
end | ||
end |
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,16 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/setup" | ||
require "evil/metrics/rails" | ||
|
||
RSpec.configure do |config| | ||
# Enable flags like --only-failures and --next-failure | ||
config.example_status_persistence_file_path = ".rspec_status" | ||
|
||
# Disable RSpec exposing methods globally on `Module` and `main` | ||
config.disable_monkey_patching! | ||
|
||
config.expect_with :rspec do |c| | ||
c.syntax = :expect | ||
end | ||
end |