Skip to content

Commit

Permalink
feat: add datadog integration and lograge JSON logs formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
karolsojko committed Jul 28, 2020
1 parent d7260e8 commit c2ac21b
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 40 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
15 changes: 15 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def app
rescue_from ActionView::MissingTemplate do |exception|
end

def route_not_found
render :json => {:error => {:message => "Not found."}}, :status => 404
end

protected

def allow_iframe
Expand All @@ -24,4 +28,19 @@ def set_csrf_cookie
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
8 changes: 6 additions & 2 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
# 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
Expand Down
12 changes: 2 additions & 10 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,13 @@
# 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.
Expand Down
10 changes: 4 additions & 6 deletions config/environments/staging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
# 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
Expand Down Expand Up @@ -73,9 +74,6 @@
# 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
6 changes: 6 additions & 0 deletions config/initializers/datadog.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions config/initializers/lograge.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 0 additions & 22 deletions lib/custom_log_formatter.rb

This file was deleted.

0 comments on commit c2ac21b

Please sign in to comment.