Skip to content

Commit 73122a2

Browse files
committed
feat(boto3): Add attribute extraction helpers
1 parent 6462197 commit 73122a2

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import TYPE_CHECKING
2+
3+
if TYPE_CHECKING:
4+
from typing import Any, Callable, Optional, Sequence, Tuple
5+
6+
from sentry_sdk._types import Attributes
7+
8+
_Converter = Callable[[Any], Optional[Any]]
9+
# e.g. ("Limit", "aws.dynamodb.limit", _as_integer) converts
10+
# {"Limit": 10} into {"aws.dynamodb.limit": 10} using `_extract_attributes()`
11+
_AttributeSpec = Tuple[str, str, _Converter]
12+
13+
14+
def _as_integer(value: "Any") -> "Optional[int]":
15+
if isinstance(value, int) and not isinstance(value, bool):
16+
return value
17+
return None
18+
19+
20+
def _as_string(value: "Any") -> "Optional[str]":
21+
return value if isinstance(value, str) and value else None
22+
23+
24+
def _extract_attributes(
25+
source: "Any", specs: "Sequence[_AttributeSpec]"
26+
) -> "Attributes":
27+
if not isinstance(source, dict):
28+
return {}
29+
30+
attributes = {}
31+
for param, attribute, convert in specs:
32+
value = convert(source.get(param))
33+
# an unexpected type results in that attribute being omitted.
34+
if value is not None:
35+
attributes[attribute] = value
36+
return attributes

0 commit comments

Comments
 (0)