Skip to content
5 changes: 5 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@ endif::[]

=== Unreleased

// Unreleased changes go here
// When the next release happens, nest these changes under the "Python Agent version 6.x" heading
//[float]
//===== Features


[float]
===== Bug fixes

* Fix an issue where compressed spans would count against `transaction_max_spans` {pull}1377[#1377]
* Make sure HTTP connections are not re-used after a process fork {pull}1374[#1374]
* Update the `User-Agent` header to the new https://github.com/elastic/apm/pull/514[spec] {pull}1378[#1378]


[[release-notes-6.x]]
=== Python Agent version 6.x
Expand Down
14 changes: 13 additions & 1 deletion elasticapm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import logging
import os
import platform
import re
import sys
import threading
import time
Expand Down Expand Up @@ -135,7 +136,7 @@ def __init__(self, config=None, **inline):
headers = {
"Content-Type": "application/x-ndjson",
"Content-Encoding": "gzip",
"User-Agent": "elasticapm-python/%s" % elasticapm.VERSION,
"User-Agent": self.get_user_agent(),
}

transport_kwargs = {
Expand Down Expand Up @@ -419,6 +420,17 @@ def get_cloud_info(self):
self.logger.warning("Unknown value for CLOUD_PROVIDER, skipping cloud metadata: {}".format(provider))
return {}

def get_user_agent(self) -> str:
"""
Compiles the user agent, which will be added as a header to all requests
to the APM Server
"""
if self.config.service_version:
service_version = re.sub(r"[^\t _\x21-\x27\x2a-\x5b\x5d-\x7e\x80-\xff]", "_", self.config.service_version)
return "apm-agent-python/{} ({} {})".format(elasticapm.VERSION, self.config.service_name, service_version)
else:
return "apm-agent-python/{} ({})".format(elasticapm.VERSION, self.config.service_name)

def build_metadata(self):
data = {
"service": self.get_service_info(),
Expand Down
15 changes: 14 additions & 1 deletion tests/client/client_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def test_send(sending_elasticapm_client):
"Content-Type": "application/x-ndjson",
"Content-Encoding": "gzip",
"Authorization": "Bearer %s" % sending_elasticapm_client.config.secret_token,
"User-Agent": "elasticapm-python/%s" % elasticapm.VERSION,
"User-Agent": "apm-agent-python/%s (myapp)" % elasticapm.VERSION,
}
seen_headers = dict(request.headers)
for k, v in expected_headers.items():
Expand Down Expand Up @@ -867,3 +867,16 @@ def test_backdating_transaction(elasticapm_client):
elasticapm_client.end_transaction()
transaction = elasticapm_client.events[TRANSACTION][0]
assert 1000 < transaction["duration"] < 2000


@pytest.mark.parametrize(
"elasticapm_client,expected",
[
({"service_version": "v2"}, " v2"),
({"service_version": "v2 \x00"}, " v2 _"),
({}, ""),
],
indirect=["elasticapm_client"],
)
def test_user_agent(elasticapm_client, expected):
assert elasticapm_client.get_user_agent() == "apm-agent-python/unknown (myapp{})".format(expected)