Skip to content

Commit

Permalink
Implement 'process.runtime.*' attributes for resources
Browse files Browse the repository at this point in the history
Implement support for the the 'process.runtime.name',
'process.runtime.version' and 'process.runtime.description'
as specified in the resource semantic conventions for
processes.
  • Loading branch information
Michele Mancioppi authored and mmanciop committed May 5, 2022
1 parent 7397605 commit c15e3b6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add variadic arguments to metric exporter/reader interfaces
([#2654](https://github.com/open-telemetry/opentelemetry-python/pull/2654))
- Implement 'process.runtime.{name,version,description}' resource attributes
([#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
17 changes: 16 additions & 1 deletion opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
)
print(trace.get_tracer_provider().resource.attributes)
{'telemetry.sdk.language': 'python',
{'process.runtime.name': 'cpython',
'process.runtime.version': '3.8.6',
'process.runtime.description': '3.8.6 (default, Sep 30 2020, 04:00:38)\n[GCC 10.2.0]'
'telemetry.sdk.language': 'python',
'telemetry.sdk.name': 'opentelemetry',
'telemetry.sdk.version': '0.13.dev0',
'service.name': 'shoppingcart',
Expand All @@ -59,6 +62,7 @@
import concurrent.futures
import logging
import os
import sys
import typing
from json import dumps

Expand Down Expand Up @@ -243,8 +247,19 @@ def __hash__(self):


_EMPTY_RESOURCE = 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
))

_DEFAULT_RESOURCE = Resource(
{
PROCESS_RUNTIME_DESCRIPTION: sys.implementation.version,
PROCESS_RUNTIME_NAME: sys.implementation.name,
PROCESS_RUNTIME_VERSION: _runtime_version,
TELEMETRY_SDK_LANGUAGE: "python",
TELEMETRY_SDK_NAME: "opentelemetry",
TELEMETRY_SDK_VERSION: _OPENTELEMETRY_SDK_VERSION,
Expand Down
16 changes: 16 additions & 0 deletions opentelemetry-sdk/tests/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@

from opentelemetry.sdk import resources

# Avoid volatility in tests due to the runtime running them
_PATCHED_DEFAULT_RESOURCE = resources._DEFAULT_RESOURCE.merge(
resources.Resource.create({
resources.PROCESS_RUNTIME_NAME: "cpython",
resources.PROCESS_RUNTIME_VERSION: "1.2.3-alpha",
resources.PROCESS_RUNTIME_DESCRIPTION: "cpython 1.2.3",
})
)


@mock.patch("opentelemetry.sdk.resources._DEFAULT_RESOURCE", new=_PATCHED_DEFAULT_RESOURCE)
class TestResources(unittest.TestCase):
def setUp(self) -> None:
os.environ[resources.OTEL_RESOURCE_ATTRIBUTES] = ""
Expand All @@ -43,6 +53,9 @@ def test_create(self):
"version": 1,
"has_bugs": True,
"cost": 112.12,
resources.PROCESS_RUNTIME_NAME: "cpython",
resources.PROCESS_RUNTIME_VERSION: "1.2.3-alpha",
resources.PROCESS_RUNTIME_DESCRIPTION: "cpython 1.2.3",
resources.TELEMETRY_SDK_NAME: "opentelemetry",
resources.TELEMETRY_SDK_LANGUAGE: "python",
resources.TELEMETRY_SDK_VERSION: resources._OPENTELEMETRY_SDK_VERSION,
Expand Down Expand Up @@ -176,6 +189,9 @@ def test_immutability(self):
}

default_attributes = {
resources.PROCESS_RUNTIME_NAME: "cpython",
resources.PROCESS_RUNTIME_VERSION: "1.2.3",
resources.PROCESS_RUNTIME_DESCRIPTION: "cpython 1.2.3",
resources.TELEMETRY_SDK_NAME: "opentelemetry",
resources.TELEMETRY_SDK_LANGUAGE: "python",
resources.TELEMETRY_SDK_VERSION: resources._OPENTELEMETRY_SDK_VERSION,
Expand Down

0 comments on commit c15e3b6

Please sign in to comment.