Skip to content
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

Optimization StoriesController#index #141

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .dev_to/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ x-backend: &backend
PSQL_HISTFILE: /usr/local/hist/.psql_history
IRB_HISTFILE: /usr/local/hist/.irb_history
EDITOR: vi
SKYLIGHT_AUTHENTICATION: $SKYLIGHT_AUTHENTICATION
PROMETHEUS_EXPORTER_HOST: exporter
depends_on: &backend_depends_on
postgres:
condition: service_healthy
Expand All @@ -71,6 +73,12 @@ services:
# <<: *backend
# command: bundle exec sidekiq -C config/sidekiq.yml

exporter:
<<: *backend
command: bundle exec prometheus_exporter -b 0.0.0.0 -a prometheus/custom_collector.rb
ports:
- "9394:9394"

postgres:
image: postgres:14
volumes:
Expand Down Expand Up @@ -98,6 +106,24 @@ services:
timeout: 3s
retries: 30

prometheus:
image: dockerhub.timeweb.cloud/prom/prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
- prom_data:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
ports:
- '9090:9090'
grafana:
image: dockerhub.timeweb.cloud/grafana/grafana
environment:
- GF_SECURITY_ADMIN_PASSWORD=pass
depends_on:
- prometheus
ports:
- "3030:3000"

webpacker:
<<: *app
command: bundle exec ./bin/webpack-dev-server
Expand All @@ -123,3 +149,4 @@ volumes:
redis:
packs:
packs-test:
prom_data:
11 changes: 11 additions & 0 deletions .dev_to/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
global:
scrape_interval: 5s
external_labels:
monitor: 'my-monitor'
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'devdev'
static_configs:
- targets: ['exporter:9394']
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ gem "sdoc", "~> 1.0", group: :doc
gem "serviceworker-rails", "~> 0.5"
gem "share_meow_client", "~> 0.1"
gem "sitemap_generator", "~> 6.0"
gem "skylight", "~> 3.1"
gem "skylight"
gem "slack-notifier", "~> 2.3"
gem "sprockets", "~> 3.7"
gem "staccato", "~> 0.5"
Expand All @@ -102,6 +102,8 @@ gem "uglifier", "~> 4.1"
gem "validate_url", "~> 1.0"
gem "webpacker", "~> 3.5"
gem "webpush", "~> 0.3"
gem "rack-mini-profiler"
gem "prometheus_exporter"

group :development do
gem "better_errors", "~> 2.5"
Expand Down
23 changes: 15 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.1.5)
concurrent-ruby (1.2.3)
connection_pool (2.2.2)
counter_culture (2.1.2)
activerecord (>= 3.0.0)
Expand Down Expand Up @@ -523,7 +523,7 @@ GEM
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
httpclient (2.8.3)
i18n (1.6.0)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
ice_nine (0.11.2)
inflecto (0.0.2)
Expand Down Expand Up @@ -585,7 +585,7 @@ GEM
mime-types-data (3.2018.0812)
mini_mime (1.0.1)
mini_portile2 (2.4.0)
minitest (5.11.3)
minitest (5.23.1)
momentjs-rails (2.20.1)
railties (>= 3.1)
msgpack (1.2.4)
Expand Down Expand Up @@ -638,6 +638,8 @@ GEM
ast (~> 2.4.0)
pg (1.1.4)
powerpack (0.1.2)
prometheus_exporter (2.1.0)
webrick
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
Expand All @@ -664,6 +666,8 @@ GEM
rack (2.0.6)
rack-host-redirect (1.3.0)
rack
rack-mini-profiler (3.1.1)
rack (>= 1.2.0)
rack-protection (2.0.4)
rack
rack-proxy (0.6.5)
Expand Down Expand Up @@ -830,9 +834,9 @@ GEM
tilt (~> 2.0)
sitemap_generator (6.0.2)
builder (~> 3.0)
skylight (3.1.4)
skylight-core (= 3.1.4)
skylight-core (3.1.4)
skylight (4.3.2)
skylight-core (= 4.3.2)
skylight-core (4.3.2)
activesupport (>= 4.2.0)
slack-notifier (2.3.2)
smart_properties (1.13.1)
Expand Down Expand Up @@ -878,7 +882,7 @@ GEM
multipart-post (~> 2.0)
naught (~> 1.0)
simple_oauth (~> 0.3.0)
tzinfo (1.2.5)
tzinfo (1.2.11)
thread_safe (~> 0.1)
uber (0.1.0)
uglifier (4.1.20)
Expand Down Expand Up @@ -915,6 +919,7 @@ GEM
webpush (0.3.2)
hkdf (~> 0.2)
jwt
webrick (1.8.1)
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
Expand Down Expand Up @@ -1003,6 +1008,7 @@ DEPENDENCIES
omniauth-twitter (~> 1.4)
parallel_tests (~> 2.27)
pg (~> 1.1)
prometheus_exporter
pry (~> 0.12)
pry-byebug (~> 3.7)
pry-rails (~> 0.3)
Expand All @@ -1012,6 +1018,7 @@ DEPENDENCIES
pusher (~> 1.3)
pusher-push-notifications (~> 1.0)
rack-host-redirect (~> 1.3)
rack-mini-profiler
rack-timeout (~> 0.5)
rails (~> 5.1.6)
rails-assets-airbrake-js-client (~> 1.5)!
Expand Down Expand Up @@ -1040,7 +1047,7 @@ DEPENDENCIES
simplecov (~> 0.16)
sinatra (~> 2.0)
sitemap_generator (~> 6.0)
skylight (~> 3.1)
skylight
slack-notifier (~> 2.3)
sprockets (~> 3.7)
staccato (~> 0.5)
Expand Down
12 changes: 12 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class ApplicationController < ActionController::Base
include Pundit
include Instrumentation

before_action :authorize_rmp, if: :authorize_rmp?

def require_http_auth
authenticate_or_request_with_http_basic do |username, password|
username == ApplicationConfig["APP_NAME"] && password == ApplicationConfig["APP_PASSWORD"]
Expand Down Expand Up @@ -78,4 +80,14 @@ def append_info_to_payload(payload)
super(payload)
append_to_honeycomb(request, self.class.name)
end

def authorize_rmp
Rack::MiniProfiler.authorize_request
end

def authorize_rmp?
# return true unless Rails.env.production?

params[:rmp_token] == 'secret'
end
end
4 changes: 3 additions & 1 deletion app/views/stories/_main_stories_feed.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
<% if !user_signed_in? && i == 4 %>
<%= render "stories/sign_in_invitation" %>
<% end %>
<%= render "articles/single_story", story: story %>
<% cache(story) do %>
<%= render "articles/single_story", story: story %>
<% end %>
<% end %>
<% end %>
<% if @stories.size > 1 %>
Expand Down
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Application < Rails::Application
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de

config.skylight.environments << "development"
config.skylight.environments << "local_production"

config.autoload_paths += Dir["#{config.root}/app/labor/"]
config.autoload_paths += Dir["#{config.root}/app/decorators/"]
config.autoload_paths += Dir["#{config.root}/app/services/"]
Expand Down
131 changes: 131 additions & 0 deletions config/environments/local_production.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# rubocop:disable Metrics/BlockLength

Rails.application.configure do
# Verifies that versions and hashed value of the package contents in the project's package.json
config.webpacker.check_yarn_integrity = false

# Settings specified here will take precedence over those in config/application.rb.

# Code is not reloaded between requests.
config.cache_classes = true

# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both threaded web servers
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
config.eager_load = true

# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.action_controller.perform_caching = true

# Enable Rack::Cache to put a simple HTTP cache in front of your application
# Add `rack-cache` to your Gemfile before enabling this.
# For large-scale production use, consider using a caching reverse proxy like
# NGINX, varnish or squid.
# config.action_dispatch.rack_cache = true
config.read_encrypted_secrets = true

# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present?
config.public_file_server.headers = {
"Cache-Control" => "public, s-maxage=2592000, max-age=86400"
}

# Compress JavaScripts and CSS.
config.assets.js_compressor = Uglifier.new(harmony: true)
# config.assets.css_compressor = :sass

# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = true

# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true

# `config.assets.precompile` and `config.assets.version`
# have moved to config/initializers/assets.rb

# Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true

# Use the lowest log level to ensure availability of diagnostic information
# when problems arise.
config.log_level = :debug

# Prepend all log lines with the following tags.
config.log_tags = [:request_id]

# Use a different logger for distributed setups.
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)

# Use a different cache store in production.
# config.cache_store = :mem_cache_store

# Enable serving of images, stylesheets, and JavaScripts from an asset server.
config.action_controller.asset_host = ENV["FASTLY_CDN_URL"]
config.action_mailer.perform_caching = false

# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
# config.action_mailer.raise_delivery_errors = false

# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation cannot be found).
config.i18n.fallbacks = [I18n.default_locale]

# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify

# Use default logging formatter so that PID and timestamp are not suppressed.
# config.log_formatter = ::Logger::Formatter.new
config.log_formatter = ::Logger::Formatter.new
if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end

# Install the Timber.io logger
send_logs_to_timber = ENV["SEND_LOGS_TO_TIMBER"] || "false" # <---- set to false to stop sending dev logs to Timber.io
log_device = send_logs_to_timber == "true" ? Timber::LogDevices::HTTP.new(ENV["TIMBER"]) : STDOUT
logger = Timber::Logger.new(log_device)
logger.level = config.log_level
config.logger = ActiveSupport::TaggedLogging.new(logger)

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

config.cache_store = :dalli_store,
(ENV["MEMCACHIER_SERVERS"] || "").split(","),
{ username: ENV["MEMCACHIER_USERNAME"],
password: ENV["MEMCACHIER_PASSWORD"],
failover: true,
socket_timeout: 1.5,
socket_failure_delay: 0.2 }

config.app_domain = "dev.to"

config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { host: config.app_domain }
ActionMailer::Base.smtp_settings = {
address: "smtp.sendgrid.net",
port: "587",
authentication: :plain,
user_name: ENV["SENDGRID_USERNAME_ACCEL"],
password: ENV["SENDGRID_PASSWORD_ACCEL"],
domain: "dev.to",
enable_starttls_auto: true
}

config.middleware.use Rack::HostRedirect,
"practicaldev.herokuapp.com" => "dev.to"
end

# rubocop:enable Metrics/BlockLength
2 changes: 1 addition & 1 deletion config/initializers/airbrake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
# environments.
# NOTE: This option *does not* work if you don't set the 'environment' option.
# https://github.com/airbrake/airbrake-ruby#ignore_environments
c.ignore_environments = %w[test development]
c.ignore_environments = %w[test development local_production]

# A list of parameters that should be filtered out of what is sent to
# Airbrake. By default, all "password" attributes will have their contents
Expand Down
6 changes: 6 additions & 0 deletions config/initializers/prometheus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
unless Rails.env.test?
require 'prometheus_exporter/middleware'

# This reports stats per request like HTTP status and timings
Rails.application.middleware.unshift PrometheusExporter::Middleware
end
3 changes: 3 additions & 0 deletions config/secrets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
development:
secret_key_base: a60edc976c913b19fd9fc8118936fbe1df2b07f4eecc5ad32f975e33cd4ea36b150c1ce933b681b90874a46568041629003dcbfc07238f7dca91741bcd1ec870

local_production:
secret_key_base: a60edc976c913b19fd9fc8118936fbe1df2b07f4eecc5ad32f975e33cd4ea36b150c1ce933b681b90874a46568041629003dcbfc07238f7dca91741bcd1ec870

test:
secret_key_base: 42dd7834039ebbea271af22635a6782ee15e519b14629c5276bfcdd4cff841e9926994784bb43a335a8f8c9739bb254ea3afe831839d4dc65654ec7516ec25f0

Expand Down
9 changes: 9 additions & 0 deletions config/webpacker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,12 @@ production:

# Cache manifest.json for performance
cache_manifest: true

local_production:
<<: *default

# Production depends on precompilation of packs prior to booting for performance.
compile: false

# Cache manifest.json for performance
cache_manifest: true
Loading