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

View File

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