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 5 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))
- Added a `opentelemetry.sdk.resources.ServiceInstanceIdResourceDetector` that
adds the 'service.instance.id' resource attribute
pyohannes marked this conversation as resolved.
Show resolved Hide resolved

## 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
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,12 @@ def detect(self) -> "Resource":
return Resource(resource_info)


class ServiceInstanceIdResourceDetector(ResourceDetector):
# pylint: disable=no-self-use
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?

return Resource({SERVICE_INSTANCE_ID: str(uuid.uuid4())})
xrmx marked this conversation as resolved.
Show resolved Hide resolved


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 @@ -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,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(resource.attributes[SERVICE_INSTANCE_ID])
Loading