Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Sep 20, 2024
1 parent dada0be commit 9658d8b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 52 deletions.
91 changes: 52 additions & 39 deletions opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"""

from abc import ABC, abstractmethod
from warnings import warn
import concurrent.futures
import logging
import os
Expand Down Expand Up @@ -253,12 +254,18 @@ def __init__(
self,
attributes: Attributes,
schema_url: typing.Optional[str] = None,
*entities
):
self._attributes = BoundedAttributes(attributes=attributes)
if schema_url is None:
schema_url = ""
else:
warn("Resource.schema_url is deprecated", DeprecationWarning)

self._schema_url = schema_url

self._entities = entities

@staticmethod
def create(
attributes: typing.Optional[Attributes] = None,
Expand Down Expand Up @@ -318,8 +325,8 @@ def create(
)
return resource

@staticmethod
def create_using_entities(
self,
attributes: typing.Optional[Attributes] = None,
schema_url: typing.Optional[str] = None,
) -> "Resource":
Expand Down Expand Up @@ -353,52 +360,22 @@ def create_using_entities(

priorities.add(entity_detector)

self.select_entities(
selected_entities = _select_entities(
entity_detector.detect() for entity in entity_detectors
)

# Now, entities will be merged to construct a Resource. See the
# algorigthm here:
if len(selected_entities) > 1:

# https://github.com/open-telemetry/oteps/pull/264/files#diff-f31c399a9b6dfa710794c070caed4820ad54d5076e5640a881cd1082a7aa33bfR119
entity_schema_url = selected_entities[0].schema_url

return entity_detectors
for selected_entity in selected_entities[1]:
if selected_entity.schema_url != entity_schema_url:
break

@staticmethod
def select_entities(unselected_entities):

selected_entities = [unselected_entities.pop(0)]

for unselected_entity in unselected_entities:

for selected_entity in selected_entities:

if selected_entity.type == unselected_entity.type:
if (
selected_entity.id == unselected_entity.id and
selected_entity.schema_url == (
unselected_entity.schema_url
)
):
for key, value in unselected_entity.attributes.items():
if key not in selected_entity.attributes.keys():
selected_entity._attributes[key] = value
break
elif (
selected_entity.id == unselected_entity.id and
selected_entity.schema_url != (
unselected_entity.schema_url
)
):
break
elif (
selected_entity.id != unselected_entity.id
):
break
else:
selected_entities.append(unselected_entity)
return Resource.create(schema_url=entity_schema_url)

return selected_entities
return Resource.create()

@staticmethod
def get_empty() -> "Resource":
Expand Down Expand Up @@ -473,6 +450,42 @@ def to_json(self, indent: int = 4) -> str:
)


def _select_entities(unselected_entities):

selected_entities = [unselected_entities.pop(0)]

for unselected_entity in unselected_entities:

for selected_entity in selected_entities:

if selected_entity.type == unselected_entity.type:
if (
selected_entity.id == unselected_entity.id and
selected_entity.schema_url == (
unselected_entity.schema_url
)
):
for key, value in unselected_entity.attributes.items():
if key not in selected_entity.attributes.keys():
selected_entity._attributes[key] = value
break
elif (
selected_entity.id == unselected_entity.id and
selected_entity.schema_url != (
unselected_entity.schema_url
)
):
break
elif (
selected_entity.id != unselected_entity.id
):
break
else:
selected_entities.append(unselected_entity)

return selected_entities


_EMPTY_RESOURCE = Resource({})
_DEFAULT_RESOURCE = Resource(
{
Expand Down
18 changes: 5 additions & 13 deletions opentelemetry-sdk/tests/resources/test_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
from opentelemetry.sdk.resources import (
Type0EntityDetector,
Type1EntityDetector,
_select_entities,
Resource,
Entity
)
from unittest.mock import MagicMock


class TestEntities(TestCase):
Expand Down Expand Up @@ -50,9 +50,7 @@ def test_select_entities(self):
),
]

selected_entities = Resource(MagicMock(), MagicMock()).select_entities(
unselected_entities
)
selected_entities = _select_entities(unselected_entities)

self.assertEqual(len(selected_entities), 1)
self.assertEqual(selected_entities[0].type, "type_0")
Expand All @@ -75,9 +73,7 @@ def test_select_entities(self):
),
]

selected_entities = Resource(MagicMock(), MagicMock()).select_entities(
unselected_entities
)
selected_entities = _select_entities(unselected_entities)

self.assertEqual(len(selected_entities), 1)
self.assertEqual(selected_entities[0].type, "type_0")
Expand All @@ -100,9 +96,7 @@ def test_select_entities(self):
),
]

selected_entities = Resource(MagicMock(), MagicMock()).select_entities(
unselected_entities
)
selected_entities = _select_entities(unselected_entities)

self.assertEqual(len(selected_entities), 1)
self.assertEqual(selected_entities[0].type, "type_0")
Expand All @@ -125,9 +119,7 @@ def test_select_entities(self):
),
]

selected_entities = Resource(MagicMock(), MagicMock()).select_entities(
unselected_entities
)
selected_entities = _select_entities(unselected_entities)

self.assertEqual(len(selected_entities), 2)
self.assertEqual(selected_entities[0].type, "type_0")
Expand Down

0 comments on commit 9658d8b

Please sign in to comment.