-
Notifications
You must be signed in to change notification settings - Fork 651
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into add-schema_url-attr…
…ibute
- Loading branch information
Showing
9 changed files
with
458 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
opentelemetry-api/src/opentelemetry/attributes/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Copyright The 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. | ||
# type: ignore | ||
|
||
import logging | ||
from types import MappingProxyType | ||
from typing import MutableSequence, Sequence | ||
|
||
from opentelemetry.util import types | ||
|
||
_VALID_ATTR_VALUE_TYPES = (bool, str, int, float) | ||
|
||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
def _is_valid_attribute_value(value: types.AttributeValue) -> bool: | ||
"""Checks if attribute value is valid. | ||
An attribute value is valid if it is either: | ||
- A primitive type: string, boolean, double precision floating | ||
point (IEEE 754-1985) or integer. | ||
- An array of primitive type values. The array MUST be homogeneous, | ||
i.e. it MUST NOT contain values of different types. | ||
""" | ||
|
||
if isinstance(value, Sequence): | ||
if len(value) == 0: | ||
return True | ||
|
||
sequence_first_valid_type = None | ||
for element in value: | ||
if element is None: | ||
continue | ||
element_type = type(element) | ||
if element_type not in _VALID_ATTR_VALUE_TYPES: | ||
_logger.warning( | ||
"Invalid type %s in attribute value sequence. Expected one of " | ||
"%s or None", | ||
element_type.__name__, | ||
[ | ||
valid_type.__name__ | ||
for valid_type in _VALID_ATTR_VALUE_TYPES | ||
], | ||
) | ||
return False | ||
# The type of the sequence must be homogeneous. The first non-None | ||
# element determines the type of the sequence | ||
if sequence_first_valid_type is None: | ||
sequence_first_valid_type = element_type | ||
elif not isinstance(element, sequence_first_valid_type): | ||
_logger.warning( | ||
"Mixed types %s and %s in attribute value sequence", | ||
sequence_first_valid_type.__name__, | ||
type(element).__name__, | ||
) | ||
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", | ||
type(value).__name__, | ||
[valid_type.__name__ for valid_type in _VALID_ATTR_VALUE_TYPES], | ||
) | ||
return False | ||
return True | ||
|
||
|
||
def _filter_attributes(attributes: types.Attributes) -> None: | ||
"""Applies attribute validation rules and drops (key, value) pairs | ||
that doesn't adhere to attributes specification. | ||
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/common/common.md#attributes. | ||
""" | ||
if attributes: | ||
for attr_key, attr_value in list(attributes.items()): | ||
if not attr_key: | ||
_logger.warning("invalid key `%s` (empty or null)", attr_key) | ||
attributes.pop(attr_key) | ||
continue | ||
|
||
if _is_valid_attribute_value(attr_value): | ||
if isinstance(attr_value, MutableSequence): | ||
attributes[attr_key] = tuple(attr_value) | ||
if isinstance(attr_value, bytes): | ||
try: | ||
attributes[attr_key] = attr_value.decode() | ||
except ValueError: | ||
attributes.pop(attr_key) | ||
_logger.warning("Byte attribute could not be decoded.") | ||
else: | ||
attributes.pop(attr_key) | ||
|
||
|
||
def _create_immutable_attributes( | ||
attributes: types.Attributes, | ||
) -> types.Attributes: | ||
return MappingProxyType(attributes.copy() if attributes else {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# Copyright The 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. | ||
|
||
# type: ignore | ||
|
||
import unittest | ||
|
||
from opentelemetry.attributes import ( | ||
_create_immutable_attributes, | ||
_filter_attributes, | ||
_is_valid_attribute_value, | ||
) | ||
|
||
|
||
class TestAttributes(unittest.TestCase): | ||
def test_is_valid_attribute_value(self): | ||
self.assertFalse(_is_valid_attribute_value([1, 2, 3.4, "ss", 4])) | ||
self.assertFalse(_is_valid_attribute_value([dict(), 1, 2, 3.4, 4])) | ||
self.assertFalse(_is_valid_attribute_value(["sw", "lf", 3.4, "ss"])) | ||
self.assertFalse(_is_valid_attribute_value([1, 2, 3.4, 5])) | ||
self.assertFalse(_is_valid_attribute_value(dict())) | ||
self.assertTrue(_is_valid_attribute_value(True)) | ||
self.assertTrue(_is_valid_attribute_value("hi")) | ||
self.assertTrue(_is_valid_attribute_value(3.4)) | ||
self.assertTrue(_is_valid_attribute_value(15)) | ||
self.assertTrue(_is_valid_attribute_value([1, 2, 3, 5])) | ||
self.assertTrue(_is_valid_attribute_value([1.2, 2.3, 3.4, 4.5])) | ||
self.assertTrue(_is_valid_attribute_value([True, False])) | ||
self.assertTrue(_is_valid_attribute_value(["ss", "dw", "fw"])) | ||
self.assertTrue(_is_valid_attribute_value([])) | ||
# None in sequences are valid | ||
self.assertTrue(_is_valid_attribute_value(["A", None, None])) | ||
self.assertTrue(_is_valid_attribute_value(["A", None, None, "B"])) | ||
self.assertTrue(_is_valid_attribute_value([None, None])) | ||
self.assertFalse(_is_valid_attribute_value(["A", None, 1])) | ||
self.assertFalse(_is_valid_attribute_value([None, "A", None, 1])) | ||
|
||
def test_filter_attributes(self): | ||
attrs_with_invalid_keys = { | ||
"": "empty-key", | ||
None: "None-value", | ||
"attr-key": "attr-value", | ||
} | ||
_filter_attributes(attrs_with_invalid_keys) | ||
self.assertTrue(len(attrs_with_invalid_keys), 1) | ||
self.assertEqual(attrs_with_invalid_keys, {"attr-key": "attr-value"}) | ||
|
||
attrs_with_invalid_values = { | ||
"nonhomogeneous": [1, 2, 3.4, "ss", 4], | ||
"nonprimitive": dict(), | ||
"mixed": [1, 2.4, "st", dict()], | ||
"validkey1": "validvalue1", | ||
"intkey": 5, | ||
"floatkey": 3.14, | ||
"boolkey": True, | ||
"valid-byte-string": b"hello-otel", | ||
} | ||
_filter_attributes(attrs_with_invalid_values) | ||
self.assertEqual(len(attrs_with_invalid_values), 5) | ||
self.assertEqual( | ||
attrs_with_invalid_values, | ||
{ | ||
"validkey1": "validvalue1", | ||
"intkey": 5, | ||
"floatkey": 3.14, | ||
"boolkey": True, | ||
"valid-byte-string": "hello-otel", | ||
}, | ||
) | ||
|
||
def test_create_immutable_attributes(self): | ||
attrs = {"key": "value", "pi": 3.14} | ||
immutable = _create_immutable_attributes(attrs) | ||
# TypeError: 'mappingproxy' object does not support item assignment | ||
with self.assertRaises(TypeError): | ||
immutable["pi"] = 1.34 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.