Skip to content

Commit 4b5b5ef

Browse files
opentelemetry-sdk: update declarative config to use ServiceInstanceIdResourceDetector (#5299)
* opentelemetry-sdk: update declarative config to use ServiceInstanceIdResourceDetector * add changelog fragment --------- Co-authored-by: Mike Goldsmith <goldsmith.mike@gmail.com>
1 parent 7e4043c commit 4b5b5ef

3 files changed

Lines changed: 7 additions & 7 deletions

File tree

.changelog/5299.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
`opentelemetry-sdk`: update declarative config to use `ServiceInstanceIdResourceDetector`

opentelemetry-sdk/src/opentelemetry/sdk/_configuration/_resource.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import fnmatch
88
import logging
99
import os
10-
import uuid
1110
from collections.abc import Callable
1211
from typing import Any
1312
from urllib import parse
@@ -23,10 +22,10 @@
2322
from opentelemetry.sdk.resources import (
2423
_DEFAULT_RESOURCE,
2524
OTEL_SERVICE_NAME,
26-
SERVICE_INSTANCE_ID,
2725
SERVICE_NAME,
2826
ProcessResourceDetector,
2927
Resource,
28+
ServiceInstanceIdResourceDetector,
3029
_HostResourceDetector,
3130
)
3231
from opentelemetry.util.types import AttributeValue
@@ -140,9 +139,9 @@ def create_resource(config: ResourceConfig | None) -> Resource:
140139

141140
def _detect_service(_config: Any) -> dict[str, AttributeValue]:
142141
"""Service detector: generates instance ID and reads OTEL_SERVICE_NAME."""
143-
attrs: dict[str, AttributeValue] = {
144-
SERVICE_INSTANCE_ID: str(uuid.uuid4()),
145-
}
142+
attrs: dict[str, AttributeValue] = dict(
143+
ServiceInstanceIdResourceDetector().detect().attributes
144+
)
146145
if service_name := os.environ.get(OTEL_SERVICE_NAME):
147146
attrs[SERVICE_NAME] = service_name
148147
return attrs

opentelemetry-sdk/tests/_configuration/test_resource.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,10 @@ def test_service_detector_adds_instance_id(self):
311311
resource = create_resource(self._config_with_service())
312312
self.assertIn(SERVICE_INSTANCE_ID, resource.attributes)
313313

314-
def test_service_instance_id_is_unique_per_call(self):
314+
def test_service_instance_id_is_stable_within_process(self):
315315
r1 = create_resource(self._config_with_service())
316316
r2 = create_resource(self._config_with_service())
317-
self.assertNotEqual(
317+
self.assertEqual(
318318
r1.attributes[SERVICE_INSTANCE_ID],
319319
r2.attributes[SERVICE_INSTANCE_ID],
320320
)

0 commit comments

Comments
 (0)