|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import os |
| 16 | +import socket |
16 | 17 | import unittest |
17 | 18 | from unittest.mock import patch |
18 | 19 |
|
19 | 20 | from opentelemetry.sdk._configuration._resource import create_resource |
20 | 21 | from opentelemetry.sdk._configuration.models import ( |
21 | 22 | AttributeNameValue, |
22 | 23 | AttributeType, |
| 24 | + ExperimentalResourceDetection, |
| 25 | + ExperimentalResourceDetector, |
| 26 | + IncludeExclude, |
23 | 27 | ) |
24 | 28 | from opentelemetry.sdk._configuration.models import Resource as ResourceConfig |
25 | 29 | from opentelemetry.sdk.resources import ( |
| 30 | + HOST_ARCH, |
| 31 | + HOST_NAME, |
26 | 32 | SERVICE_NAME, |
27 | 33 | TELEMETRY_SDK_LANGUAGE, |
28 | 34 | TELEMETRY_SDK_NAME, |
@@ -295,3 +301,73 @@ def test_attributes_list_invalid_pair_skipped(self): |
295 | 301 | self.assertEqual(resource.attributes["foo"], "bar") |
296 | 302 | self.assertNotIn("no-equals", resource.attributes) |
297 | 303 | self.assertTrue(any("no-equals" in msg for msg in cm.output)) |
| 304 | + |
| 305 | + |
| 306 | +class TestHostResourceDetector(unittest.TestCase): |
| 307 | + def _config_with_host(self) -> ResourceConfig: |
| 308 | + return ResourceConfig( |
| 309 | + detection_development=ExperimentalResourceDetection( |
| 310 | + detectors=[ExperimentalResourceDetector(host={})] |
| 311 | + ) |
| 312 | + ) |
| 313 | + |
| 314 | + def test_host_detector_adds_host_attributes(self): |
| 315 | + resource = create_resource(self._config_with_host()) |
| 316 | + self.assertIn(HOST_NAME, resource.attributes) |
| 317 | + self.assertEqual(resource.attributes[HOST_NAME], socket.gethostname()) |
| 318 | + self.assertIn(HOST_ARCH, resource.attributes) |
| 319 | + |
| 320 | + def test_host_detector_also_includes_sdk_defaults(self): |
| 321 | + resource = create_resource(self._config_with_host()) |
| 322 | + self.assertEqual(resource.attributes[TELEMETRY_SDK_LANGUAGE], "python") |
| 323 | + self.assertIn(TELEMETRY_SDK_VERSION, resource.attributes) |
| 324 | + |
| 325 | + def test_host_detector_not_run_when_absent(self): |
| 326 | + resource = create_resource(ResourceConfig()) |
| 327 | + self.assertNotIn(HOST_NAME, resource.attributes) |
| 328 | + self.assertNotIn(HOST_ARCH, resource.attributes) |
| 329 | + |
| 330 | + def test_host_detector_not_run_when_detection_development_is_none(self): |
| 331 | + resource = create_resource(ResourceConfig(detection_development=None)) |
| 332 | + self.assertNotIn(HOST_NAME, resource.attributes) |
| 333 | + |
| 334 | + def test_host_detector_not_run_when_detectors_list_empty(self): |
| 335 | + config = ResourceConfig( |
| 336 | + detection_development=ExperimentalResourceDetection(detectors=[]) |
| 337 | + ) |
| 338 | + resource = create_resource(config) |
| 339 | + self.assertNotIn(HOST_NAME, resource.attributes) |
| 340 | + |
| 341 | + def test_explicit_attributes_override_host_detector(self): |
| 342 | + config = ResourceConfig( |
| 343 | + attributes=[ |
| 344 | + AttributeNameValue(name="host.name", value="custom-host") |
| 345 | + ], |
| 346 | + detection_development=ExperimentalResourceDetection( |
| 347 | + detectors=[ExperimentalResourceDetector(host={})] |
| 348 | + ), |
| 349 | + ) |
| 350 | + resource = create_resource(config) |
| 351 | + self.assertEqual(resource.attributes[HOST_NAME], "custom-host") |
| 352 | + |
| 353 | + def test_included_filter_limits_host_attributes(self): |
| 354 | + config = ResourceConfig( |
| 355 | + detection_development=ExperimentalResourceDetection( |
| 356 | + detectors=[ExperimentalResourceDetector(host={})], |
| 357 | + attributes=IncludeExclude(included=["host.name"]), |
| 358 | + ) |
| 359 | + ) |
| 360 | + resource = create_resource(config) |
| 361 | + self.assertIn(HOST_NAME, resource.attributes) |
| 362 | + self.assertNotIn(HOST_ARCH, resource.attributes) |
| 363 | + |
| 364 | + def test_excluded_filter_removes_host_attributes(self): |
| 365 | + config = ResourceConfig( |
| 366 | + detection_development=ExperimentalResourceDetection( |
| 367 | + detectors=[ExperimentalResourceDetector(host={})], |
| 368 | + attributes=IncludeExclude(excluded=["host.name"]), |
| 369 | + ) |
| 370 | + ) |
| 371 | + resource = create_resource(config) |
| 372 | + self.assertNotIn(HOST_NAME, resource.attributes) |
| 373 | + self.assertIn(HOST_ARCH, resource.attributes) |
0 commit comments