Skip to content

Commit 6a01bb0

Browse files
committed
Add user-agent to http transport headers
1 parent e989439 commit 6a01bb0

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

elasticapm/transport/http_base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3131
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3232

33+
from elasticapm.base import Client
3334
from elasticapm.conf import constants
3435
from elasticapm.transport.base import Transport
3536
from elasticapm.utils import compat
37+
from elasticapm.version import VERSION
3638

3739

3840
class HTTPTransportBase(Transport):
@@ -63,6 +65,7 @@ def __init__(
6365
self._config_url = "".join((base, constants.AGENT_CONFIG_PATH, tail))
6466
self._server_info_url = "".join((base, constants.SERVER_INFO_PATH, tail))
6567
super(HTTPTransportBase, self).__init__(client, compress_level=compress_level, **kwargs)
68+
self._headers["User-Agent"] = get_user_agent(self.client)
6669

6770
def send(self, data):
6871
"""
@@ -98,5 +101,16 @@ def auth_headers(self):
98101
return {}
99102

100103

104+
def get_user_agent(client: Client) -> str:
105+
"""
106+
Compiles the user agent, which will be added as a header to all requests
107+
to the APM Server
108+
"""
109+
if client.config.service_version:
110+
return "apm-agent-python/{} ({} {})".format(VERSION, client.config.service_name, client.config.service_version)
111+
else:
112+
return "apm-agent-python/{} ({})".format(VERSION, client.config.service_name)
113+
114+
101115
# left for backwards compatibility
102116
AsyncHTTPTransportBase = HTTPTransportBase

0 commit comments

Comments
 (0)