|
33 | 33 | HOST_NAME, |
34 | 34 | PROCESS_PID, |
35 | 35 | PROCESS_RUNTIME_NAME, |
| 36 | + SERVICE_INSTANCE_ID, |
36 | 37 | SERVICE_NAME, |
37 | 38 | TELEMETRY_SDK_LANGUAGE, |
38 | 39 | TELEMETRY_SDK_NAME, |
@@ -307,6 +308,79 @@ def test_attributes_list_invalid_pair_skipped(self): |
307 | 308 | self.assertTrue(any("no-equals" in msg for msg in cm.output)) |
308 | 309 |
|
309 | 310 |
|
| 311 | +class TestServiceResourceDetector(unittest.TestCase): |
| 312 | + @staticmethod |
| 313 | + def _config_with_service() -> ResourceConfig: |
| 314 | + return ResourceConfig( |
| 315 | + detection_development=ExperimentalResourceDetection( |
| 316 | + detectors=[ExperimentalResourceDetector(service={})] |
| 317 | + ) |
| 318 | + ) |
| 319 | + |
| 320 | + def test_service_detector_adds_instance_id(self): |
| 321 | + resource = create_resource(self._config_with_service()) |
| 322 | + self.assertIn(SERVICE_INSTANCE_ID, resource.attributes) |
| 323 | + |
| 324 | + def test_service_instance_id_is_unique_per_call(self): |
| 325 | + r1 = create_resource(self._config_with_service()) |
| 326 | + r2 = create_resource(self._config_with_service()) |
| 327 | + self.assertNotEqual( |
| 328 | + r1.attributes[SERVICE_INSTANCE_ID], |
| 329 | + r2.attributes[SERVICE_INSTANCE_ID], |
| 330 | + ) |
| 331 | + |
| 332 | + def test_service_detector_reads_otel_service_name_env_var(self): |
| 333 | + with patch.dict(os.environ, {"OTEL_SERVICE_NAME": "my-service"}): |
| 334 | + resource = create_resource(self._config_with_service()) |
| 335 | + self.assertEqual(resource.attributes[SERVICE_NAME], "my-service") |
| 336 | + |
| 337 | + def test_service_detector_no_env_var_leaves_default_service_name(self): |
| 338 | + with patch.dict(os.environ, {}, clear=True): |
| 339 | + resource = create_resource(self._config_with_service()) |
| 340 | + self.assertEqual(resource.attributes[SERVICE_NAME], "unknown_service") |
| 341 | + |
| 342 | + def test_explicit_service_name_overrides_env_var(self): |
| 343 | + """Config attributes win over the service detector's env-var value.""" |
| 344 | + config = ResourceConfig( |
| 345 | + attributes=[ |
| 346 | + AttributeNameValue(name="service.name", value="explicit-svc") |
| 347 | + ], |
| 348 | + detection_development=ExperimentalResourceDetection( |
| 349 | + detectors=[ExperimentalResourceDetector(service={})] |
| 350 | + ), |
| 351 | + ) |
| 352 | + with patch.dict(os.environ, {"OTEL_SERVICE_NAME": "env-svc"}): |
| 353 | + resource = create_resource(config) |
| 354 | + self.assertEqual(resource.attributes[SERVICE_NAME], "explicit-svc") |
| 355 | + |
| 356 | + def test_service_detector_not_run_when_absent(self): |
| 357 | + resource = create_resource(ResourceConfig()) |
| 358 | + self.assertNotIn(SERVICE_INSTANCE_ID, resource.attributes) |
| 359 | + |
| 360 | + def test_service_detector_not_run_when_detection_development_is_none(self): |
| 361 | + resource = create_resource(ResourceConfig(detection_development=None)) |
| 362 | + self.assertNotIn(SERVICE_INSTANCE_ID, resource.attributes) |
| 363 | + |
| 364 | + def test_service_detector_also_includes_sdk_defaults(self): |
| 365 | + resource = create_resource(self._config_with_service()) |
| 366 | + self.assertEqual(resource.attributes[TELEMETRY_SDK_LANGUAGE], "python") |
| 367 | + self.assertIn(TELEMETRY_SDK_VERSION, resource.attributes) |
| 368 | + |
| 369 | + def test_included_filter_limits_service_attributes(self): |
| 370 | + config = ResourceConfig( |
| 371 | + detection_development=ExperimentalResourceDetection( |
| 372 | + detectors=[ExperimentalResourceDetector(service={})], |
| 373 | + attributes=IncludeExclude(included=["service.instance.id"]), |
| 374 | + ) |
| 375 | + ) |
| 376 | + with patch.dict(os.environ, {"OTEL_SERVICE_NAME": "my-service"}): |
| 377 | + resource = create_resource(config) |
| 378 | + self.assertIn(SERVICE_INSTANCE_ID, resource.attributes) |
| 379 | + # service.name comes from the filter-excluded detector output, but the |
| 380 | + # default "unknown_service" is still added by create_resource directly |
| 381 | + self.assertEqual(resource.attributes[SERVICE_NAME], "unknown_service") |
| 382 | + |
| 383 | + |
310 | 384 | class TestHostResourceDetector(unittest.TestCase): |
311 | 385 | @staticmethod |
312 | 386 | def _config_with_host() -> ResourceConfig: |
|
0 commit comments