|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import os |
| 16 | +import sys |
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, |
23 | 26 | ) |
24 | 27 | from opentelemetry.sdk._configuration.models import Resource as ResourceConfig |
25 | 28 | from opentelemetry.sdk.resources import ( |
| 29 | + PROCESS_PID, |
| 30 | + PROCESS_RUNTIME_NAME, |
26 | 31 | SERVICE_NAME, |
27 | 32 | TELEMETRY_SDK_LANGUAGE, |
28 | 33 | TELEMETRY_SDK_NAME, |
@@ -295,3 +300,69 @@ def test_attributes_list_invalid_pair_skipped(self): |
295 | 300 | self.assertEqual(resource.attributes["foo"], "bar") |
296 | 301 | self.assertNotIn("no-equals", resource.attributes) |
297 | 302 | self.assertTrue(any("no-equals" in msg for msg in cm.output)) |
| 303 | + |
| 304 | + |
| 305 | +class TestProcessResourceDetector(unittest.TestCase): |
| 306 | + def _config_with_process(self) -> ResourceConfig: |
| 307 | + return ResourceConfig( |
| 308 | + detection_development=ExperimentalResourceDetection( |
| 309 | + detectors=[ExperimentalResourceDetector(process={})] |
| 310 | + ) |
| 311 | + ) |
| 312 | + |
| 313 | + def test_process_detector_adds_process_attributes(self): |
| 314 | + resource = create_resource(self._config_with_process()) |
| 315 | + self.assertIn(PROCESS_PID, resource.attributes) |
| 316 | + self.assertEqual(resource.attributes[PROCESS_PID], os.getpid()) |
| 317 | + self.assertEqual( |
| 318 | + resource.attributes[PROCESS_RUNTIME_NAME], |
| 319 | + sys.implementation.name, |
| 320 | + ) |
| 321 | + |
| 322 | + def test_process_detector_also_includes_sdk_defaults(self): |
| 323 | + resource = create_resource(self._config_with_process()) |
| 324 | + self.assertEqual(resource.attributes[TELEMETRY_SDK_LANGUAGE], "python") |
| 325 | + self.assertIn(TELEMETRY_SDK_VERSION, resource.attributes) |
| 326 | + |
| 327 | + def test_process_detector_not_run_when_absent(self): |
| 328 | + resource = create_resource(ResourceConfig()) |
| 329 | + self.assertNotIn(PROCESS_PID, resource.attributes) |
| 330 | + |
| 331 | + def test_process_detector_not_run_when_detection_development_is_none(self): |
| 332 | + resource = create_resource(ResourceConfig(detection_development=None)) |
| 333 | + self.assertNotIn(PROCESS_PID, resource.attributes) |
| 334 | + |
| 335 | + def test_process_detector_not_run_when_detectors_list_empty(self): |
| 336 | + config = ResourceConfig( |
| 337 | + detection_development=ExperimentalResourceDetection(detectors=[]) |
| 338 | + ) |
| 339 | + resource = create_resource(config) |
| 340 | + self.assertNotIn(PROCESS_PID, resource.attributes) |
| 341 | + |
| 342 | + def test_explicit_attributes_override_process_detector(self): |
| 343 | + """Config attributes win over detector-provided values.""" |
| 344 | + config = ResourceConfig( |
| 345 | + attributes=[ |
| 346 | + AttributeNameValue( |
| 347 | + name="process.pid", value=99999, type=AttributeType.int |
| 348 | + ) |
| 349 | + ], |
| 350 | + detection_development=ExperimentalResourceDetection( |
| 351 | + detectors=[ExperimentalResourceDetector(process={})] |
| 352 | + ), |
| 353 | + ) |
| 354 | + resource = create_resource(config) |
| 355 | + self.assertEqual(resource.attributes[PROCESS_PID], 99999) |
| 356 | + |
| 357 | + def test_multiple_detector_entries_run_process_once(self): |
| 358 | + """Multiple detector list entries each with process={} should still work.""" |
| 359 | + config = ResourceConfig( |
| 360 | + detection_development=ExperimentalResourceDetection( |
| 361 | + detectors=[ |
| 362 | + ExperimentalResourceDetector(process={}), |
| 363 | + ExperimentalResourceDetector(process={}), |
| 364 | + ] |
| 365 | + ) |
| 366 | + ) |
| 367 | + resource = create_resource(config) |
| 368 | + self.assertEqual(resource.attributes[PROCESS_PID], os.getpid()) |
0 commit comments