diff --git a/.env.sample b/.env.sample index 5a23522fb..bb5bdaf90 100644 --- a/.env.sample +++ b/.env.sample @@ -9,3 +9,6 @@ APP_HOST=http://localhost:3000 EXTENSIONS_MANAGER_LOCATION=extensions/extensions-manager/dist/index.html BATCH_MANAGER_LOCATION=extensions/batch-manager/dist/index.min.html SF_DEFAULT_SERVER=http://localhost:3001 + +# Datadog +DATADOG_ENABLED=false diff --git a/Gemfile b/Gemfile index 7f21b61b0..3c2ecc4ed 100644 --- a/Gemfile +++ b/Gemfile @@ -46,3 +46,8 @@ group :development, :test do gem 'capistrano-sidekiq' gem 'capistrano-git-with-submodules', '~> 2.0' end + +gem "dogstatsd-ruby", "~> 4.8" +gem "ddtrace", "~> 0.38.0" + +gem "lograge", "~> 0.11.2" diff --git a/Gemfile.lock b/Gemfile.lock index 6b47c56e8..f8e71596f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -67,6 +67,9 @@ GEM concurrent-ruby (1.1.5) connection_pool (2.2.2) crass (1.0.6) + ddtrace (0.38.0) + msgpack + dogstatsd-ruby (4.8.1) dotenv (2.7.5) dotenv-rails (2.7.5) dotenv (= 2.7.5) @@ -83,6 +86,11 @@ GEM i18n (1.8.2) concurrent-ruby (~> 1.0) json (1.8.6) + lograge (0.11.2) + actionpack (>= 4) + activesupport (>= 4) + railties (>= 4) + request_store (~> 1.0) loofah (2.4.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) @@ -92,6 +100,8 @@ GEM mini_mime (1.0.2) mini_portile2 (2.4.0) minitest (5.14.0) + msgpack (1.3.3) + msgpack (1.3.3-x64-mingw32) net-scp (2.0.0) net-ssh (>= 2.6.5, < 6.0.0) net-ssh (5.2.0) @@ -140,6 +150,8 @@ GEM ffi (~> 1.0) rdoc (4.3.0) redis (4.1.3) + request_store (1.5.0) + rack (>= 1.4) responders (2.4.1) actionpack (>= 4.2.0, < 6.0) railties (>= 4.2.0, < 6.0) @@ -200,8 +212,11 @@ DEPENDENCIES capistrano-rails capistrano-rvm capistrano-sidekiq + ddtrace (~> 0.38.0) + dogstatsd-ruby (~> 4.8) dotenv-rails haml + lograge (~> 0.11.2) non-stupid-digest-assets puma rack-cors diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d6ac487eb..cae57aaf0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -14,6 +14,10 @@ class ApplicationController < ActionController::Base rescue_from ActionView::MissingTemplate do |exception| end + def route_not_found + render :json => {:error => {:message => "Not found."}}, :status => 404 + end + protected def allow_iframe @@ -24,4 +28,19 @@ class ApplicationController < ActionController::Base cookies['XSRF-TOKEN'] = form_authenticity_token if protect_against_forgery? end + def append_info_to_payload(payload) + super + + unless payload[:status] + return + end + + payload[:level] = 'INFO' + if payload[:status] >= 500 + payload[:level] = 'ERROR' + elsif payload[:status] >= 400 + payload[:level] = 'WARN' + end + end + end diff --git a/config/environments/development.rb b/config/environments/development.rb index 246740235..38034757b 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -14,8 +14,12 @@ Rails.application.configure do # Do not eager load code on boot. config.eager_load = false - require 'custom_log_formatter' - config.log_formatter = CustomLogFormatter.new + MAX_LOG_MEGABYTES = 50 + config.logger = ActiveSupport::Logger.new(config.paths['log'].first, 1, MAX_LOG_MEGABYTES * 1024 * 1024) + + if ENV["RAILS_LOG_TO_STDOUT"].present? + config.logger = ActiveSupport::Logger.new(STDOUT) + end # Show full error reports and disable caching. config.consider_all_requests_local = true diff --git a/config/environments/production.rb b/config/environments/production.rb index 7a9334c62..0cc1b38e1 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -7,21 +7,13 @@ Rails.application.configure do # Code is not reloaded between requests. config.cache_classes = true - # Use a different logger for distributed setups. - # require 'syslog/logger' - # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') + MAX_LOG_MEGABYTES = 50 + config.logger = ActiveSupport::Logger.new(config.paths['log'].first, 1, MAX_LOG_MEGABYTES * 1024 * 1024) if ENV["RAILS_LOG_TO_STDOUT"].present? config.logger = ActiveSupport::Logger.new(STDOUT) - else - MAX_LOG_MEGABYTES = 50 - config.logger = ActiveSupport::Logger.new(config.paths['log'].first, 1, MAX_LOG_MEGABYTES * 1024 * 1024) end - require 'custom_log_formatter' - config.log_formatter = CustomLogFormatter.new - config.logger.formatter = config.log_formatter - # 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. diff --git a/config/environments/staging.rb b/config/environments/staging.rb index 1f42d82e3..0e11719e7 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -8,10 +8,11 @@ Rails.application.configure do # require 'syslog/logger' # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') + MAX_LOG_MEGABYTES = 50 + config.logger = ActiveSupport::Logger.new(config.paths['log'].first, 1, MAX_LOG_MEGABYTES * 1024 * 1024) + if ENV["RAILS_LOG_TO_STDOUT"].present? - logger = ActiveSupport::Logger.new(STDOUT) - logger.formatter = config.log_formatter - config.logger = ActiveSupport::TaggedLogging.new(logger) + config.logger = ActiveSupport::Logger.new(STDOUT) end # Eager load code on boot. This eager loads most of Rails and @@ -73,9 +74,6 @@ Rails.application.configure do # 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 - # Do not dump schema after migrations. # config.active_record.dump_schema_after_migration = false end diff --git a/config/initializers/datadog.rb b/config/initializers/datadog.rb new file mode 100644 index 000000000..a882239a3 --- /dev/null +++ b/config/initializers/datadog.rb @@ -0,0 +1,6 @@ +if ENV['DATADOG_ENABLED'] == 'true' + Datadog.configure do |c| + # This will activate auto-instrumentation for Rails + c.use :rails + end +end diff --git a/config/initializers/lograge.rb b/config/initializers/lograge.rb new file mode 100644 index 000000000..0618fff49 --- /dev/null +++ b/config/initializers/lograge.rb @@ -0,0 +1,14 @@ +Rails.application.configure do + config.lograge.enabled = true + + # Generate log in JSON + config.lograge.formatter = Lograge::Formatters::Json.new + config.lograge.custom_options = lambda do |event| + { + :ddsource => ["ruby"], + :time => event.time, + :params => event.payload[:params], + :level => event.payload[:level] + } + end +end diff --git a/config/routes.rb b/config/routes.rb index a62838c23..7742f14bf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,7 @@ Rails.application.routes.draw do get "/healthcheck" => "health_check#index" + get '*unmatched_route', to: 'application#route_not_found' + root 'application#app' end diff --git a/lib/custom_log_formatter.rb b/lib/custom_log_formatter.rb deleted file mode 100644 index d84026ecb..000000000 --- a/lib/custom_log_formatter.rb +++ /dev/null @@ -1,22 +0,0 @@ -class CustomLogFormatter < ActiveSupport::Logger::SimpleFormatter - SEVERITY_TO_COLOR_MAP = {'DEBUG'=>'0;37', 'INFO'=>'32', 'WARN'=>'33', 'ERROR'=>'31', 'FATAL'=>'31', 'UNKNOWN'=>'37'} - - IPRegexp = /\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/ - FilteredString = '**FILTERED**' - - def call(severity, time, progname, msg) - formatted_severity = sprintf("%-5s","#{severity}") - formatted_time = time.strftime("%Y-%m-%d %H:%M:%S.") << time.usec.to_s[0..2].rjust(3) - color = SEVERITY_TO_COLOR_MAP[severity] - - "\033[0;37m#{formatted_time}\033[0m [\033[#{color}m#{formatted_severity}\033[0m] #{filter_ip(msg)} (pid:#{$$})\n" - end - - def filter_ip(msg) - if msg.is_a? String - return msg.gsub(IPRegexp, FilteredString).strip - else - return msg - end - end -end