Giter VIP home page Giter VIP logo

production_rails's Introduction

Production Rails

Best practices for running Rails in production

Gem coming soon! Get notified

Errors

Use an error reporting service like Rollbar.

Also, track 400 and 500 status codes.

ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, start, finish, id, payload|
  if !payload[:status] or payload[:status].to_i >= 400
    # track it
  end
end

Timeouts

Use Slowpoke for request and database timeouts.

gem 'slowpoke'

Throttling

Use Rack Attack to throttle and block requests.

Audits

Use an auditing library like Audited.

Slow Requests

Keep track of slow requests

ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, start, finish, id, payload|
  duration = finish - start
  if duration > 5.seconds
    # track here
  end
end

Unpermitted Parameters

ActiveSupport::Notifications.subscribe "unpermitted_parameters.action_controller" do |name, start, finish, id, payload|
  # track here
end

Failed Validations

module TrackErrors
  extend ActiveSupport::Concern

  included do
    after_validation :track_errors
  end

  def track_errors
    if errors.any?
      # track here
    end
  end
end

ActiveRecord::Base.send(:include, TrackErrors)

Failed CSRF

class ApplicationController < ActionController::Base
  def handle_unverified_request_with_tracking(*args)
    # track here

    handle_unverified_request_without_tracking(*args)
  end
  alias_method_chain :handle_unverified_request, :tracking
end

Logging

Use Lograge.

gem 'lograge'

Add the following to config/environments/production.rb.

config.lograge.enabled = true
config.lograge.custom_options = lambda do |event|
  options = event.payload.slice(:request_id, :user_id, :visit_id)
  options[:params] = event.payload[:params].except("controller", "action")
  # if you use Searchkick
  if event.payload[:searchkick_runtime].to_f > 0
    options[:search] = event.payload[:searchkick_runtime]
  end
  options
end

Add the following to app/controllers/application_controller.rb.

def append_info_to_payload(payload)
  super
  payload[:request_id] = request.uuid
  payload[:user_id] = current_user.id if current_user
  payload[:visit_id] = ahoy.visit_id # if you use Ahoy
end

Uptime Monitoring

Use an uptime monitoring service like Pingdom or Uptime Robot.

Monitor web servers, background jobs, and scheduled tasks.

Performance Monitoring

Use a performance monitoring service like New Relic or AppSignal.

Be sure to monitor:

Web Requests

  • requests by action - total time, count
  • errors - with affected users
  • queue time - X-Request-Start header
  • timeouts
  • 404s
  • invalid authenticity token
  • unpermitted parameters
  • invalid form submissions

Background Jobs and Rake Tasks

  • jobs by type - total time, count
  • errors

Data Stores - Database, Elasticsearch, Redis

  • requests by type - total time, count
  • errors
  • CPU usage
  • space

External Services

  • requests by type - total time, count
  • errors

Web Server

Use a high performance web server like Unicorn.

gem 'unicorn'

Security

Use SSL to protect your users. Add the following to config/environments/production.rb.

config.force_ssl = true

Development Bonus

Fix double logging in the Rails console. Create config/initializers/log_once.rb with:

ActiveSupport::Logger.class_eval do
  def self.broadcast(logger)
    Module.new do
    end
  end
end

TODO

  • Redis timeout
  • Elasticsearch timeout
  • Background jobs
  • Scheduled jobs
  • Gemify parts

Thanks

production_rails's People

Contributors

ankane avatar

Watchers

Tony Castiglione avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.