-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtag-object.ts
37 lines (35 loc) · 1.12 KB
/
tag-object.ts
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
const redactableKeys = ["authorization", "x-authorization", "password", "token"];
export function tagObject(currentSpan: any, key: string, obj: any, depth = 0, maxDepth = 10): any {
if (obj === null) {
return currentSpan.setTag(key, obj);
}
if (depth >= maxDepth) {
return currentSpan.setTag(key, redactVal(key, JSON.stringify(obj).substring(0, 5000)));
}
depth += 1;
if (typeof obj === "string") {
let parsed: string;
try {
parsed = JSON.parse(obj);
} catch (e) {
const redacted = redactVal(key, obj.substring(0, 5000));
return currentSpan.setTag(key, redacted);
}
return tagObject(currentSpan, key, parsed, depth, maxDepth);
}
if (typeof obj === "number" || typeof obj === "boolean") {
return currentSpan.setTag(key, obj.toString());
}
if (typeof obj === "object") {
for (const [k, v] of Object.entries(obj)) {
tagObject(currentSpan, `${key}.${k}`, v, depth, maxDepth);
}
}
}
function redactVal(k: string, v: string): string {
const splitKey = k.split(".").pop() || k;
if (redactableKeys.includes(splitKey)) {
return "redacted";
}
return v;
}