-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
183 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require "signalman/base_handler" | ||
|
||
module Signalman | ||
class ActionHandler < BaseHandler | ||
def process | ||
headers = {} | ||
event.payload.fetch(:headers, {}).each do |name, value| | ||
headers[name] = value if name.start_with?("HTTP") | ||
headers[name] = value if ActionDispatch::Http::Headers::CGI_VARIABLES.include?(name) | ||
|
||
[ | ||
"action_dispatch.request_id" | ||
].each do |header_name| | ||
headers[name] = value if name == header_name | ||
end | ||
end | ||
|
||
create_event event.payload.slice( | ||
:method, | ||
:path, | ||
:controller, | ||
:action, | ||
:params, | ||
:format, | ||
:status, | ||
:db_runtime, | ||
:view_runtime | ||
).merge(headers: headers) | ||
end | ||
|
||
def skip? | ||
event.payload[:controller].start_with?("Signalman::") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module Signalman | ||
class BaseHandler | ||
attr_reader :current_time, :event | ||
|
||
def self.call(event) | ||
current_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) | ||
new(event, current_time).start | ||
end | ||
|
||
def initialize(event, current_time) | ||
@event, @current_time = event, current_time | ||
end | ||
|
||
def start | ||
process unless skip? | ||
end | ||
|
||
def process | ||
create_event | ||
end | ||
|
||
def skip? | ||
false | ||
end | ||
|
||
def create_event(payload = nil) | ||
payload ||= event.payload | ||
|
||
Event.create( | ||
name: event.name, | ||
started_at: started_at, | ||
finished_at: finished_at, | ||
duration: event.duration, | ||
payload: payload | ||
) | ||
rescue ActiveRecord::StatementInvalid => e | ||
Rails.logger.error "[Signalman] #{e.message}" | ||
end | ||
|
||
# Time measure since system boot with | ||
# Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) | ||
def started_at | ||
Time.current - ((current_time - event.time) / 1_000) | ||
end | ||
|
||
def finished_at | ||
Time.current - ((current_time - event.end) / 1_000) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require "signalman/base_handler" | ||
|
||
module Signalman | ||
class JobHandler < BaseHandler | ||
def process | ||
job = event.payload[:job] | ||
create_event( | ||
class: job.class.name, | ||
id: job.job_id, | ||
enqueued_at: job.enqueued_at, | ||
scheduled_at: scheduled_at(event), | ||
queue_name: queue_name(event), | ||
args: args_info(job) | ||
) | ||
end | ||
|
||
# From ActiveJob::LogSubscriber | ||
def queue_name(event) | ||
# Rails 7.1 -> ActiveJob.adapter_name(event.payload[:adapter]) + "(#{event.payload[:job].queue_name})" | ||
event.payload[:adapter].class.name.demodulize.remove("Adapter") + "(#{event.payload[:job].queue_name})" | ||
end | ||
|
||
def args_info(job) | ||
if job.class.log_arguments? && job.arguments.any? | ||
" with arguments: " + | ||
job.arguments.map { |arg| format(arg).inspect }.join(", ") | ||
else | ||
"" | ||
end | ||
end | ||
|
||
def format(arg) | ||
case arg | ||
when Hash | ||
arg.transform_values { |value| format(value) } | ||
when Array | ||
arg.map { |value| format(value) } | ||
when GlobalID::Identification | ||
begin | ||
arg.to_global_id | ||
rescue | ||
arg | ||
end | ||
else | ||
arg | ||
end | ||
end | ||
|
||
def scheduled_at(event) | ||
return unless event.payload[:job].scheduled_at | ||
Time.at(event.payload[:job].scheduled_at).utc | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "signalman/base_handler" | ||
|
||
module Signalman | ||
class MailHandler < BaseHandler | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require "signalman/base_handler" | ||
|
||
module Signalman | ||
class QueryHandler < BaseHandler | ||
IGNORED_QUERIES = [ | ||
"SCHEMA", | ||
"TRANSACTION", | ||
"ActiveRecord::SchemaMigration", | ||
"ActiveRecord::InternalMetadata", | ||
/^Signalman/ | ||
] | ||
|
||
# CREATE_TABLE queries have nil for `name` | ||
def skip? | ||
return if event.payload[:name].blank? | ||
IGNORED_QUERIES.any? { |q| q.match? event.payload[:name] } | ||
end | ||
|
||
def process | ||
create_event event.payload.except(:connection) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require "signalman/base_handler" | ||
|
||
module Signalman | ||
class ViewHandler < BaseHandler | ||
def skip? | ||
event.payload[:identifier].include?("app/views/signalman/") | ||
end | ||
end | ||
end |