From e28793cff13a964f3e081a132e87c2757b97a20a Mon Sep 17 00:00:00 2001 From: Joshua Powers Date: Tue, 10 May 2022 15:24:01 -0600 Subject: [PATCH] fix: re-init azure monitor http client on context deadline error (#11030) --- .../outputs/azure_monitor/azure_monitor.go | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/plugins/outputs/azure_monitor/azure_monitor.go b/plugins/outputs/azure_monitor/azure_monitor.go index 6b0637cac..462f75cf0 100644 --- a/plugins/outputs/azure_monitor/azure_monitor.go +++ b/plugins/outputs/azure_monitor/azure_monitor.go @@ -3,12 +3,14 @@ package azure_monitor import ( "bytes" "compress/gzip" + "context" "encoding/binary" "encoding/json" "fmt" "hash/fnv" "io" "net/http" + "net/url" "regexp" "strings" "time" @@ -109,12 +111,7 @@ func (a *AzureMonitor) Connect() error { a.Timeout = config.Duration(defaultRequestTimeout) } - a.client = &http.Client{ - Transport: &http.Transport{ - Proxy: http.ProxyFromEnvironment, - }, - Timeout: time.Duration(a.Timeout), - } + a.initHTTPClient() var err error var region string @@ -168,6 +165,15 @@ func (a *AzureMonitor) Connect() error { return nil } +func (a *AzureMonitor) initHTTPClient() { + a.client = &http.Client{ + Transport: &http.Transport{ + Proxy: http.ProxyFromEnvironment, + }, + Timeout: time.Duration(a.Timeout), + } +} + // vmMetadata retrieves metadata about the current Azure VM func vmInstanceMetadata(c *http.Client) (region string, resourceID string, err error) { req, err := http.NewRequest("GET", vmInstanceMetadataURL, nil) @@ -313,6 +319,10 @@ func (a *AzureMonitor) send(body []byte) error { resp, err := a.client.Do(req) if err != nil { + if err.(*url.Error).Unwrap() == context.DeadlineExceeded { + a.initHTTPClient() + } + return err } defer resp.Body.Close()