-
Notifications
You must be signed in to change notification settings - Fork 35
Refactor route handling and server #78
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
Changes from all commits
41f933e
10520b1
e0d7ac1
26d301a
f33cdf5
e44b857
1f22ae5
0533440
07d0928
f20f82b
1d49a36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,68 +29,73 @@ type AgentData struct { | |
} | ||
|
||
// URL: http://server/ | ||
func handleInfoRequest(handler *serverHandler, w http.ResponseWriter, r *http.Request) { | ||
client := &http.Client{} | ||
func handleInfoRequest(apmServerUrl string) func(w http.ResponseWriter, r *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
client := &http.Client{} | ||
|
||
req, err := http.NewRequest(r.Method, handler.config.apmServerUrl, nil) | ||
//forward every header received | ||
for name, values := range r.Header { | ||
// Loop over all values for the name. | ||
for _, value := range values { | ||
req.Header.Set(name, value) | ||
req, err := http.NewRequest(r.Method, apmServerUrl, nil) | ||
//forward every header received | ||
for name, values := range r.Header { | ||
// Loop over all values for the name. | ||
for _, value := range values { | ||
req.Header.Set(name, value) | ||
} | ||
} | ||
if err != nil { | ||
log.Printf("could not create request object for %s:%s: %v", r.Method, apmServerUrl, err) | ||
return | ||
} | ||
} | ||
if err != nil { | ||
log.Printf("could not create request object for %s:%s: %v", r.Method, handler.config.apmServerUrl, err) | ||
return | ||
} | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
log.Printf("error forwarding info request (`/`) to APM Server: %v", err) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
log.Printf("could not read info request response to APM Server: %v", err) | ||
return | ||
} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
log.Printf("error forwarding info request (`/`) to APM Server: %v", err) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks, I'll change that 👍🏼 |
||
if err != nil { | ||
log.Printf("could not read info request response to APM Server: %v", err) | ||
return | ||
} | ||
|
||
// send status code | ||
w.WriteHeader(resp.StatusCode) | ||
// send status code | ||
w.WriteHeader(resp.StatusCode) | ||
|
||
// send every header received | ||
for name, values := range resp.Header { | ||
// Loop over all values for the name. | ||
for _, value := range values { | ||
w.Header().Add(name, value) | ||
// send every header received | ||
for name, values := range resp.Header { | ||
// Loop over all values for the name. | ||
for _, value := range values { | ||
w.Header().Add(name, value) | ||
} | ||
} | ||
// send body | ||
w.Write([]byte(body)) | ||
} | ||
// send body | ||
w.Write([]byte(body)) | ||
} | ||
|
||
// URL: http://server/intake/v2/events | ||
func handleIntakeV2Events(handler *serverHandler, w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusAccepted) | ||
w.Write([]byte("ok")) | ||
func handleIntakeV2Events(agentDataChan chan AgentData) func(w http.ResponseWriter, r *http.Request) { | ||
|
||
if r.Body == nil { | ||
log.Println("Could not get bytes from agent request body") | ||
return | ||
} | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusAccepted) | ||
w.Write([]byte("ok")) | ||
|
||
rawBytes, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
log.Println("Could not read bytes from agent request body") | ||
return | ||
} | ||
if r.Body == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you encounter the body being nil? I took a look at the docs because I was checking to see if you need to close the request body, and it says:
so I'm not sure if this nil check is required. I suppose it doesn't hurt, though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the docs say it will never be |
||
log.Println("No body in agent request") | ||
return | ||
} | ||
|
||
agentData := AgentData{ | ||
Data: rawBytes, | ||
ContentEncoding: r.Header.Get("Content-Encoding"), | ||
rawBytes, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
log.Println("Could not read bytes from agent request body") | ||
return | ||
} | ||
|
||
agentData := AgentData{ | ||
Data: rawBytes, | ||
ContentEncoding: r.Header.Get("Content-Encoding"), | ||
} | ||
log.Println("Adding agent data to buffer to be sent to apm server") | ||
agentDataChan <- agentData | ||
} | ||
log.Println("Adding agent data to buffer to be sent to apm server") | ||
handler.data <- agentData | ||
} |
Uh oh!
There was an error while loading. Please reload this page.