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

Populate resource attributes as per semantic conventions #1053

Merged
merged 13 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
([#1023](https://github.com/open-telemetry/opentelemetry-python/pull/1023))
- Remove lazy Event and Link API from Span interface
([#1045](https://github.com/open-telemetry/opentelemetry-python/pull/1045))

- Populate resource attributes as per semantic conventions
([#1053](https://github.com/open-telemetry/opentelemetry-python/pull/1053))
## Version 0.12b0

Released 2020-08-14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ class MeterProvider(metrics_api.MeterProvider):
def __init__(
self,
stateful=True,
resource: Resource = Resource.create_empty(),
resource: Resource = Resource.create({}),
shutdown_on_exit: bool = True,
):
self.stateful = stateful
Expand Down
21 changes: 19 additions & 2 deletions opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import concurrent.futures
import logging
import os
import pkg_resources
import typing
from json import dumps

Expand All @@ -24,15 +25,24 @@
logger = logging.getLogger(__name__)


TELEMETRY_SDK_LANGUAGE = "telemetry.sdk.language"
TELEMETRY_SDK_NAME = "telemetry.sdk.name"
TELEMETRY_SDK_VERSION = "telemetry.sdk.version"

OPENTELEMETRY_SDK_VERSION = pkg_resources.get_distribution(
"opentelemetry-sdk"
).version


class Resource:
def __init__(self, labels: Labels):
self._labels = labels.copy()

@staticmethod
def create(labels: Labels) -> "Resource":
if not labels:
return _EMPTY_RESOURCE
return Resource(labels)
return _DEFAULT_RESOURCE
return _DEFAULT_RESOURCE.merge(Resource(labels))

@staticmethod
def create_empty() -> "Resource":
Expand Down Expand Up @@ -60,6 +70,13 @@ def __hash__(self):


_EMPTY_RESOURCE = Resource({})
_DEFAULT_RESOURCE = Resource(
{
TELEMETRY_SDK_NAME: "opentelemetry",
TELEMETRY_SDK_LANGUAGE: "python",
TELEMETRY_SDK_VERSION: OPENTELEMETRY_SDK_VERSION,
}
)


class ResourceDetector(abc.ABC):
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def __init__(
parent: Optional[trace_api.SpanContext] = None,
sampler: Optional[sampling.Sampler] = None,
trace_config: None = None, # TODO
resource: Resource = Resource.create_empty(),
resource: Resource = Resource.create({}),
attributes: types.Attributes = None, # TODO
events: Sequence[Event] = None, # TODO
links: Sequence[trace_api.Link] = (),
Expand Down Expand Up @@ -828,7 +828,7 @@ class TracerProvider(trace_api.TracerProvider):
def __init__(
self,
sampler: sampling.Sampler = sampling.DEFAULT_ON,
resource: Resource = Resource.create_empty(),
resource: Resource = Resource.create({}),
shutdown_on_exit: bool = True,
active_span_processor: Union[
SynchronousMultiSpanProcessor, ConcurrentMultiSpanProcessor
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-sdk/tests/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_resource_empty(self):
meter_provider = metrics.MeterProvider()
meter = meter_provider.get_meter(__name__)
# pylint: disable=protected-access
self.assertIs(meter.resource, resources._EMPTY_RESOURCE)
self.assertIs(meter.resource, resources._DEFAULT_RESOURCE)

def test_start_pipeline(self):
exporter = mock.Mock()
Expand Down
29 changes: 26 additions & 3 deletions opentelemetry-sdk/tests/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
# pylint: disable=protected-access

import os
import pkg_resources
import unittest
from unittest import mock

from opentelemetry.sdk import resources


OPENTELEMETRY_SDK_VERSION = pkg_resources.get_distribution(
"opentelemetry-sdk"
).version


class TestResources(unittest.TestCase):
def test_create(self):
labels = {
Expand All @@ -30,18 +36,28 @@ def test_create(self):
"cost": 112.12,
}

expected_labels = {
"service": "ui",
"version": 1,
"has_bugs": True,
"cost": 112.12,
resources.TELEMETRY_SDK_NAME: "opentelemetry",
resources.TELEMETRY_SDK_LANGUAGE: "python",
resources.TELEMETRY_SDK_VERSION: OPENTELEMETRY_SDK_VERSION,
}

resource = resources.Resource.create(labels)
self.assertIsInstance(resource, resources.Resource)
self.assertEqual(resource.labels, labels)
self.assertEqual(resource.labels, expected_labels)

resource = resources.Resource.create_empty()
self.assertIs(resource, resources._EMPTY_RESOURCE)

resource = resources.Resource.create(None)
self.assertIs(resource, resources._EMPTY_RESOURCE)
self.assertIs(resource, resources._DEFAULT_RESOURCE)

resource = resources.Resource.create({})
self.assertIs(resource, resources._EMPTY_RESOURCE)
self.assertIs(resource, resources._DEFAULT_RESOURCE)

def test_resource_merge(self):
left = resources.Resource({"service": "ui"})
Expand Down Expand Up @@ -75,7 +91,14 @@ def test_immutability(self):
"cost": 112.12,
}

default_labels = {
resources.TELEMETRY_SDK_NAME: "opentelemetry",
resources.TELEMETRY_SDK_LANGUAGE: "python",
resources.TELEMETRY_SDK_VERSION: OPENTELEMETRY_SDK_VERSION,
}

labels_copy = labels.copy()
labels_copy.update(default_labels)

resource = resources.Resource.create(labels)
self.assertEqual(resource.labels, labels_copy)
Expand Down
33 changes: 24 additions & 9 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import pkg_resources
import shutil
import subprocess
import unittest
Expand All @@ -26,6 +27,11 @@
from opentelemetry.util import time_ns


OPENTELEMETRY_SDK_VERSION = pkg_resources.get_distribution(
"opentelemetry-sdk"
).version


def new_tracer() -> trace_api.Tracer:
return trace.TracerProvider().get_tracer(__name__)

Expand Down Expand Up @@ -384,7 +390,7 @@ def test_default_span_resource(self):
tracer = tracer_provider.get_tracer(__name__)
span = tracer.start_span("root")
# pylint: disable=protected-access
self.assertIs(span.resource, resources._EMPTY_RESOURCE)
self.assertIs(span.resource, resources._DEFAULT_RESOURCE)

def test_span_context_remote_flag(self):
tracer = new_tracer()
Expand Down Expand Up @@ -924,24 +930,33 @@ def test_to_json(self):

self.assertEqual(
span.to_json(),
"""{
"""{{
"name": "span-name",
"context": {
"context": {{
"trace_id": "0x000000000000000000000000deadbeef",
"span_id": "0x00000000deadbef0",
"trace_state": "{}"
},
"trace_state": "{{}}"
}},
"kind": "SpanKind.INTERNAL",
"parent_id": null,
"start_time": null,
"end_time": null,
"attributes": {},
"attributes": {{}},
"events": [],
"links": [],
"resource": {}
}""",
"resource": {{
"telemetry.sdk.name": "opentelemetry",
"telemetry.sdk.language": "python",
"telemetry.sdk.version": "{version}"
}}
}}""".format(
version=OPENTELEMETRY_SDK_VERSION
),
)
self.assertEqual(
span.to_json(indent=None),
'{"name": "span-name", "context": {"trace_id": "0x000000000000000000000000deadbeef", "span_id": "0x00000000deadbef0", "trace_state": "{}"}, "kind": "SpanKind.INTERNAL", "parent_id": null, "start_time": null, "end_time": null, "attributes": {}, "events": [], "links": [], "resource": {}}',
'{{"name": "span-name", "context": {{"trace_id": "0x000000000000000000000000deadbeef", "span_id": "0x00000000deadbef0", "trace_state": "{{}}"}}, "kind": "SpanKind.INTERNAL", "parent_id": null, "start_time": null, "end_time": null, "attributes": {{}}, "events": [], "links": [], "resource": {{"telemetry.sdk.name": "opentelemetry", "telemetry.sdk.language": "python", "telemetry.sdk.version": "{version}"}}}}'.format(
version=OPENTELEMETRY_SDK_VERSION
),
)