PERF: Cache `hostname` in `DiscourseLogstashLogger` (#27442)

Logging events happen all the time. No need for us to keep running code
to fetch the hostname when it doesn't change between deploys.
This commit is contained in:
Alan Guo Xiang Tan 2024-06-12 13:34:07 +08:00 committed by GitHub
parent 30f369fffe
commit be4f1e3350
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 4 deletions

View File

@ -3,21 +3,22 @@
require "logstash-logger" require "logstash-logger"
class DiscourseLogstashLogger class DiscourseLogstashLogger
def self.logger(uri:, type:) def self.hostname
# See Discourse.os_hostname @hostname ||=
hostname =
begin begin
require "socket" require "socket"
Socket.gethostname Socket.gethostname
rescue => e rescue => e
`hostname`.chomp `hostname`.chomp
end end
end
def self.logger(uri:, type:)
LogStashLogger.new( LogStashLogger.new(
uri: uri, uri: uri,
sync: true, sync: true,
customize_event: ->(event) do customize_event: ->(event) do
event["hostname"] = hostname event["hostname"] = self.hostname
event["severity_name"] = event["severity"] event["severity_name"] = event["severity"]
event["severity"] = Object.const_get("Logger::Severity::#{event["severity"]}") event["severity"] = Object.const_get("Logger::Severity::#{event["severity"]}")
event["type"] = type event["type"] = type