Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stop recreating http transports on each info request #260

Merged
merged 1 commit into from
Aug 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions apm-lambda-extension/apmproxy/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ type AgentData struct {
// StartHttpServer starts the server listening for APM agent data.
func (c *Client) StartReceiver() error {
mux := http.NewServeMux()
mux.HandleFunc("/", c.handleInfoRequest())

handleInfoRequest, err := c.handleInfoRequest()
if err != nil {
return err
}

mux.HandleFunc("/", handleInfoRequest)
mux.HandleFunc("/intake/v2/events", c.handleIntakeV2Events())

c.receiver.Handler = mux
Expand Down Expand Up @@ -68,27 +74,26 @@ func (c *Client) Shutdown() error {
}

// URL: http://server/
func (c *Client) handleInfoRequest() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
extension.Log.Debug("Handling APM server Info Request")
func (c *Client) handleInfoRequest() (func(w http.ResponseWriter, r *http.Request), error) {
// Init reverse proxy
parsedApmServerUrl, err := url.Parse(c.serverURL)
if err != nil {
return nil, fmt.Errorf("could not parse APM server URL: %w", err)
}

// Init reverse proxy
parsedApmServerUrl, err := url.Parse(c.serverURL)
if err != nil {
extension.Log.Errorf("could not parse APM server URL: %v", err)
return
}
reverseProxy := httputil.NewSingleHostReverseProxy(parsedApmServerUrl)

reverseProxy := httputil.NewSingleHostReverseProxy(parsedApmServerUrl)
customTransport := http.DefaultTransport.(*http.Transport).Clone()
customTransport.ResponseHeaderTimeout = c.dataForwarderTimeout
reverseProxy.Transport = customTransport

customTransport := http.DefaultTransport.(*http.Transport).Clone()
customTransport.ResponseHeaderTimeout = c.dataForwarderTimeout
reverseProxy.Transport = customTransport
reverseProxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
c.SetApmServerTransportState(r.Context(), Failing)
extension.Log.Errorf("Error querying version from the APM server: %v", err)
}

reverseProxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
c.SetApmServerTransportState(r.Context(), Failing)
extension.Log.Errorf("Error querying version from the APM server: %v", err)
}
return func(w http.ResponseWriter, r *http.Request) {
extension.Log.Debug("Handling APM server Info Request")

// Process request (the Golang doc suggests removing any pre-existing X-Forwarded-For header coming
// from the client or an untrusted proxy to prevent IP spoofing : https://pkg.go.dev/net/http/httputil#ReverseProxy
Expand All @@ -102,7 +107,7 @@ func (c *Client) handleInfoRequest() func(w http.ResponseWriter, r *http.Request

// Forward request to the APM server
reverseProxy.ServeHTTP(w, r)
}
}, nil
}

// URL: http://server/intake/v2/events
Expand Down