Skip to content

Commit

Permalink
Initial commit: declare and collect web request metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Envek committed Oct 3, 2018
0 parents commit 25c2ab4
Show file tree
Hide file tree
Showing 15 changed files with 317 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
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
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
46 changes: 46 additions & 0 deletions .rubocop.yml
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

5 changes: 5 additions & 0 deletions .travis.yml
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
16 changes: 16 additions & 0 deletions Gemfile
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
21 changes: 21 additions & 0 deletions LICENSE.txt
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.
55 changes: 55 additions & 0 deletions README.md
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"
8 changes: 8 additions & 0 deletions Rakefile
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
11 changes: 11 additions & 0 deletions bin/console
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
8 changes: 8 additions & 0 deletions bin/setup
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
27 changes: 27 additions & 0 deletions evil-metrics-rails.gemspec
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
63 changes: 63 additions & 0 deletions lib/evil/metrics/rails.rb
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
15 changes: 15 additions & 0 deletions lib/evil/metrics/rails/railtie.rb
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
11 changes: 11 additions & 0 deletions spec/evil/prome/rails_spec.rb
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
16 changes: 16 additions & 0 deletions spec/spec_helper.rb
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

0 comments on commit 25c2ab4

Please sign in to comment.