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

add check for event attribute values #678

Merged
merged 16 commits into from
May 15, 2020

Conversation

AndrewAXue
Copy link
Contributor

@AndrewAXue AndrewAXue commented May 11, 2020

This PR addresses issue #352

Validates attribute values and events upon span creation and setting attributes on spans.

Validation logic involves checking if value is one of (int, float, str, bool, list), and if list, each element must be valid and must be all the same primitive, valid type
list values will not contain nested lists
If value is a list, grants immutable property by converting value to a tuple.

@AndrewAXue AndrewAXue requested a review from a team as a code owner May 11, 2020 22:06
@mauriciovasquezbernal
Copy link
Member

Given that you are looking at this, could you also consider the case where attributes and events are passed to the span's constructor?

if attributes is None:
self.attributes = Span._empty_attributes
else:
self.attributes = BoundedDict.from_map(
MAX_NUM_ATTRIBUTES, attributes
)
if events is None:
self.events = Span._empty_events
else:
self.events = BoundedList.from_seq(MAX_NUM_EVENTS, events)
if links is None:
self.links = Span._empty_links
else:
self.links = BoundedList.from_seq(MAX_NUM_LINKS, links)

I always wanted to open an issue about it but never did.

@AndrewAXue AndrewAXue closed this May 12, 2020
@AndrewAXue AndrewAXue reopened this May 12, 2020
Copy link
Contributor

@sjbrunst sjbrunst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this PR is for #352, but I don't see anything in the PR tying it back to that issue. Should a link be added to the description or something?

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py Outdated Show resolved Hide resolved
opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py Outdated Show resolved Hide resolved
@AndrewAXue AndrewAXue removed the request for review from a team May 12, 2020 17:29
Copy link
Contributor

@sjbrunst sjbrunst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple minor comments, otherwise LGTM!

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py Outdated Show resolved Hide resolved
opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py Outdated Show resolved Hide resolved
@lzchen
Copy link
Contributor

lzchen commented May 12, 2020

It would be nice if you could add a description of your changes in the PR description. Sometimes linking the issue is ambiguous, especially if the issue has an ongoing discussion (like this one). It's also good practice to include specific implementation details that is not obvious.

For example:
Validates attribute values upon span creation and setting attributes on spans.

  1. Validation logic involves checking if value is one of (int, float, str, bool, list), and if list, each element must be valid and must be all the same type
  2. list values will not contain nested lists
  3. If value is a list, grants immutable property by converting value to a tuple.

Copy link
Contributor

@lzchen lzchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Some non-blocking comments.

Copy link
Member

@c24t c24t left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic and tests look great, and nice helpful error messages to boot.

We should consider filtering LazyEvent.attributes too (and caching the attributes after the first call to _event_formatter, which I'm surprised to see we're not doing already), but that's a problem for another PR.

Welcome to the project @AndrewAXue!

)
return False

for element in list(value)[1:]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: it's a sequence, do you need to cast to slice it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if its a generator its not possible to slice it immediately

Copy link
Member

@c24t c24t May 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are generators sequences? It looks like they need to implement __getitem__, which is what slice uses under the hood.

@c24t
Copy link
Member

c24t commented May 13, 2020

@AndrewAXue one more formatting nit: wrapping strings at 80 chars:

diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
index d4054de2..5b74b4a6 100644
--- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
+++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
@@ -192,8 +192,9 @@ class LazyEvent(EventBase):
 def _is_valid_attribute_value(value: types.AttributeValue) -> bool:
     """Checks if attribute value is valid.

-    An attribute value is valid if it is one of the valid types. If the value is a sequence, it is only valid
-    if all items in the sequence are of valid type, not a sequence, and are of the same type.
+    An attribute value is valid if it is one of the valid types. If the value
+    is a sequence, it is only valid if all items in the sequence are of valid
+    type, not a sequence, and are of the same type.
     """

     if isinstance(value, Sequence):
@@ -204,7 +205,8 @@ def _is_valid_attribute_value(value: types.AttributeValue) -> bool:

         if first_element_type not in VALID_ATTR_VALUE_TYPES:
             logger.warning(
-                "Invalid type %s in attribute value sequence. Expected one of %s or a sequence of those types",
+                "Invalid type %s in attribute value sequence. Expected one of "
+                "%s or a sequence of those types",
                 first_element_type.__name__,
                 [valid_type.__name__ for valid_type in VALID_ATTR_VALUE_TYPES],
             )
@@ -220,7 +222,8 @@ def _is_valid_attribute_value(value: types.AttributeValue) -> bool:
                 return False
     elif not isinstance(value, VALID_ATTR_VALUE_TYPES):
         logger.warning(
-            "Invalid type %s for attribute value. Expected one of %s or a sequence of those types",
+            "Invalid type %s for attribute value. Expected one of %s or a "
+            "sequence of those types",
             type(value).__name__,
             [valid_type.__name__ for valid_type in VALID_ATTR_VALUE_TYPES],
         )

It looks like I can't push to your branch directly, did you check "allow edits from maintainers"?

@AndrewAXue
Copy link
Contributor Author

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

I checked the "allow edits from maintainers" box and it was checked before as well. Maybe it's because I signed the CLA midway through the PR? Either way I've manually added the changes.

Copy link
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for taking this on

@codeboten codeboten merged commit 4ab3978 into open-telemetry:master May 15, 2020
@AndrewAXue AndrewAXue deleted the validate_event_attrs branch May 15, 2020 00:39
cnnradams pushed a commit to cnnradams/opentelemetry-python that referenced this pull request May 15, 2020
This PR addresses issue open-telemetry#352

Validates attribute values and events upon span creation and setting attributes on spans.

Validation logic involves checking if value is one of (int, float, str, bool, list), and if list, each element must be valid and must be all the same primitive, valid type
list values will not contain nested lists
If value is a list, grants immutable property by converting value to a tuple.
owais pushed a commit to owais/opentelemetry-python that referenced this pull request May 22, 2020
This PR addresses issue open-telemetry#352

Validates attribute values and events upon span creation and setting attributes on spans.

Validation logic involves checking if value is one of (int, float, str, bool, list), and if list, each element must be valid and must be all the same primitive, valid type
list values will not contain nested lists
If value is a list, grants immutable property by converting value to a tuple.
@AndrewAXue AndrewAXue mentioned this pull request May 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants