Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
Automate everything

Tasdik Rahman tasdikrahman

💭
Automate everything
View GitHub Profile
View tdd-slide-snippet.diff
diff --git a/app/jobs/check_certificate_job.rb b/app/jobs/check_certificate_job.rb
index 93fb16b..76f995b 100644
+++ b/app/jobs/check_certificate_job.rb
--- a/app/jobs/check_certificate_job.rb
@@ -10,7 +10,7 @@ class CheckCertificateJob < ApplicationJob
Domain.all.each do |domain|
if domain.certificate_expiring?
Rails.logger.info("#{domain.fqdn} is expiring within the buffer period")
+ if (Figaro.env.send_expiry_notifications_to_slack == 'true') && !Figaro.env.slack_webhook_url.empty?
- if (Figaro.env.send_expiry_notifications_to_slack == true) && !Figaro.env.slack_webhook_url.empty?
View aasm_after_commit.rb
# ruby 2.2.2
# activerecord 4.2.3
# aasm 4.2.0
require 'active_record'
require 'aasm'
# Setup a mock AR table
ActiveRecord::Migration.verbose = false
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
View example_controller_with_rails_logger.rb
# app/controllers/health_check_controller.rb
class HealthCheckController < ActionController::Base
def ping
Rails.logger.info("bazbar, request-id: #{request.request_id}")
render json: { success: true, errors: nil, data: 'pong' }, status: :ok
end
end
View custom_logformatter_rails_config.rb
# config/environments/development.rb
Rails.application.configure do
config.log_formatter = LogFormatter.new
end
View logs_lograge_with_log_formatter.rb
{"level":"INFO","progname":null,"message":"bazbar, request_id: 4967a677-ab86-4a10-8a01-ea520951cqw3"}
{"level":"INFO","progname":null,"message":"{\"method\":\"GET\",\"path\":\"/ping\",\"format\":\"*/*\",\"controller\":\"HealthCheckController\",\"action\":\"ping\",\"status\":200,\"duration\":8.36,\"view\":0.22,\"db\":0.0,\"request_time\":\"2020-07-07 16:10:17 +0530\",\"application\":\"MyApplication\",\"process_id\":7869,\"params\":\"{}\",\"rails_env\":\"development\",\"request_id\":\"4967a677-ab86-4a10-8a01-ea520951cqw3\"}"}
View log_formatter.rb
# app/logger/log_formatter.rb
class LogFormatter < ::Logger::Formatter
def call(severity, time, program_name, message)
message = '' if message.blank?
severity = '' if message.blank?
{
level: severity,
progname: program_name,
message: message,
View rails_lograge_logs.json
{"level":"INFO","progname":null,"message":"{\"method\":\"GET\",\"path\":\"/ping\",\"format\":\"*/*\",\"controller\":\"HealthCheckController\",\"action\":\"ping\",\"status\":200,\"duration\":8.36,\"view\":0.22,\"db\":0.0,\"request_time\":\"2020-07-07 16:10:17 +0530\",\"application\":\"MyApplication\",\"process_id\":7869,\"params\":\"{}\",\"rails_env\":\"development\",\"request_id\":\"4967a677-ab86-4a10-8a01-ea520951cf46\"}"}
View sample_curl_rails_app_with_x_request_id.txt
$ curl -I localhost:3000/ping | grep -i "request"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
X-Request-Id: 4967a677-ab86-4a10-8a01-ea520951cf46
View sample_application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: exception
def append_info_to_payload(payload)
super
payload[:host] = request.host
payload[:remote_ip] = request.remote_ip
payload[:ip] = request.ip
end
end
View config_with_lograge.rb
# config/environments/development.rb
Rails.application.configure do
config.lograge.formatter = Lograge::Formatters::Json.new
config.lograge.enabled = true
config.lograge.base_controller_class = ['ActionController::Base']
config.lograge.custom_options = lambda do |event|
{
request_time: Time.now,
application: Rails.application.class.parent_name,
process_id: Process.pid,