forked from DataDog/datadog-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtag_object.py
More file actions
60 lines (51 loc) · 1.71 KB
/
tag_object.py
File metadata and controls
60 lines (51 loc) · 1.71 KB
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
# 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 json
import logging
redactable_keys = ["authorization", "x-authorization", "password", "token"]
max_depth = 10
logger = logging.getLogger(__name__)
def tag_object(span, key, obj, depth=0):
if depth >= max_depth:
return
else:
depth += 1
if obj is None:
return span.set_tag(key, obj)
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, obj)
if isinstance(obj, list):
for k, v in enumerate(obj):
formatted_key = "{}.{}".format(key, k)
tag_object(span, formatted_key, v, depth)
return
if isinstance(obj, object):
for k in obj:
v = obj.get(k)
formatted_key = "{}.{}".format(key, k)
tag_object(span, formatted_key, v, depth)
return
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