Skip to content

Commit

Permalink
Emit key=value logs in production.
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharydenton authored and aptos-bot committed May 11, 2022
1 parent a7fae8f commit 6ae769a
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions ecosystem/platform/server/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ gem 'tailwindcss-rails', '~> 2.0'
# Monitoring
gem 'sentry-rails'
gem 'sentry-ruby'
gem 'lograge', '~> 0.12.0'

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
Expand Down
8 changes: 8 additions & 0 deletions ecosystem/platform/server/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ GEM
llhttp-ffi (0.4.0)
ffi-compiler (~> 1.0)
rake (~> 13.0)
lograge (0.12.0)
actionpack (>= 4)
activesupport (>= 4)
railties (>= 4)
request_store (~> 1.0)
loofah (2.17.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand Down Expand Up @@ -323,6 +328,8 @@ GEM
regexp_parser (2.3.1)
reline (0.3.1)
io-console (~> 0.5)
request_store (1.5.1)
rack (>= 1.4)
responders (3.0.1)
actionpack (>= 5.0)
railties (>= 5.0)
Expand Down Expand Up @@ -452,6 +459,7 @@ DEPENDENCIES
jbuilder
kaminari
maxmind-geoip2
lograge (~> 0.12.0)
omniauth
omniauth-discord
omniauth-github
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
# Copyright (c) Aptos
# SPDX-License-Identifier: Apache-2.0

require 'logging/logs'

class ApplicationController < ActionController::Base
include Logging::Logs

before_action :set_csrf_cookie
before_action :set_logging_metadata

protect_from_forgery with: :exception

Expand Down Expand Up @@ -35,4 +40,17 @@ def admin_access_denied(_exception)
def ensure_confirmed!
redirect_to onboarding_email_path unless current_user.confirmed?
end

def append_info_to_payload(payload)
super
# Add metadata to lograge request logs.
payload[:request_id] = request.request_id
payload[:user_id] = current_user&.id
end

def set_logging_metadata
# Add metadata to thread local for Logging::Logs.log().
Thread.current.thread_variable_set(REQUEST_ID_KEY, request.request_id)
Thread.current.thread_variable_set(USER_ID_KEY, current_user&.id)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def create
end

if @it1_profile.save
log @it1_profile, 'created'
validate_node(v, do_location: true)
if @it1_profile.validator_verified?
redirect_to it1_path, notice: 'AIT1 application completed successfully: your node is verified!'
Expand All @@ -64,6 +65,7 @@ def update

ip_changed = @it1_profile.validator_ip_changed?
if @it1_profile.update(it1_profile_params)
log @it1_profile, 'updated'
if @it1_profile.validator_verified? && !@it1_profile.needs_revalidation?
redirect_to it1_path,
notice: 'AIT1 node information updated' and return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

# Copyright (c) Aptos
# SPDX-License-Identifier: Apache-2.0
# frozen_string_literal: true

class OnboardingController < ApplicationController
before_action :authenticate_user!
Expand All @@ -13,6 +14,7 @@ def email_update

email_params = params.require(:user).permit(:email, :username)
if verify_recaptcha(model: current_user) && current_user.update(email_params)
log current_user, 'email updated'
current_user.send_confirmation_instructions
redirect_to onboarding_email_path, notice: "Verification email sent to #{email_params[:email]}"
else
Expand Down
16 changes: 16 additions & 0 deletions ecosystem/platform/server/config/initializers/lograge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

# Copyright (c) Aptos
# SPDX-License-Identifier: Apache-2.0

Rails.application.configure do
config.lograge.enabled = !Rails.env.development? || ENV.fetch('LOGRAGE_IN_DEVELOPMENT', nil) == 'true'

config.lograge.custom_options = lambda do |event|
result = {}
result[:time] = Time.now.to_i
result[:request_id] = event.payload[:request_id]
result[:user_id] = event.payload[:user_id] if event.payload[:user_id].present?
result
end
end
36 changes: 36 additions & 0 deletions ecosystem/platform/server/lib/logging/logs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

# Copyright (c) Aptos
# SPDX-License-Identifier: Apache-2.0

module Logging
module Logs
REQUEST_ID_KEY = 'request_id'
USER_ID_KEY = 'user_id'

def log(message_or_object, message = nil)
result = []
result << "time=#{Time.now.to_i}"
result << "class=#{self.class}"

request_id = Thread.current.thread_variable_get(REQUEST_ID_KEY)
result << "request_id=#{request_id}"

user_id = Thread.current.thread_variable_get(USER_ID_KEY)
result << "user_id=#{user_id}" if user_id.present?

if message.nil?
message = message_or_object
else
object = message_or_object
result << "object_class=#{object.class}"
result << "object_id=#{object.id}" if object.respond_to?(:id)
end

message = message.gsub('"', '\"')
result << "message=\"#{message}\""

Rails.logger.info(result.join(' '))
end
end
end

0 comments on commit 6ae769a

Please sign in to comment.