-
Notifications
You must be signed in to change notification settings - Fork 34
Preserve compressed agent data and compress agent data that is not already compressed #72
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4b97826
Preserve compressed agent data and compress agent data that is not al…
estolfo c2dc879
Change name and type of agent data channel
estolfo 917742a
Accidentally removed a test
estolfo f3f1bb6
Use same body variable
estolfo d5d2715
Use BestSpeed compression when sending to APM server
estolfo aff9443
Use io.Pipe instead of separate buffer when compressing data
estolfo c2e479d
Log error if data cannot be compressed
estolfo 4744109
Remove Todo note
estolfo 55bf717
Use err variable
estolfo ea0ef6f
Use ioutil.ReadAll instead for go 1.15
estolfo 2a93da7
Handle all possible errors when reading agent request body
estolfo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you under | ||
| // the Apache License, Version 2.0 (the "License"); you may | ||
| // not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package extension | ||
|
|
||
| import ( | ||
| "compress/gzip" | ||
| "io" | ||
| "io/ioutil" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "gotest.tools/assert" | ||
| ) | ||
|
|
||
| func TestPostToApmServerDataCompressed(t *testing.T) { | ||
| s := "A long time ago in a galaxy far, far away..." | ||
|
|
||
| // Compress the data | ||
| pr, pw := io.Pipe() | ||
| gw, _ := gzip.NewWriterLevel(pw, gzip.BestSpeed) | ||
| go func() { | ||
| gw.Write([]byte(s)) | ||
| gw.Close() | ||
| pw.Close() | ||
| }() | ||
|
|
||
| // Create AgentData struct with compressed data | ||
| data, _ := ioutil.ReadAll(pr) | ||
| agentData := AgentData{Data: data, ContentEncoding: "gzip"} | ||
|
|
||
| // Create apm server and handler | ||
| apmServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| bytes, _ := ioutil.ReadAll(r.Body) | ||
| assert.Equal(t, string(data), string(bytes)) | ||
| assert.Equal(t, "gzip", r.Header.Get("Content-Encoding")) | ||
| w.Write([]byte(`{"foo": "bar"}`)) | ||
| })) | ||
| defer apmServer.Close() | ||
|
|
||
| config := extensionConfig{ | ||
| apmServerUrl: apmServer.URL + "/", | ||
| } | ||
|
|
||
| err := PostToApmServer(agentData, &config) | ||
| assert.Equal(t, nil, err) | ||
| } | ||
|
|
||
| func TestPostToApmServerDataNotCompressed(t *testing.T) { | ||
| s := "A long time ago in a galaxy far, far away..." | ||
| body := []byte(s) | ||
| agentData := AgentData{Data: body, ContentEncoding: ""} | ||
|
|
||
| // Compress the data, so it can be compared with what | ||
| // the apm server receives | ||
| pr, pw := io.Pipe() | ||
| gw, _ := gzip.NewWriterLevel(pw, gzip.BestSpeed) | ||
| go func() { | ||
| gw.Write(body) | ||
| gw.Close() | ||
| pw.Close() | ||
| }() | ||
|
|
||
| // Create apm server and handler | ||
| apmServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| request_bytes, _ := ioutil.ReadAll(r.Body) | ||
| compressed_bytes, _ := ioutil.ReadAll(pr) | ||
| assert.Equal(t, string(compressed_bytes), string(request_bytes)) | ||
| assert.Equal(t, "gzip", r.Header.Get("Content-Encoding")) | ||
| w.Write([]byte(`{"foo": "bar"}`)) | ||
| })) | ||
| defer apmServer.Close() | ||
|
|
||
| config := extensionConfig{ | ||
| apmServerUrl: apmServer.URL + "/", | ||
| } | ||
|
|
||
| err := PostToApmServer(agentData, &config) | ||
| assert.Equal(t, nil, err) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[minor suggestion]
IIUC, this function will only be executed once the request has been established and is then streamed (via chunked-encoding) to the server.
If that's correct, the message may be a bit misleading as the more common source of failure is that there's a network issue when streaming the data to APM Server.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are actually not streaming data yet, but rather sending in batches. At this point in the code, the actual failure is that the data could not be compressed in a go routine. If compression failed in that go routine, we will also get an error that is logged on line 52 (
failed to create a new request when posting to APM server) and the function will return. So I think the error message here is accurate and should stay as-is.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this streams the batches. See also https://medium.com/stupid-gopher-tricks/streaming-data-in-go-without-buffering-3285ddd2a1e5#2342
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're right that the actual send could be chunked by the go internal transport code, I thought you were referring to the way that we send the data as it's received from the agent. But at this point in the code, I'm calling
io.Copy, which ends up calling Write on thegzipWriterbefore it gets to the network code. So any compression failures would be returned byio.Copy. Network errors would be returned by resp, err := client.Do(req).But that actually brings up another point: the error returned from
client.Do(req)actually wraps the compression error, which means we don't really need to do anything with the error returned fromio.Copy. In other words, we could removing the logging of the error and just rely on the error returned in this lineThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I'm understanding from the blog that I linked is that
io.Copyblocks until thePipeReaderis being read from. This happens whenclient.Do(req)is invoked and the data from thePipeReaderis written into the HTTP request body via chunked encoding. It's becauseio.Copyblocks that we have to execute it in a concurrently running goroutine. If we didn't do that, we'd never get to theclient.Do(req)part.See also the docs for
io.Pipe