Skip to content

Commit

Permalink
only use content-type and content-encoding headers for POSTing events (
Browse files Browse the repository at this point in the history
…#1651)

* only use content-type and content-encoding headers for POSTing events

closes #1634

* fix test
  • Loading branch information
beniwohli authored Oct 3, 2022
1 parent 0f3ec98 commit 21980d8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
2 changes: 0 additions & 2 deletions elasticapm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ def __init__(self, config=None, **inline):
logging.setLogRecordFactory(new_factory)

headers = {
"Content-Type": "application/x-ndjson",
"Content-Encoding": "gzip",
"User-Agent": self.get_user_agent(),
}

Expand Down
9 changes: 7 additions & 2 deletions elasticapm/transport/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ def send(self, data, forced_flush=False):

headers = self._headers.copy() if self._headers else {}
headers.update(self.auth_headers)
headers.update(
{
b"Content-Type": b"application/x-ndjson",
b"Content-Encoding": b"gzip",
}
)

url = self._url
if forced_flush:
Expand Down Expand Up @@ -146,7 +152,6 @@ def get_config(self, current_version=None, keys=None):
data = json_encoder.dumps(keys).encode("utf-8")
headers = self._headers.copy()
headers[b"Content-Type"] = "application/json"
headers.pop(b"Content-Encoding", None) # remove gzip content-encoding header
headers.update(self.auth_headers)
max_age = 300
if current_version:
Expand Down Expand Up @@ -190,7 +195,7 @@ def _process_queue(self):
def fetch_server_info(self):
headers = self._headers.copy() if self._headers else {}
headers.update(self.auth_headers)
headers["accept"] = "text/plain"
headers[b"accept"] = b"text/plain"
try:
response = self.http.urlopen("GET", self._server_info_url, headers=headers, timeout=self._timeout)
body = response.data
Expand Down
21 changes: 18 additions & 3 deletions tests/transports/test_urllib3.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,20 @@

@pytest.mark.flaky(reruns=3) # test is flaky on Windows
def test_send(waiting_httpserver, elasticapm_client):
elasticapm_client.server_version = (8, 0) # avoid making server_info request
waiting_httpserver.serve_content(code=202, content="", headers={"Location": "http://example.com/foo"})
transport = Transport(waiting_httpserver.url, client=elasticapm_client)
transport = Transport(
waiting_httpserver.url, client=elasticapm_client, headers=elasticapm_client._transport._headers
)
transport.start_thread()
try:
url = transport.send("x".encode("latin-1"))
assert url == "http://example.com/foo"
request_headers = waiting_httpserver.requests[0].headers
assert request_headers["User-Agent"].startswith("apm-agent-python/")
assert request_headers["Authorization"] == "Bearer test_key"
assert request_headers["Content-Type"] == "application/x-ndjson"
assert request_headers["Content-Encoding"] == "gzip"
finally:
transport.close()

Expand Down Expand Up @@ -277,7 +285,7 @@ def test_get_config(waiting_httpserver, elasticapm_client):
transport = Transport(
url + "/" + constants.EVENTS_API_PATH,
client=elasticapm_client,
headers={"Content-Type": "application/x-ndjson", "Content-Encoding": "gzip"},
headers=elasticapm_client._transport._headers,
)
version, data, max_age = transport.get_config("1", {})
assert version == "2"
Expand Down Expand Up @@ -380,9 +388,16 @@ def test_fetch_server_info(waiting_httpserver, elasticapm_client):
content=b'{"version": "8.0.0-alpha1"}',
)
url = waiting_httpserver.url
transport = Transport(url + "/" + constants.EVENTS_API_PATH, client=elasticapm_client)
transport = Transport(
url + "/" + constants.EVENTS_API_PATH, client=elasticapm_client, headers=elasticapm_client._transport._headers
)
transport.fetch_server_info()
assert elasticapm_client.server_version == (8, 0, 0, "alpha1")
request_headers = waiting_httpserver.requests[0].headers
assert request_headers["User-Agent"].startswith("apm-agent-python/")
assert "Authorization" in request_headers
assert "Content-Type" not in request_headers
assert "Content-Encoding" not in request_headers


def test_fetch_server_info_no_json(waiting_httpserver, caplog, elasticapm_client):
Expand Down

0 comments on commit 21980d8

Please sign in to comment.