-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Change logger to use ASIM format
Adding Current via ActiveSupport::CurrentAttributes for the Asim formatter to get the current user and type. * Enable ASIM logger in dev via ENABLE_ASIM_LOGGER env var
- Loading branch information
1 parent
c04b39b
commit 2d554ee
Showing
8 changed files
with
138 additions
and
6 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
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
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,4 @@ | ||
class Current < ActiveSupport::CurrentAttributes | ||
attribute :user_id | ||
attribute :user_type | ||
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
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,87 @@ | ||
require "time" | ||
require "date" | ||
|
||
class AsimFormatter | ||
def call(data) | ||
end_time = Time.now.utc | ||
start_time = end_time - (data[:duration] || 0) | ||
|
||
{ | ||
"EventCount" => 1, | ||
"EventStartTime" => format_time(start_time), | ||
"EventEndTime" => format_time(end_time), | ||
"EventType" => event_type(data), | ||
"EventResult" => event_result(data), | ||
"EventSeverity" => map_severity(data[:level]), | ||
"EventOriginalSeverity" => data[:level], | ||
"EventSchema" => "ProcessEvent", | ||
"EventSchemaVersion" => "0.1.4", | ||
"EventProduct" => "KAE", | ||
"EventProductVersion" => Rails.version, | ||
"EventVendor" => "Ruby on Rails", | ||
"EventOwner" => data[:owner], | ||
"ActingAppType" => "Ruby on Rails", | ||
"SrcIpAddr" => data[:remote_ip], | ||
"SrcPortNumber" => ENV["PORT"] || 3000, | ||
"IpAddr" => data[:remote_ip], | ||
"SrcUserId" => Current.user_id, | ||
"SrcUserType" => Current.user_type, | ||
"HttpUserAgent" => data[:user_agent], | ||
"Dvc" => ENV["DOMAIN"] || "unknown", | ||
"DvcDomain" => ENV["DOMAIN"] || "unknown", | ||
"AdditionalFields" => { | ||
"RailsLogFormatterAsimVersion" => "1.0.0", | ||
"TraceHeaders" => trace_headers(data), | ||
"RawLog" => data.to_json, | ||
}, | ||
}.to_json | ||
end | ||
|
||
private | ||
|
||
def format_time(time) | ||
time.iso8601 if time | ||
end | ||
|
||
def event_type(data) | ||
controller = data[:controller] | ||
action = data[:action] | ||
if controller && action | ||
"#{controller}##{action}" | ||
else | ||
"UnknownEvent" | ||
end | ||
end | ||
|
||
def event_result(data) | ||
status = data[:status] | ||
if status | ||
(status >= 400) ? "Failure" : "Success" | ||
else | ||
"NA" | ||
end | ||
end | ||
|
||
def map_severity(level) | ||
case level.to_s.upcase | ||
when "DEBUG" | ||
"Informational" | ||
when "INFO" | ||
"Informational" | ||
when "WARN" | ||
"Medium" | ||
when "ERROR" | ||
"High" | ||
when "FATAL" | ||
"High" | ||
else | ||
"Informational" | ||
end | ||
end | ||
|
||
def trace_headers(data) | ||
{ | ||
"X-Request-Id" => data[:request_id], | ||
} | ||
end | ||
end |