github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/acceptor/http_client.go (about) 1 // (c) Copyright IBM Corp. 2021 2 // (c) Copyright Instana Inc. 2020 3 4 package acceptor 5 6 import ( 7 "errors" 8 "net/http" 9 "net/url" 10 "os" 11 "time" 12 ) 13 14 // ErrMalformedProxyURL is returned by NewHTTPClient() when the INSTANA_ENDPOINT_PROXY= env var contains 15 // a malformed URL string 16 var ErrMalformedProxyURL = errors.New("malformed URL value found in INSTANA_ENDPOINT_PROXY, ignoring") 17 18 // NewHTTPClient returns an http.Client configured for sending requests to the Instana serverless acceptor. 19 // If INSTANA_ENDPOINT_PROXY= env var is populated with a vaild URL, the returned client will use it as an 20 // HTTP proxy. If the value is malformed, this setting is ignored and ErrMalformedProxyURL error is returned. 21 // The returned http.Client instance in this case is usable, but does not use a proxy to connect to the acceptor. 22 func NewHTTPClient(timeout time.Duration) (*http.Client, error) { 23 client := &http.Client{} 24 25 if timeout > 0 { 26 client.Timeout = timeout 27 } 28 29 proxy := os.Getenv("INSTANA_ENDPOINT_PROXY") 30 if proxy == "" { 31 return client, nil 32 } 33 34 proxyURL, err := url.Parse(proxy) 35 if err != nil { 36 return client, ErrMalformedProxyURL 37 } 38 39 client.Transport = &http.Transport{ 40 Proxy: http.ProxyURL(proxyURL), 41 } 42 43 return client, nil 44 }