From eac39ffdb8975da4385b558013d7d44484f3cd00 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 15 Feb 2025 20:42:58 +0100 Subject: [PATCH] daemon/logger/splunk: remove some intermediate variables Remove intermediate variables or move them closer to where they're used, as this function has various early returns on errors. Signed-off-by: Sebastiaan van Stijn --- daemon/logger/splunk/splunk.go | 41 +++++++++++++--------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/daemon/logger/splunk/splunk.go b/daemon/logger/splunk/splunk.go index c2c01c70ad..0b240a9cef 100644 --- a/daemon/logger/splunk/splunk.go +++ b/daemon/logger/splunk/splunk.go @@ -227,27 +227,8 @@ func New(info logger.Info) (logger.Logger, error) { } } - transport := &http.Transport{ - TLSClientConfig: tlsConfig, - Proxy: http.ProxyFromEnvironment, - } - client := &http.Client{ - Transport: transport, - } - - source := info.Config[splunkSourceKey] - sourceType := info.Config[splunkSourceTypeKey] - index := info.Config[splunkIndexKey] - - nullMessage := &splunkMessage{ - Host: hostname, - Source: source, - SourceType: sourceType, - Index: index, - } - // Allow user to remove tag from the messages by setting tag to empty string - tag := "" + var tag string if tagTemplate, ok := info.Config[tagKey]; !ok || tagTemplate != "" { tag, err = loggerutils.ParseLogTag(info, loggerutils.DefaultTemplate) if err != nil { @@ -267,12 +248,22 @@ func New(info logger.Info) (logger.Logger, error) { streamChannelSize = getAdvancedOptionInt(envVarStreamChannelSize, defaultStreamChannelSize) ) + transport := &http.Transport{ + TLSClientConfig: tlsConfig, + Proxy: http.ProxyFromEnvironment, + } + splLogger := &splunkLogger{ - client: client, - transport: transport, - url: splunkURL.String(), - auth: "Splunk " + splunkToken, - nullMessage: nullMessage, + client: &http.Client{Transport: transport}, + transport: transport, + url: splunkURL.String(), + auth: "Splunk " + splunkToken, + nullMessage: &splunkMessage{ + Host: hostname, + Source: info.Config[splunkSourceKey], + SourceType: info.Config[splunkSourceTypeKey], + Index: info.Config[splunkIndexKey], + }, gzipCompression: gzipCompression, gzipCompressionLevel: gzipCompressionLevel, stream: make(chan *splunkMessage, streamChannelSize),