Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a resource detector for populating service.instance.id #4061

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
- Add `ServiceInstanceIdResourceDetector` that adds the 'service.instance.id' resource attribute
([#4061](https://github.com/open-telemetry/opentelemetry-python/pull/4061))

## 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
12 changes: 12 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import os
import sys
import typing
import uuid
from json import dumps
from os import environ
from urllib import parse
Expand Down Expand Up @@ -371,6 +372,17 @@ def detect(self) -> "Resource":
return Resource(resource_info)


class ServiceInstanceIdResourceDetector(ResourceDetector):
# pylint: disable=no-self-use
_instance_id_cache = {}

def detect(self) -> "Resource":
Copy link
Contributor

@lzchen lzchen Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to simply wait until service.instance.id get added as stable and include them in OtelResourceDetector? Are there plans to include them as part of "Semantic Attributes with SDK-provided Default Value"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there plans to include them as part of "Semantic Attributes with SDK-provided Default Value"

Yes, see open-telemetry/semantic-conventions#311.

Any reason not to simply wait until service.instance.id get added as stable and include them in OtelResourceDetector?

One prerequisite for adding service.instance.id as stable are working prototypes and implementation in several languages. Having such an implementation (or at least prototype) in Python would be beneficial for those stabilization efforts, especially as the description of service.instance.id explicitly mentions application servers like gunicorn.

Showing that service.instance.id can be populated according to the conventions for all major usage scenarios in Python would be a prerequisite for declaring it stable and making it a default attribute.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, see open-telemetry/semantic-conventions#311.

Having such an implementation (or at least prototype) in Python would be beneficial for those stabilization efforts

If there are plans to make them default doesn't that mean they would move into OtelResourceDetector eventually? If we create a new resource detector (even though it might remain experimental) it sounds like we would have to support this forever. Maybe I might be misunderstanding what "prototype" is, will this just be temporary to get the specs to move along?

instance_id = self._instance_id_cache.setdefault(
os.getpid(), str(uuid.uuid4())
)
return Resource({SERVICE_INSTANCE_ID: instance_id})


def get_aggregated_resources(
detectors: typing.List["ResourceDetector"],
initial_resource: typing.Optional[Resource] = None,
Expand Down
35 changes: 35 additions & 0 deletions opentelemetry-sdk/tests/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
PROCESS_RUNTIME_DESCRIPTION,
PROCESS_RUNTIME_NAME,
PROCESS_RUNTIME_VERSION,
SERVICE_INSTANCE_ID,
SERVICE_NAME,
TELEMETRY_SDK_LANGUAGE,
TELEMETRY_SDK_NAME,
Expand All @@ -49,6 +50,7 @@
ProcessResourceDetector,
Resource,
ResourceDetector,
ServiceInstanceIdResourceDetector,
get_aggregated_resources,
)

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

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

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

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

other_resource = get_aggregated_resources(
[ServiceInstanceIdResourceDetector()], Resource({})
)

# The instance id should be stable across invocations of the detector
# in the same process.
self.assertEqual(
resource.attributes[SERVICE_INSTANCE_ID],
other_resource.attributes[SERVICE_INSTANCE_ID],
)

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

Expand Down Expand Up @@ -723,3 +746,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(resource.attributes[SERVICE_INSTANCE_ID])