Skip to content

Commit

Permalink
Merge pull request #4 from GetDutchie/0.4.0
Browse files Browse the repository at this point in the history
v0.4.0
  • Loading branch information
evan-waters authored Dec 2, 2021
2 parents 6330ce3 + c61c626 commit f23524a
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
.rspec_status
Gemfile.lock
.ruby-version

*.gem
10 changes: 6 additions & 4 deletions lib/twirp/rails.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true
require "twirp/rails/rails_ext/rack/logger"
require "twirp/rails/configuration"
require "twirp/rails/logging/formatters/json"
require "twirp/rails/logging/formatters/key_value"
require "twirp/rails/configurations/logging"
require "twirp/rails/configuration"
require 'twirp/rails/logging/adapter'
require 'twirp/rails/logging/subscriber'
require "twirp/rails/helpers/hooks"
Expand All @@ -24,9 +26,9 @@ def configure(&block)
end

def setup
if configuration.log_twirp_calls
if configuration.log_twirp_calls.is_a?(Proc)
log_twirp_calls!(&configuration.log_twirp_calls)
if configuration.logging.log_twirp_calls
if configuration.logging.log_twirp_calls.is_a?(Proc)
log_twirp_calls!(&configuration.logging.log_twirp_calls)
else
log_twirp_calls!
end
Expand Down
15 changes: 2 additions & 13 deletions lib/twirp/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
module Twirp
module Rails
class Configuration
attr_reader :handlers_paths, :hooks_paths
attr_reader :handlers_paths, :hooks_paths, :logging
attr_writer :log_exceptions, :log_params, :log_twirp_calls

def initialize
@handlers_paths = []
@hooks_paths = []
@logging ||= Twirp::Rails::Configurations::Logging.new
end

def add_handlers_path(path)
Expand All @@ -18,18 +19,6 @@ def add_handlers_path(path)
def add_hooks_path(path)
@hooks_paths << path.to_s
end

def log_twirp_calls
@log_twirp_calls ||= true
end

def log_exceptions
@log_exceptions ||= false
end

def log_params
@log_params ||= false
end
end
end
end
39 changes: 39 additions & 0 deletions lib/twirp/rails/configurations/logging.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

module Twirp
module Rails
module Configurations
class Logging
attr_accessor :log_twirp_calls, :log_exceptions, :log_params

LOG_FORMATTERS = {
json: Twirp::Rails::Logging::Formatters::Json,
key_value: Twirp::Rails::Logging::Formatters::KeyValue
}.freeze

def log_twirp_calls
@log_twirp_calls ||= true
end

def log_exceptions
@log_exceptions ||= false
end

def log_params
@log_params ||= false
end

def log_formatter
@log_formatter ||= LOG_FORMATTERS[:key_value].new
end

def log_formatter=(format)
format = format.to_sym
raise ArgumentError, 'Twirp::Rails defined an invalid log formatter' unless LOG_FORMATTERS.keys.include?(format)

@log_formatter = LOG_FORMATTERS[format].new
end
end
end
end
end
17 changes: 17 additions & 0 deletions lib/twirp/rails/logging/formatters/json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require "json"

module Twirp
module Rails
module Logging
module Formatters
class Json
def call(data)
::JSON.dump(data)
end
end
end
end
end
end
38 changes: 27 additions & 11 deletions lib/twirp/rails/logging/subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,40 @@ def instrumenter(event)

def default_log_writer(event)
twirp_call_info = {
"service" => event.payload[:env][:service].try(:full_name),
"method" => event.payload[:env][:rpc_method],
"path" => event.payload[:rack_env]["REQUEST_PATH"],
"duration" => event.duration
**status_info(event),
**base_log_info(event)
}

if (twerr = event.payload[:twerr])
twirp_call_info["error_code"] = twerr.code
twirp_call_info[:params] = event.payload[:env][:input].to_h if Twirp::Rails.configuration.logging.log_params

if (exception = event.payload[:env][:exception]) && Twirp::Rails.configuration.logging.log_exceptions
twirp_call_info[:exception] = exception
end

twirp_call_info["params"] = event.payload[:env][:input].to_h if Twirp::Rails.configuration.log_params
data = Twirp::Rails.configuration.logging.log_formatter.call(twirp_call_info)
logger.info data
end

if (exception = event.payload[:env][:exception]) && Twirp::Rails.configuration.log_exceptions
twirp_call_info['exception'] = exception
def base_log_info(event)
{
service: event.payload[:env][:service].try(:full_name),
method: event.payload[:env][:rpc_method],
path: event.payload[:rack_env]["REQUEST_PATH"],
time: Time.current.iso8601,
duration: event.duration
}
end

def status_info(event)
status = {}

if (twerr = event.payload[:twerr])
status[:status] = twerr.code&.upcase || "UNKNOWN"
else
status[:status] = "OK"
end

data = Formatters::KeyValue.new.call(twirp_call_info)
logger.info data
status
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twirp/rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Twirp
module Rails
VERSION = "0.3.1"
VERSION = "0.4.0"
end
end

0 comments on commit f23524a

Please sign in to comment.