Skip to content

Commit

Permalink
Add a resource detector for populating service.instance.id
Browse files Browse the repository at this point in the history
  • Loading branch information
pyohannes committed Jul 16, 2024
1 parent a67f5f8 commit acde88b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#3991](https://github.com/open-telemetry/opentelemetry-python/pull/3991))
- Add attributes field in `MeterProvider.get_meter` and `InstrumentationScope`
([#4015](https://github.com/open-telemetry/opentelemetry-python/pull/4015))
- Added a `opentelemetry.sdk.resources.ServiceInstanceIdResourceDetector` that
adds the 'service.instance.id' resource attribute

## Version 1.25.0/0.46b0 (2024-05-30)

Expand Down
1 change: 1 addition & 0 deletions opentelemetry-sdk/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ console = "opentelemetry.sdk.trace.export:ConsoleSpanExporter"
[project.entry-points.opentelemetry_resource_detector]
otel = "opentelemetry.sdk.resources:OTELResourceDetector"
process = "opentelemetry.sdk.resources:ProcessResourceDetector"
serviceinstanceid = "opentelemetry.sdk.resources:ServiceInstanceIdResourceDetector"

[project.urls]
Homepage = "https://github.com/open-telemetry/opentelemetry-python/tree/main/opentelemetry-sdk"
Expand Down
7 changes: 7 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
from json import dumps
from os import environ
from urllib import parse
import uuid

from opentelemetry.attributes import BoundedAttributes
from opentelemetry.sdk.environment_variables import (
Expand Down Expand Up @@ -371,6 +372,12 @@ def detect(self) -> "Resource":
return Resource(resource_info)


class ServiceInstanceIdResourceDetector(ResourceDetector):
# pylint: disable=no-self-use
def detect(self) -> "Resource":
return Resource({SERVICE_INSTANCE_ID: str(uuid.uuid4())})


def get_aggregated_resources(
detectors: typing.List["ResourceDetector"],
initial_resource: typing.Optional[Resource] = None,
Expand Down
27 changes: 27 additions & 0 deletions opentelemetry-sdk/tests/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@
PROCESS_RUNTIME_NAME,
PROCESS_RUNTIME_VERSION,
SERVICE_NAME,
SERVICE_INSTANCE_ID,
TELEMETRY_SDK_LANGUAGE,
TELEMETRY_SDK_NAME,
TELEMETRY_SDK_VERSION,
OTELResourceDetector,
ProcessResourceDetector,
Resource,
ResourceDetector,
ServiceInstanceIdResourceDetector,
get_aggregated_resources,
)

Expand Down Expand Up @@ -611,6 +613,19 @@ def test_process_detector(self):
tuple(sys.argv),
)

def test_service_instance_id_detector(self):
initial_resource = Resource({})
aggregated_resource = get_aggregated_resources(
[ServiceInstanceIdResourceDetector()], initial_resource
)

self.assertIn(
SERVICE_INSTANCE_ID,
aggregated_resource.attributes.keys())

# This throws if service instance id is not a valid UUID.
uuid.UUID(aggregated_resource.attributes[SERVICE_INSTANCE_ID])

def test_resource_detector_entry_points_default(self):
resource = Resource({}).create()

Expand Down Expand Up @@ -723,3 +738,15 @@ def test_resource_detector_entry_points_otel(self):
)
self.assertIn(PROCESS_RUNTIME_VERSION, resource.attributes.keys())
self.assertEqual(resource.schema_url, "")
with patch.dict(
environ,
{
OTEL_EXPERIMENTAL_RESOURCE_DETECTORS: "serviceinstanceid",
},
clear=True,
):
resource = Resource({}).create()
self.assertIn(SERVICE_INSTANCE_ID, resource.attributes.keys())

# This throws if service instance id is not a valid UUID.
uuid.UUID(aggregated_resource.attributes[SERVICE_INSTANCE_ID])

0 comments on commit acde88b

Please sign in to comment.