add check for event attribute values#678
Conversation
|
Given that you are looking at this, could you also consider the case where attributes and events are passed to the span's constructor? I always wanted to open an issue about it but never did. |
fe73e6e to
3ab4182
Compare
2aee48f to
587297b
Compare
sjbrunst
left a comment
There was a problem hiding this comment.
A couple minor comments, otherwise LGTM!
|
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:
|
lzchen
left a comment
There was a problem hiding this comment.
LGTM. Some non-blocking comments.
9c4e3f6 to
ad919a0
Compare
ad919a0 to
f23ebf5
Compare
c24t
left a comment
There was a problem hiding this comment.
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:]: |
There was a problem hiding this comment.
Nit: it's a sequence, do you need to cast to slice it?
There was a problem hiding this comment.
Yes, if its a generator its not possible to slice it immediately
There was a problem hiding this comment.
Are generators sequences? It looks like they need to implement __getitem__, which is what slice uses under the hood.
|
@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"? |
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. |
5ab4435 to
fa51d63
Compare
codeboten
left a comment
There was a problem hiding this comment.
LGTM. Thanks for taking this on
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.
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.
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.