Skip to content

Commit

Permalink
wip: add klogger
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcooke committed Feb 1, 2024
1 parent 409e884 commit a7b2b72
Show file tree
Hide file tree
Showing 21 changed files with 167 additions and 178 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ gem "highline", require: false
gem "execjs", '~> 2.7', "< 2.8"
gem "jwt"
gem "kaminari"
gem "klogger-logger"
gem "mail"
gem "moonrope"
gem "mysql2"
Expand Down
11 changes: 6 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ GEM
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
klogger-logger (1.3.1)
concurrent-ruby (>= 1.0, < 2.0)
json
rouge (>= 3.30, < 5.0)
loofah (2.19.1)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand All @@ -156,7 +160,6 @@ GEM
marcel (1.0.2)
method_source (1.0.0)
mini_mime (1.1.2)
mini_portile2 (2.8.1)
minitest (5.18.0)
moonrope (2.0.2)
deep_merge (~> 1.0)
Expand All @@ -177,9 +180,6 @@ GEM
activerecord (>= 4.0.0)
activesupport (>= 4.0.0)
nio4r (2.7.0)
nokogiri (1.14.3)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
nokogiri (1.14.3-arm64-darwin)
racc (~> 1.4)
nokogiri (1.14.3-x86_64-linux)
Expand Down Expand Up @@ -225,6 +225,7 @@ GEM
regexp_parser (2.7.0)
resolv (0.2.2)
rexml (3.2.5)
rouge (4.2.0)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
Expand Down Expand Up @@ -310,7 +311,6 @@ GEM

PLATFORMS
arm64-darwin-22
ruby
x86_64-linux

DEPENDENCIES
Expand Down Expand Up @@ -339,6 +339,7 @@ DEPENDENCIES
jquery-rails
jwt
kaminari
klogger-logger
mail
moonrope
mysql2
Expand Down
63 changes: 32 additions & 31 deletions app/models/webhook_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,41 +51,42 @@ def queue
end

def deliver
logger = Postal.logger_for(:webhooks)
payload = { event: event, timestamp: created_at.to_f, payload: self.payload, uuid: uuid }.to_json
logger.info "[#{id}] Sending webhook request to `#{url}`"
result = Postal::HTTP.post(url, sign: true, json: payload, timeout: 5)
self.attempts += 1
self.retry_after = RETRIES[self.attempts]&.from_now
server.message_db.webhooks.record(
event: event,
url: url,
webhook_id: webhook_id,
attempt: self.attempts,
timestamp: Time.now.to_f,
payload: self.payload.to_json,
uuid: uuid,
status_code: result[:code],
body: result[:body],
will_retry: (retry_after ? 0 : 1)
)
Postal.logger.tagged(event: event, url: url, component: "webhooks") do
Postal.logger.info "Sending webhook request"
result = Postal::HTTP.post(url, sign: true, json: payload, timeout: 5)
self.attempts += 1
self.retry_after = RETRIES[self.attempts]&.from_now
server.message_db.webhooks.record(
event: event,
url: url,
webhook_id: webhook_id,
attempt: self.attempts,
timestamp: Time.now.to_f,
payload: self.payload.to_json,
uuid: uuid,
status_code: result[:code],
body: result[:body],
will_retry: (retry_after ? 0 : 1)
)

if result[:code] >= 200 && result[:code] < 300
logger.info "[#{id}] -> Received #{result[:code]} status code. That's OK."
destroy
webhook&.update_column(:last_used_at, Time.now)
true
else
logger.error "[#{id}] -> Received #{result[:code]} status code. That's not OK."
self.error = "Couldn't send to URL. Code received was #{result[:code]}"
if retry_after
logger.info "[#{id}] -> Will retry #{retry_after} (this was attempt #{self.attempts})"
save
else
logger.info "[#{id}] -> Have tried #{self.attempts} times. Giving up."
if result[:code] >= 200 && result[:code] < 300
Postal.logger.info "Received #{result[:code]} status code. That's OK."
destroy
webhook&.update_column(:last_used_at, Time.now)
true
else
Postal.logger.error "Received #{result[:code]} status code. That's not OK."
self.error = "Couldn't send to URL. Code received was #{result[:code]}"
if retry_after
Postal.logger.info "Will retry #{retry_after} (this was attempt #{self.attempts})"
save
else
Postal.logger.info "Have tried #{self.attempts} times. Giving up."
destroy
end
false
end
false
end
end

Expand Down
6 changes: 4 additions & 2 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ class Application < Rails::Application
require "postal/tracking_middleware"
config.middleware.insert_before ActionDispatch::HostAuthorization, Postal::TrackingMiddleware

config.logger = Postal.logger_for(:rails)

config.hosts << Postal.config.web.host

if Postal.config.logging.rails_log == false
config.logger = Logger.new("/dev/null")
end

end
end
2 changes: 1 addition & 1 deletion config/cron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Clockwork

configure do |config|
config[:tz] = "UTC"
config[:logger] = Postal.logger_for(:cron)
config[:logger] = Postal.logger
end

every 1.minute, "every-1-minutes" do
Expand Down
6 changes: 0 additions & 6 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@
# require 'syslog/logger'
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')

if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
end
51 changes: 51 additions & 0 deletions config/initializers/logging.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
begin
def add_exception_to_payload(payload, event)
return unless exception = event.payload[:exception_object]

payload[:exception_class] = exception.class.name
payload[:exception_message] = exception.message
payload[:exception_backtrace] = exception.backtrace[0, 4].join("\n")
end

ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args|
event = ActiveSupport::Notifications::Event.new(*args)

payload = {
event: "request",
transaction: event.transaction_id,
controller: event.payload[:controller],
action: event.payload[:action],
format: event.payload[:format],
method: event.payload[:method],
path: event.payload[:path],
request_id: event.payload[:request].request_id,
ip_address: event.payload[:request].ip,
status: event.payload[:status],
view_runtime: event.payload[:view_runtime],
db_runtime: event.payload[:db_runtime]
}

add_exception_to_payload(payload, event)

string = "#{payload[:method]} #{payload[:path]} (#{payload[:status]})"

if payload[:exception_class]
Postal.logger.error(string, **payload)
else
Postal.logger.info(string, **payload)
end
end

ActiveSupport::Notifications.subscribe "deliver.action_mailer" do |*args|
event = ActiveSupport::Notifications::Event.new(*args)

Postal.logger.info({
event: "send_email",
transaction: event.transaction_id,
message_id: event.payload[:message_id],
subject: event.payload[:subject],
from: event.payload[:from],
to: event.payload[:to].is_a?(Array) ? event.payload[:to].join(", ") : event.payload[:to].to_s
})
end
end
1 change: 1 addition & 0 deletions config/initializers/smtp.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "postal/config"

if Postal.config&.smtp
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = { address: Postal.config.smtp.host, user_name: Postal.config.smtp.username, password: Postal.config.smtp.password, port: Postal.config.smtp.port || 25 }
Expand Down
6 changes: 2 additions & 4 deletions config/postal.defaults.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ rabbitmq:
tls_ca_certificates: <%= ENV.fetch('RABBITMQ_TLS_CA_CERTIFICATES', '/etc/ssl/certs/ca-certificates.crt'.split(',').inspect) %>

logging:
stdout: <%= ENV.fetch('LOGGING_STDOUT', 'false') %>
root: <%= ENV.fetch('LOGGING_ROOT', '') %>
max_log_file_size: <%= ENV.fetch('LOGGING_MAX_LOG_FILES', '20') %>
max_log_files: <%= ENV.fetch('LOGGING_MAX_LOG_FILES', '10') %>
rails_log: <%= ENV.fetch('LOGGING_RAILS_LOG', 'false') %>
graylog:
host: <%= ENV.fetch('GRAYLOG_HOST', '') %>
port: <%= ENV.fetch('GRAYLOG_PORT', '12201') %>
facility: <%= ENV.fetch('GRAYLOG_FACILITY', 'postal') %>

workers:
threads: <%= ENV.fetch('WORKER_THREADS', '4') %>
Expand Down
3 changes: 0 additions & 3 deletions docker/ci-config/postal.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ web_server:
smtp_server:
port: 2525

logging:
stdout: false

main_db:
host: mariadb
username: root
Expand Down
62 changes: 0 additions & 62 deletions lib/postal/app_logger.rb

This file was deleted.

40 changes: 22 additions & 18 deletions lib/postal/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
require_relative "version"

module Postal
module Config
end

def self.host
@host ||= config.web.host || "localhost:5000"
Expand Down Expand Up @@ -42,14 +44,6 @@ def self.config_root
end
end

def self.log_root
if config.logging.root
@log_root ||= Pathname.new(config.logging.root)
else
@log_root ||= app_root.join("log")
end
end

def self.config_file_path
if env == "default"
@config_file_path ||= File.join(config_root, "postal.yml")
Expand Down Expand Up @@ -86,16 +80,11 @@ def self.defaults
end
end

def self.logger_for(name)
@loggers ||= {}
@loggers[name.to_sym] ||= begin
require "postal/app_logger"
if config.logging.stdout || ENV["LOG_TO_STDOUT"]
Postal::AppLogger.new(name, STDOUT)
else
FileUtils.mkdir_p(log_root)
Postal::AppLogger.new(name, log_root.join("#{name}.log"), config.logging.max_log_files, config.logging.max_log_file_size.megabytes)
end
def self.logger
@logger ||= begin
k = Klogger.new(nil, destination: Rails.env.test? ? "/dev/null" : $stdout, highlight: Rails.env.development?)
k.add_destination(graylog_logging_destination) if config.logging.graylog.host.present?
k
end
end

Expand Down Expand Up @@ -180,4 +169,19 @@ def self.ip_pools?
config.general.use_ip_pools?
end

def self.graylog_logging_destination
@graylog_destination ||= begin
notifier = GELF::Notifier.new(config.logging.graylog.host, config.logging.graylog.port, "WAN")
proc do |_logger, payload, group_ids|
short_message = payload.delete(:message) || "[message missing]"
notifier.notify!(short_message: short_message, **{
facility: config.logging.graylog.facility,
_environment: Rails.env.to_s,
_version: Postal::VERSION.to_s,
_group_ids: group_ids.join(" ")
}.merge(payload.transform_keys { |k| "_#{k}".to_sym }.transform_values(&:to_s)))
end
end
end

end
2 changes: 1 addition & 1 deletion lib/postal/http_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def send_message(message)
private

def log(text)
Postal.logger_for(:http_sender).info("[#{@log_id}] #{text}")
Postal.logger.info text, id: @log_id, component: "http-sender"
end

def parameters(message, options = {})
Expand Down
Loading

0 comments on commit a7b2b72

Please sign in to comment.