Skip to content

Commit 4b7334a

Browse files
committed
feat(sdk): add service.instance.id to default resource
The Python SDK did not auto-generate service.instance.id, unlike the Java SDK and the stable semantic convention recommendation. Add it to _DEFAULT_RESOURCE so every process gets a unique instance identity at startup without any user configuration.
1 parent 28b6852 commit 4b7334a

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
from types import ModuleType
7373
from typing import List, Optional, Set, cast
7474
from urllib import parse
75+
from uuid import uuid4
7576

7677
from opentelemetry.attributes import BoundedAttributes
7778
from opentelemetry.sdk.environment_variables import (
@@ -323,6 +324,7 @@ def to_json(self, indent: Optional[int] = 4) -> str:
323324
TELEMETRY_SDK_LANGUAGE: "python",
324325
TELEMETRY_SDK_NAME: "opentelemetry",
325326
TELEMETRY_SDK_VERSION: _OPENTELEMETRY_SDK_VERSION,
327+
SERVICE_INSTANCE_ID: str(uuid4()),
326328
}
327329
)
328330

opentelemetry-sdk/tests/resources/test_resources.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
PROCESS_RUNTIME_DESCRIPTION,
4646
PROCESS_RUNTIME_NAME,
4747
PROCESS_RUNTIME_VERSION,
48+
SERVICE_INSTANCE_ID,
4849
SERVICE_NAME,
4950
TELEMETRY_SDK_LANGUAGE,
5051
TELEMETRY_SDK_NAME,
@@ -88,6 +89,9 @@ def test_create(self):
8889
TELEMETRY_SDK_LANGUAGE: "python",
8990
TELEMETRY_SDK_VERSION: _OPENTELEMETRY_SDK_VERSION,
9091
SERVICE_NAME: "unknown_service",
92+
SERVICE_INSTANCE_ID: _DEFAULT_RESOURCE.attributes[
93+
SERVICE_INSTANCE_ID
94+
],
9195
}
9296

9397
resource = Resource.create(attributes)
@@ -211,6 +215,9 @@ def test_immutability(self):
211215
TELEMETRY_SDK_LANGUAGE: "python",
212216
TELEMETRY_SDK_VERSION: _OPENTELEMETRY_SDK_VERSION,
213217
SERVICE_NAME: "unknown_service",
218+
SERVICE_INSTANCE_ID: _DEFAULT_RESOURCE.attributes[
219+
SERVICE_INSTANCE_ID
220+
],
214221
}
215222

216223
attributes_copy = attributes.copy()

0 commit comments

Comments
 (0)