Skip to content

Commit

Permalink
Implement 'process.runtime.*' resource detector (#2660)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michele Mancioppi committed May 9, 2022
1 parent 606d535 commit f367ec2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2653](https://github.com/open-telemetry/opentelemetry-python/pull/2653))
- Add variadic arguments to metric exporter/reader interfaces
([#2654](https://github.com/open-telemetry/opentelemetry-python/pull/2654))
- Added a `opentelemetry.sdk.resources.ProcessResourceDetector` that adds the
'process.runtime.{name,version,description}' resource attributes when used
with the `opentelemetry.sdk.resources.get_aggregated_resources` API
([#2660](https://github.com/open-telemetry/opentelemetry-python/pull/2660))
- Move Metrics API behind internal package
([#2651](https://github.com/open-telemetry/opentelemetry-python/pull/2651))

Expand Down
23 changes: 23 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import concurrent.futures
import logging
import os
import sys
import typing
from json import dumps

Expand Down Expand Up @@ -286,6 +287,28 @@ def detect(self) -> "Resource":
return Resource(env_resource_map)


class ProcessResourceDetector(ResourceDetector):
# pylint: disable=no-self-use
def detect(self) -> "Resource":
_runtime_version = ".".join(
map(
str,
sys.version_info[:3]
if sys.version_info.releaselevel == "final"
and not sys.version_info.serial
else sys.version_info,
)
)

return Resource(
{
PROCESS_RUNTIME_DESCRIPTION: sys.version,
PROCESS_RUNTIME_NAME: sys.implementation.name,
PROCESS_RUNTIME_VERSION: _runtime_version,
}
)


def get_aggregated_resources(
detectors: typing.List["ResourceDetector"],
initial_resource: typing.Optional[Resource] = None,
Expand Down
19 changes: 19 additions & 0 deletions opentelemetry-sdk/tests/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,22 @@ def test_service_name_env_precedence(self):
detector.detect(),
resources.Resource({"service.name": "from-service-name"}),
)

def test_process_detector(self):
initial_resource = resources.Resource({"foo": "bar"})
aggregated_resource = resources.get_aggregated_resources(
[resources.ProcessResourceDetector()], initial_resource
)

self.assertIn(
resources.PROCESS_RUNTIME_NAME,
aggregated_resource.attributes.keys(),
)
self.assertIn(
resources.PROCESS_RUNTIME_DESCRIPTION,
aggregated_resource.attributes.keys(),
)
self.assertIn(
resources.PROCESS_RUNTIME_VERSION,
aggregated_resource.attributes.keys(),
)

0 comments on commit f367ec2

Please sign in to comment.