From 0554ec3d6862748db8026bed1c7a56d49db7ac4b Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Fri, 19 Jul 2019 22:31:23 -0700 Subject: [PATCH 1/6] Adding the Resource API --- .../src/opentelemetry/resources/__init__.py | 53 +++++++++++++++++++ opentelemetry-api/tests/resources/__init__.py | 0 .../tests/resources/test_init.py | 12 +++++ 3 files changed, 65 insertions(+) create mode 100644 opentelemetry-api/tests/resources/__init__.py create mode 100644 opentelemetry-api/tests/resources/test_init.py diff --git a/opentelemetry-api/src/opentelemetry/resources/__init__.py b/opentelemetry-api/src/opentelemetry/resources/__init__.py index d853a7bcf6..151fac381d 100644 --- a/opentelemetry-api/src/opentelemetry/resources/__init__.py +++ b/opentelemetry-api/src/opentelemetry/resources/__init__.py @@ -11,3 +11,56 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +from typing import Any, Dict, Union + + +class Resource: + def __init__(self, labels: Dict[str, str]): + """ + Construct a resource. Direct calling of the + constructor is discouraged, as it cannot + take advantage of caching and restricts + to the type of object that can be returned. + """ + self._labels = labels + + @staticmethod + def create(labels: Dict[str, str]) -> "Resource": + """ + create a new resource. This is the recommended + method to use to create a new resource. + """ + return Resource(labels) + + def merge(self, other: Union["Resource", None]) -> "Resource": + """ + Perform a merge of the resources, resulting + in a union of the resource objects. + + labels that exist in the main Resource take + precedence unless the label value is empty. + """ + if other is None: + return self + if not self._labels: + return other + merged_labels = self.get_labels().copy() + for key, value in other.get_labels().items(): + if key not in merged_labels or merged_labels[key] == "": + merged_labels[key] = value + return Resource(merged_labels) + + def get_labels(self) -> Dict[str, str]: + """ + Return the labels associated with this resource. + + get_labels exposes the raw internal dictionary, + and as such it is not recommended to copy the + result if it is desired to mutate the result. + """ + return self._labels + + def __eq__(self, other: Any) -> bool: + if not isinstance(other, Resource): + return False + return self.get_labels() == other.get_labels() diff --git a/opentelemetry-api/tests/resources/__init__.py b/opentelemetry-api/tests/resources/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/opentelemetry-api/tests/resources/test_init.py b/opentelemetry-api/tests/resources/test_init.py new file mode 100644 index 0000000000..168082e426 --- /dev/null +++ b/opentelemetry-api/tests/resources/test_init.py @@ -0,0 +1,12 @@ +import unittest +from opentelemetry.resources import Resource + + +class TestResources(unittest.TestCase): + @staticmethod + def test_resource_merge(): + left = Resource({"service": "ui"}) + right = Resource({"host": "service-host"}) + assert left.merge(right) == Resource( + {"service": "ui", "host": "service-host"} + ) From e16e854faee1aec52415c9a2a534f29ac02b050f Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Wed, 24 Jul 2019 13:48:24 -0700 Subject: [PATCH 2/6] Resource API: unit test on empty string precedence When merging resources, the non-empty string value takes precedence. --- opentelemetry-api/tests/resources/test_init.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/opentelemetry-api/tests/resources/test_init.py b/opentelemetry-api/tests/resources/test_init.py index 168082e426..d664910f60 100644 --- a/opentelemetry-api/tests/resources/test_init.py +++ b/opentelemetry-api/tests/resources/test_init.py @@ -10,3 +10,15 @@ def test_resource_merge(): assert left.merge(right) == Resource( {"service": "ui", "host": "service-host"} ) + + @staticmethod + def test_resource_merge_empty_string(): + """ + labels from the source Resource take + precedence, with the exception of the empty string. + """ + left = Resource({"service": "ui", "host": ""}) + right = Resource({"host": "service-host", "service": "not-ui"}) + assert left.merge(right) == Resource( + {"service": "ui", "host": "service-host"} + ) From b95da6a5c460e95d26b81650028f23b83c12622c Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Thu, 25 Jul 2019 15:09:56 -0700 Subject: [PATCH 3/6] Moving Resource to API and SDK The API package should be minimal, and example implementations should live in SDK when possible. Fixing docstrings to match sphinx-compatible formatting. using unittest-standard asserts to adhere to conventions. switcihng from Any to object works helps pass linting and mypy compatibility. --- .../src/opentelemetry/resources/__init__.py | 65 ++++++++----------- .../tests/resources/test_init.py | 24 ------- .../opentelemetry/sdk/resources/__init__.py | 44 +++++++++++++ .../tests/resources/__init__.py | 0 .../tests/resources/test_init.py | 28 ++++++++ 5 files changed, 99 insertions(+), 62 deletions(-) delete mode 100644 opentelemetry-api/tests/resources/test_init.py create mode 100644 opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py rename {opentelemetry-api => opentelemetry-sdk}/tests/resources/__init__.py (100%) create mode 100644 opentelemetry-sdk/tests/resources/test_init.py diff --git a/opentelemetry-api/src/opentelemetry/resources/__init__.py b/opentelemetry-api/src/opentelemetry/resources/__init__.py index 151fac381d..467efe5562 100644 --- a/opentelemetry-api/src/opentelemetry/resources/__init__.py +++ b/opentelemetry-api/src/opentelemetry/resources/__init__.py @@ -11,56 +11,45 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from typing import Any, Dict, Union +from abc import ABC, abstractmethod +from typing import Dict, Union -class Resource: +class Resource(ABC): + """This the interface that resources must implement""" def __init__(self, labels: Dict[str, str]): - """ - Construct a resource. Direct calling of the - constructor is discouraged, as it cannot - take advantage of caching and restricts - to the type of object that can be returned. + """Construct a resource. + + Direct calling of the constructor is discouraged, as it cannot + take advantage of caching and restricts to the type of object + that can be returned. """ self._labels = labels @staticmethod def create(labels: Dict[str, str]) -> "Resource": - """ - create a new resource. This is the recommended - method to use to create a new resource. - """ - return Resource(labels) + """create a new resource. - def merge(self, other: Union["Resource", None]) -> "Resource": - """ - Perform a merge of the resources, resulting - in a union of the resource objects. + Args: + labels: the labels that define the resource - labels that exist in the main Resource take - precedence unless the label value is empty. + Returns: + The resource with the labels in question """ - if other is None: - return self - if not self._labels: - return other - merged_labels = self.get_labels().copy() - for key, value in other.get_labels().items(): - if key not in merged_labels or merged_labels[key] == "": - merged_labels[key] = value - return Resource(merged_labels) + @property + @abstractmethod + def labels(self) -> Dict[str, str]: + """Return the label dictionary associated with this resource. - def get_labels(self) -> Dict[str, str]: + Returns: + A dictionary with the labels of the resource """ - Return the labels associated with this resource. + def merge(self, other: Union["Resource", None]) -> "Resource": + """Return a resource with the union of labels for both resources. - get_labels exposes the raw internal dictionary, - and as such it is not recommended to copy the - result if it is desired to mutate the result. - """ - return self._labels + Labels that exist in the main Resource take + precedence unless the label value is the empty string. - def __eq__(self, other: Any) -> bool: - if not isinstance(other, Resource): - return False - return self.get_labels() == other.get_labels() + Args: + other: the resource to merge in + """ diff --git a/opentelemetry-api/tests/resources/test_init.py b/opentelemetry-api/tests/resources/test_init.py deleted file mode 100644 index d664910f60..0000000000 --- a/opentelemetry-api/tests/resources/test_init.py +++ /dev/null @@ -1,24 +0,0 @@ -import unittest -from opentelemetry.resources import Resource - - -class TestResources(unittest.TestCase): - @staticmethod - def test_resource_merge(): - left = Resource({"service": "ui"}) - right = Resource({"host": "service-host"}) - assert left.merge(right) == Resource( - {"service": "ui", "host": "service-host"} - ) - - @staticmethod - def test_resource_merge_empty_string(): - """ - labels from the source Resource take - precedence, with the exception of the empty string. - """ - left = Resource({"service": "ui", "host": ""}) - right = Resource({"host": "service-host", "service": "not-ui"}) - assert left.merge(right) == Resource( - {"service": "ui", "host": "service-host"} - ) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py new file mode 100644 index 0000000000..c106cfb8f8 --- /dev/null +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -0,0 +1,44 @@ +# Copyright 2019, OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from typing import Any, Dict, Union +from opentelemetry.resources import Resource + + +class DefaultResource(Resource): + def __init__(self, labels): + self._labels = labels + + @property + def labels(self): + return self._labels + + @staticmethod + def create(labels): + return DefaultResource(labels) + + def merge(self, other): + if other is None: + return self + if not self._labels: + return other + merged_labels = self.labels.copy() + for key, value in other.labels.items(): + if key not in merged_labels or merged_labels[key] == "": + merged_labels[key] = value + return DefaultResource(merged_labels) + + def __eq__(self, other: object) -> bool: + if not isinstance(other, DefaultResource): + return False + return self.labels == other.labels diff --git a/opentelemetry-api/tests/resources/__init__.py b/opentelemetry-sdk/tests/resources/__init__.py similarity index 100% rename from opentelemetry-api/tests/resources/__init__.py rename to opentelemetry-sdk/tests/resources/__init__.py diff --git a/opentelemetry-sdk/tests/resources/test_init.py b/opentelemetry-sdk/tests/resources/test_init.py new file mode 100644 index 0000000000..d6cbfbb90a --- /dev/null +++ b/opentelemetry-sdk/tests/resources/test_init.py @@ -0,0 +1,28 @@ +import unittest +from opentelemetry.sdk.resources import DefaultResource + + +class TestResources(unittest.TestCase): + def test_resource_merge(self): + left = DefaultResource({"service": "ui"}) + right = DefaultResource({"host": "service-host"}) + self.assertEqual( + left.merge(right), + DefaultResource({ + "service": "ui", + "host": "service-host" + })) + + def test_resource_merge_empty_string(self): + """ + labels from the source Resource take + precedence, with the exception of the empty string. + """ + left = DefaultResource({"service": "ui", "host": ""}) + right = DefaultResource({"host": "service-host", "service": "not-ui"}) + self.assertEqual( + left.merge(right), + DefaultResource({ + "service": "ui", + "host": "service-host" + })) From 8beab78b61d172566bc835749b93771078faf8a5 Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Fri, 26 Jul 2019 21:16:10 -0700 Subject: [PATCH 4/6] Resource API: renaming DefaultResource to Resource The precedent that exists is to name both the interface and the implementation in the sdk the same. This avoids the generally unhelpful "Default" prefix. opentelemetry-python is standardizing on Google style docstrings. As such resolving formatting that is inconsistent with the style guilde. Removing init from the abstract Resource class: it does not need to be part of the Resource API spec. Other Resource implementations may consume other arguments as well. --- .../src/opentelemetry/resources/__init__.py | 29 +++++++++---------- .../opentelemetry/sdk/resources/__init__.py | 18 ++++++------ .../tests/resources/test_init.py | 27 ++++++++++------- 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/resources/__init__.py b/opentelemetry-api/src/opentelemetry/resources/__init__.py index 467efe5562..386eeff2e9 100644 --- a/opentelemetry-api/src/opentelemetry/resources/__init__.py +++ b/opentelemetry-api/src/opentelemetry/resources/__init__.py @@ -11,30 +11,24 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + from abc import ABC, abstractmethod -from typing import Dict, Union +from typing import Dict, Optional class Resource(ABC): - """This the interface that resources must implement""" - def __init__(self, labels: Dict[str, str]): - """Construct a resource. - - Direct calling of the constructor is discouraged, as it cannot - take advantage of caching and restricts to the type of object - that can be returned. - """ - self._labels = labels - + """The interface that resources must implement.""" @staticmethod + @abstractmethod def create(labels: Dict[str, str]) -> "Resource": - """create a new resource. + """Create a new resource. Args: labels: the labels that define the resource Returns: The resource with the labels in question + """ @property @abstractmethod @@ -43,13 +37,16 @@ def labels(self) -> Dict[str, str]: Returns: A dictionary with the labels of the resource + """ - def merge(self, other: Union["Resource", None]) -> "Resource": + @abstractmethod + def merge(self, other: Optional["Resource"]) -> "Resource": """Return a resource with the union of labels for both resources. - Labels that exist in the main Resource take - precedence unless the label value is the empty string. + Labels that exist in the main Resource take precedence unless the + label value is the empty string. Args: - other: the resource to merge in + other: The resource to merge in + """ diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index c106cfb8f8..b488c0a0c7 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -11,22 +11,22 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from typing import Any, Dict, Union -from opentelemetry.resources import Resource +import opentelemetry.resources as resources -class DefaultResource(Resource): + +class Resource(resources.Resource): def __init__(self, labels): self._labels = labels + @staticmethod + def create(labels): + return Resource(labels) + @property def labels(self): return self._labels - @staticmethod - def create(labels): - return DefaultResource(labels) - def merge(self, other): if other is None: return self @@ -36,9 +36,9 @@ def merge(self, other): for key, value in other.labels.items(): if key not in merged_labels or merged_labels[key] == "": merged_labels[key] = value - return DefaultResource(merged_labels) + return Resource(merged_labels) def __eq__(self, other: object) -> bool: - if not isinstance(other, DefaultResource): + if not isinstance(other, Resource): return False return self.labels == other.labels diff --git a/opentelemetry-sdk/tests/resources/test_init.py b/opentelemetry-sdk/tests/resources/test_init.py index d6cbfbb90a..cddc4cc87f 100644 --- a/opentelemetry-sdk/tests/resources/test_init.py +++ b/opentelemetry-sdk/tests/resources/test_init.py @@ -1,28 +1,33 @@ import unittest -from opentelemetry.sdk.resources import DefaultResource +from opentelemetry.sdk import resources class TestResources(unittest.TestCase): def test_resource_merge(self): - left = DefaultResource({"service": "ui"}) - right = DefaultResource({"host": "service-host"}) + left = resources.Resource({"service": "ui"}) + right = resources.Resource({"host": "service-host"}) self.assertEqual( left.merge(right), - DefaultResource({ + resources.Resource({ "service": "ui", "host": "service-host" })) def test_resource_merge_empty_string(self): + """Verify Resource.merge behavior with the empty string. + + Labels from the source Resource take precedence, with + the exception of the empty string. + """ - labels from the source Resource take - precedence, with the exception of the empty string. - """ - left = DefaultResource({"service": "ui", "host": ""}) - right = DefaultResource({"host": "service-host", "service": "not-ui"}) + left = resources.Resource({"service": "ui", "host": ""}) + right = resources.Resource({ + "host": "service-host", + "service": "not-ui" + }) self.assertEqual( left.merge(right), - DefaultResource({ + resources.Resource({ "service": "ui", "host": "service-host" - })) + })) \ No newline at end of file From 389dde8d0eed15c54ec013ad59dd526a115eede8 Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Fri, 26 Jul 2019 21:22:59 -0700 Subject: [PATCH 5/6] adding missing newline (lint error) --- opentelemetry-sdk/tests/resources/test_init.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-sdk/tests/resources/test_init.py b/opentelemetry-sdk/tests/resources/test_init.py index cddc4cc87f..953b4c2517 100644 --- a/opentelemetry-sdk/tests/resources/test_init.py +++ b/opentelemetry-sdk/tests/resources/test_init.py @@ -30,4 +30,4 @@ def test_resource_merge_empty_string(self): resources.Resource({ "service": "ui", "host": "service-host" - })) \ No newline at end of file + })) From 3c363c679eafd32548198948c2562abfec96286d Mon Sep 17 00:00:00 2001 From: Yusuke Tsutsumi Date: Sat, 27 Jul 2019 06:36:25 -0700 Subject: [PATCH 6/6] Fixing imports to adhere with isort the master branch now include isort configuration that forces one import per line. --- .../src/opentelemetry/resources/__init__.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/resources/__init__.py b/opentelemetry-api/src/opentelemetry/resources/__init__.py index 386eeff2e9..0d1e34dddb 100644 --- a/opentelemetry-api/src/opentelemetry/resources/__init__.py +++ b/opentelemetry-api/src/opentelemetry/resources/__init__.py @@ -12,15 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -from abc import ABC, abstractmethod -from typing import Dict, Optional +import abc +import typing -class Resource(ABC): +class Resource(abc.ABC): """The interface that resources must implement.""" @staticmethod - @abstractmethod - def create(labels: Dict[str, str]) -> "Resource": + @abc.abstractmethod + def create(labels: typing.Dict[str, str]) -> "Resource": """Create a new resource. Args: @@ -31,16 +31,16 @@ def create(labels: Dict[str, str]) -> "Resource": """ @property - @abstractmethod - def labels(self) -> Dict[str, str]: + @abc.abstractmethod + def labels(self) -> typing.Dict[str, str]: """Return the label dictionary associated with this resource. Returns: A dictionary with the labels of the resource """ - @abstractmethod - def merge(self, other: Optional["Resource"]) -> "Resource": + @abc.abstractmethod + def merge(self, other: typing.Optional["Resource"]) -> "Resource": """Return a resource with the union of labels for both resources. Labels that exist in the main Resource take precedence unless the