Skip to content

Commit 080274d

Browse files
authored
enhancing tag_object (#367)
* enhancing tag_object * format
1 parent 5958375 commit 080274d

File tree

2 files changed

+64
-14
lines changed

2 files changed

+64
-14
lines changed

datadog_lambda/tag_object.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,27 @@ def tag_object(span, key, obj, depth=0):
2828
redacted = _redact_val(key, obj[0:5000])
2929
return span.set_tag(key, redacted)
3030
if isinstance(obj, int) or isinstance(obj, float) or isinstance(obj, Decimal):
31-
return span.set_tag(key, obj)
31+
return span.set_tag(key, str(obj))
3232
if isinstance(obj, list):
3333
for k, v in enumerate(obj):
3434
formatted_key = "{}.{}".format(key, k)
3535
tag_object(span, formatted_key, v, depth)
3636
return
37-
if isinstance(obj, object):
38-
for k in obj:
39-
v = obj.get(k)
37+
if hasattr(obj, "items"):
38+
for k, v in obj.items():
4039
formatted_key = "{}.{}".format(key, k)
4140
tag_object(span, formatted_key, v, depth)
4241
return
42+
if hasattr(obj, "to_dict"):
43+
for k, v in obj.to_dict().items():
44+
formatted_key = "{}.{}".format(key, k)
45+
tag_object(span, formatted_key, v, depth)
46+
return
47+
try:
48+
value_as_str = str(obj)
49+
except Exception:
50+
value_as_str = "UNKNOWN"
51+
return span.set_tag(key, value_as_str)
4352

4453

4554
def _should_try_string(obj):

tests/test_tag_object.py

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ def test_tag_object(self):
1919
tag_object(spanMock, "function.request", payload)
2020
spanMock.set_tag.assert_has_calls(
2121
[
22-
call("function.request.vals.0.thingOne", 1),
23-
call("function.request.vals.1.thingTwo", 2),
22+
call("function.request.vals.0.thingOne", "1"),
23+
call("function.request.vals.1.thingTwo", "2"),
2424
call("function.request.hello", "world"),
2525
call("function.request.anotherThing.blah", None),
2626
call("function.request.anotherThing.foo", "bar"),
27-
call("function.request.anotherThing.nice", True),
27+
call("function.request.anotherThing.nice", "True"),
2828
],
2929
True,
3030
)
@@ -40,12 +40,12 @@ def test_redacted_tag_object(self):
4040
tag_object(spanMock, "function.request", payload)
4141
spanMock.set_tag.assert_has_calls(
4242
[
43-
call("function.request.vals.0.thingOne", 1),
44-
call("function.request.vals.1.thingTwo", 2),
43+
call("function.request.vals.0.thingOne", "1"),
44+
call("function.request.vals.1.thingTwo", "2"),
4545
call("function.request.authorization", "redacted"),
4646
call("function.request.anotherThing.blah", None),
4747
call("function.request.anotherThing.password", "redacted"),
48-
call("function.request.anotherThing.nice", True),
48+
call("function.request.anotherThing.nice", "True"),
4949
],
5050
True,
5151
)
@@ -62,7 +62,7 @@ def test_json_tag_object(self):
6262
call("function.request.token", "redacted"),
6363
call("function.request.jsonString.stringifyThisJson.0.here", "is"),
6464
call("function.request.jsonString.stringifyThisJson.0.an", "object"),
65-
call("function.request.jsonString.stringifyThisJson.0.number", 1),
65+
call("function.request.jsonString.stringifyThisJson.0.number", "1"),
6666
],
6767
True,
6868
)
@@ -79,18 +79,59 @@ def test_unicode_tag_object(self):
7979
call("function.request.token", "redacted"),
8080
call("function.request.jsonString.stringifyThisJson.0.here", "is"),
8181
call("function.request.jsonString.stringifyThisJson.0.an", "object"),
82-
call("function.request.jsonString.stringifyThisJson.0.number", 1),
82+
call("function.request.jsonString.stringifyThisJson.0.number", "1"),
8383
],
8484
True,
8585
)
8686

8787
def test_decimal_tag_object(self):
88-
payload = {"myValue": Decimal(500.50)}
88+
payload = {"myValue": Decimal(500.5)}
8989
spanMock = MagicMock()
9090
tag_object(spanMock, "function.request", payload)
9191
spanMock.set_tag.assert_has_calls(
9292
[
93-
call("function.request.myValue", Decimal(500.50)),
93+
call("function.request.myValue", "500.5"),
94+
],
95+
True,
96+
)
97+
98+
class CustomResponse(object):
99+
"""
100+
For example, chalice.app.Response class
101+
"""
102+
103+
def __init__(self, body, headers=None, status_code: int = 200):
104+
self.body = body
105+
if headers is None:
106+
headers = {}
107+
self.headers = headers
108+
self.status_code = status_code
109+
110+
def __str__(self):
111+
return str(self.body)
112+
113+
class ResponseHasToDict(CustomResponse):
114+
def to_dict(self):
115+
return self.headers
116+
117+
def test_custom_response(self):
118+
payload = self.CustomResponse({"hello": "world"}, {"key1": "val1"}, 200)
119+
spanMock = MagicMock()
120+
tag_object(spanMock, "function.response", payload)
121+
spanMock.set_tag.assert_has_calls(
122+
[
123+
call("function.response", "{'hello': 'world'}"),
124+
],
125+
True,
126+
)
127+
128+
def test_custom_response_to_dict(self):
129+
payload = self.ResponseHasToDict({"hello": "world"}, {"key1": "val1"}, 200)
130+
spanMock = MagicMock()
131+
tag_object(spanMock, "function.response", payload)
132+
spanMock.set_tag.assert_has_calls(
133+
[
134+
call("function.response.key1", "val1"),
94135
],
95136
True,
96137
)

0 commit comments

Comments
 (0)