Skip to content

Commit df01c07

Browse files
committed
use the configured hostname in get_system_info (#689)
* use the configured hostname in get_system_info fixes #688 * fix kubernetes tests and improve system_info test Unfortunately, we can't use mock to "hardcode" the output of socket.gethostname() anymore, as it is read when the Config class is defined, which happens pretty early when initializing the test suite. As such, we have to compare the returned value dynamically with socket.gethostname() in the test.
1 parent b22cb50 commit df01c07

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

elasticapm/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import logging
3636
import os
3737
import platform
38-
import socket
3938
import sys
4039
import time
4140
import warnings
@@ -330,7 +329,7 @@ def get_process_info(self):
330329

331330
def get_system_info(self):
332331
system_data = {
333-
"hostname": keyword_field(socket.gethostname()),
332+
"hostname": keyword_field(self.config.hostname),
334333
"architecture": platform.machine(),
335334
"platform": platform.system().lower(),
336335
}

tests/client/client_tests.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import os
3636
import platform
37+
import socket
3738
import sys
3839
import time
3940
from collections import defaultdict
@@ -79,6 +80,14 @@ def test_system_info(elasticapm_client):
7980
mocked.return_value = {}
8081
system_info = elasticapm_client.get_system_info()
8182
assert {"hostname", "architecture", "platform"} == set(system_info.keys())
83+
assert system_info["hostname"] == socket.gethostname()
84+
85+
86+
@pytest.mark.parametrize("elasticapm_client", [{"hostname": "my_custom_hostname"}], indirect=True)
87+
def test_system_info_hostname_configurable(elasticapm_client):
88+
# mock docker/kubernetes data here to get consistent behavior if test is run in docker
89+
system_info = elasticapm_client.get_system_info()
90+
assert system_info["hostname"] == "my_custom_hostname"
8291

8392

8493
@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):
8998

9099
def test_docker_kubernetes_system_info(elasticapm_client):
91100
# mock docker/kubernetes data here to get consistent behavior if test is run in docker
92-
with mock.patch("elasticapm.utils.cgroup.get_cgroup_container_metadata") as mock_metadata, mock.patch(
93-
"socket.gethostname"
94-
) as mock_gethostname:
101+
with mock.patch("elasticapm.utils.cgroup.get_cgroup_container_metadata") as mock_metadata:
95102
mock_metadata.return_value = {"container": {"id": "123"}, "kubernetes": {"pod": {"uid": "456"}}}
96-
mock_gethostname.return_value = "foo"
97103
system_info = elasticapm_client.get_system_info()
98104
assert system_info["container"] == {"id": "123"}
99-
assert system_info["kubernetes"] == {"pod": {"uid": "456", "name": "foo"}}
105+
assert system_info["kubernetes"] == {"pod": {"uid": "456", "name": socket.gethostname()}}
100106

101107

102108
@mock.patch.dict(
@@ -164,7 +170,7 @@ def test_docker_kubernetes_system_info_except_hostname_from_environ():
164170
mock_gethostname.return_value = "foo"
165171
system_info = elasticapm_client.get_system_info()
166172
assert "kubernetes" in system_info
167-
assert system_info["kubernetes"] == {"pod": {"name": "foo"}, "namespace": "namespace"}
173+
assert system_info["kubernetes"] == {"pod": {"name": socket.gethostname()}, "namespace": "namespace"}
168174

169175

170176
def test_config_by_environment():

0 commit comments

Comments
 (0)