-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtag_object.py
68 lines (59 loc) · 2.04 KB
/
tag_object.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Unless explicitly stated otherwise all files in this repository are licensed
# under the Apache License Version 2.0.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2021 Datadog, Inc.
from decimal import Decimal
import logging
import ujson as json
redactable_keys = ["authorization", "x-authorization", "password", "token"]
max_depth = 10
logger = logging.getLogger(__name__)
def tag_object(span, key, obj, depth=0):
if obj is None:
return span.set_tag(key, obj)
if depth >= max_depth:
return span.set_tag(key, _redact_val(key, str(obj)[0:5000]))
depth += 1
if _should_try_string(obj):
parsed = None
try:
parsed = json.loads(obj)
return tag_object(span, key, parsed, depth)
except ValueError:
redacted = _redact_val(key, obj[0:5000])
return span.set_tag(key, redacted)
if isinstance(obj, int) or isinstance(obj, float) or isinstance(obj, Decimal):
return span.set_tag(key, str(obj))
if isinstance(obj, list):
for k, v in enumerate(obj):
formatted_key = f"{key}.{k}"
tag_object(span, formatted_key, v, depth)
return
if hasattr(obj, "items"):
for k, v in obj.items():
formatted_key = f"{key}.{k}"
tag_object(span, formatted_key, v, depth)
return
if hasattr(obj, "to_dict"):
for k, v in obj.to_dict().items():
formatted_key = f"{key}.{k}"
tag_object(span, formatted_key, v, depth)
return
try:
value_as_str = str(obj)
except Exception:
value_as_str = "UNKNOWN"
return span.set_tag(key, value_as_str)
def _should_try_string(obj):
try:
if isinstance(obj, str) or isinstance(obj, unicode):
return True
except NameError:
if isinstance(obj, bytes):
return True
return False
def _redact_val(k, v):
split_key = k.split(".").pop() or k
if split_key in redactable_keys:
return "redacted"
return v