Repository: elastic/apm-agent-python

Issue:
HOSTNAME value ignored and socket.gethostname() sent
I've trouble in configuring the HOSTNAME, If I set it in the django settings or in the enviorment nothing changes. the `socket.gethostname()` is always the value sent.

**To Reproduce**
- setup project with APM
- add 
```
ELASTIC_APM = {
  ..
    'HOSTNAME': 'myhostname'
...
}
```


**Expected behavior**: 
To see `myhostname` in the host list in kibana

**Environment (please complete the following information)**
- OS: Linux/Mac
- Python version: 3.7
- Framework and version Django 2.2
- APM Server version: 7.5
- Agent version: elastic-apm==5.3.2



**Additional context**

Debugging the code, in the `config` the correct value is there, however the 
socket.gethostname() value is sent



Discussion:
By looking into the code, my guess is that this line: https://github.com/elastic/apm-agent-python/blob/master/elasticapm/base.py#L320

should be
`"hostname": keyword_field(self.config.hostname),`

in the config, the hostname is set as ` hostname = _ConfigValue("HOSTNAME", default=socket.gethostname())` so that the line above is cutting the string to the max length and that's it.
@esseti great catch! I'll open a PR with your suggested fix and a test in a minute

Resolving patch:
diff --git a/elasticapm/base.py b/elasticapm/base.py
--- a/elasticapm/base.py
+++ b/elasticapm/base.py
@@ -36,7 +36,6 @@
 import logging
 import os
 import platform
-import socket
 import sys
 import time
 import warnings
@@ -317,7 +316,7 @@ def get_process_info(self):
 
     def get_system_info(self):
         system_data = {
-            "hostname": keyword_field(socket.gethostname()),
+            "hostname": keyword_field(self.config.hostname),
             "architecture": platform.machine(),
             "platform": platform.system().lower(),
         }


Test patch:
diff --git a/tests/client/client_tests.py b/tests/client/client_tests.py
--- a/tests/client/client_tests.py
+++ b/tests/client/client_tests.py
@@ -34,6 +34,7 @@
 
 import os
 import platform
+import socket
 import sys
 import time
 from collections import defaultdict
@@ -79,6 +80,14 @@ def test_system_info(elasticapm_client):
         mocked.return_value = {}
         system_info = elasticapm_client.get_system_info()
     assert {"hostname", "architecture", "platform"} == set(system_info.keys())
+    assert system_info["hostname"] == socket.gethostname()
+
+
+@pytest.mark.parametrize("elasticapm_client", [{"hostname": "my_custom_hostname"}], indirect=True)
+def test_system_info_hostname_configurable(elasticapm_client):
+    # mock docker/kubernetes data here to get consistent behavior if test is run in docker
+    system_info = elasticapm_client.get_system_info()
+    assert system_info["hostname"] == "my_custom_hostname"
 
 
 @pytest.mark.parametrize("elasticapm_client", [{"global_labels": "az=us-east-1,az.rack=8"}], indirect=True)
@@ -89,14 +98,11 @@ def test_global_labels(elasticapm_client):
 
 def test_docker_kubernetes_system_info(elasticapm_client):
     # mock docker/kubernetes data here to get consistent behavior if test is run in docker
-    with mock.patch("elasticapm.utils.cgroup.get_cgroup_container_metadata") as mock_metadata, mock.patch(
-        "socket.gethostname"
-    ) as mock_gethostname:
+    with mock.patch("elasticapm.utils.cgroup.get_cgroup_container_metadata") as mock_metadata:
         mock_metadata.return_value = {"container": {"id": "123"}, "kubernetes": {"pod": {"uid": "456"}}}
-        mock_gethostname.return_value = "foo"
         system_info = elasticapm_client.get_system_info()
     assert system_info["container"] == {"id": "123"}
-    assert system_info["kubernetes"] == {"pod": {"uid": "456", "name": "foo"}}
+    assert system_info["kubernetes"] == {"pod": {"uid": "456", "name": socket.gethostname()}}
 
 
 @mock.patch.dict(
@@ -164,7 +170,7 @@ def test_docker_kubernetes_system_info_except_hostname_from_environ():
         mock_gethostname.return_value = "foo"
         system_info = elasticapm_client.get_system_info()
     assert "kubernetes" in system_info
-    assert system_info["kubernetes"] == {"pod": {"name": "foo"}, "namespace": "namespace"}
+    assert system_info["kubernetes"] == {"pod": {"name": socket.gethostname()}, "namespace": "namespace"}
 
 
 def test_config_by_environment():