newrelic: log more request context.

This commit is contained in:
evazion
2019-08-08 22:16:39 -05:00
parent 9a6add9730
commit df433bda9c
2 changed files with 30 additions and 0 deletions

View File

@@ -9,4 +9,33 @@ class DanbooruLogger
::NewRelic::Agent.notice_error(exception, expected: expected, custom_params: params)
end
end
def self.initialize(request, session, user)
add_attributes("request.params", request.params)
add_attributes("session.params", session.to_h)
add_attributes("user", { id: user.id, name: user.name, level: user.level_string, ip: request.remote_ip })
end
def self.add_attributes(prefix, hash)
return unless defined?(::NewRelic)
attributes = flatten_hash(hash).transform_keys { |key| "#{prefix}.#{key}" }
::NewRelic::Agent.add_custom_attributes(attributes)
end
private
# flatten_hash({ foo: { bar: { baz: 42 } } })
# => { "foo.bar.baz" => 42 }
def self.flatten_hash(hash)
hash.each_with_object({}) do |(k, v), h|
if v.is_a?(Hash)
flatten_hash(v).map do|h_k, h_v|
h["#{k}.#{h_k}"] = h_v
end
else
h[k.to_s] = v
end
end
end
end

View File

@@ -29,6 +29,7 @@ class SessionLoader
update_last_ip_addr
set_time_zone
CurrentUser.user.unban! if CurrentUser.user.ban_expired?
DanbooruLogger.initialize(request, session, CurrentUser.user)
end
private