-
Notifications
You must be signed in to change notification settings - Fork 608
Expand file tree
/
Copy pathfind_undocumented_set_data_keys.py
More file actions
286 lines (232 loc) · 9.47 KB
/
find_undocumented_set_data_keys.py
File metadata and controls
286 lines (232 loc) · 9.47 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""
Best-effort script to find all attribute names set by the SDK that are not documented in sentry-conventions.
Install both the `sentry_sdk` and `sentry_conventions` packages in your environment to run the script.
"""
from dataclasses import dataclass
import ast
import re
from pathlib import Path
from typing import Any
from sentry_sdk.consts import SPANDATA
from sentry_conventions.attributes import ATTRIBUTE_NAMES
ALLOWED_EXPRESSION_PATTERNS_FOR_UNRESOLVED_KEYS = {
# User-provided attributes in the `sentry_data` keyword argument of the `ai_track` decorator.
Path("sentry_sdk") / "ai" / "monitoring.py": [r"^k$"],
# Caller provides parameter name to `set_data_normalized()`.
Path("sentry_sdk") / "ai" / "utils.py": [r"^key$"],
# OTel span attributes from external instrumentation.
Path("sentry_sdk") / "integrations" / "opentelemetry" / "span_processor.py": [
r"^key$"
],
# Rust extensions instrumented with the `tracing` crate.
Path("sentry_sdk") / "integrations" / "rust_tracing.py": [r"^field$", r"^key$"],
# Determined based on MCP tool parameters only known at runtime.
Path("sentry_sdk") / "integrations" / "mcp.py": [r"mcp\.request\.argument"],
}
@dataclass
class ResolvedSetDataKey:
value: str
path: Path
line_number: int
@dataclass
class UnresolvedSetDataKey:
argument_expression: str
path: Path
line_number: int
def try_eval(node: "ast.expr", namespace: "dict[str, Any]", path: "Path") -> "Any":
"""
Evaluate expressions that can be statically resolved with the namespace.
"""
try:
return eval(compile(ast.Expression(body=node), path, "eval"), namespace)
except Exception:
return None
def evaluate_dictionary_keys(
node: "ast.Dict", namespace: "dict[str, Any]", path: "Path"
) -> "dict[str, None] | None":
"""
Evaluate dict literal keys that can be statically resolved with the namespace.
"""
partial = {}
for key_node in node.keys:
if key_node is None:
continue
resolved_key = try_eval(node=key_node, namespace=namespace, path=path)
if resolved_key is None:
continue
# Dictionary values do not matter as attribute names only appear as dictionary keys.
partial[resolved_key] = None
return partial
def build_file_namespace(
tree: "ast.AST", namespace: "dict[str, Any]", path: "Path"
) -> "dict[str, Any]":
"""
Walk tree and add assignment targets to the namespace, including annotated assignments and subscripted assignments.
"""
for node in ast.walk(tree):
if isinstance(node, ast.Assign) and len(node.targets) == 1:
target, value = node.targets[0], node.value
elif isinstance(node, ast.AnnAssign) and node.value is not None:
target, value = node.target, node.value
else:
continue
if isinstance(target, ast.Name):
val = try_eval(node=value, namespace=namespace, path=path)
if val is not None:
namespace[target.id] = val
elif isinstance(value, ast.Dict):
# Store dictionary with the subset of keys that could be statically resolved in the namespace.
namespace[target.id] = evaluate_dictionary_keys(value, namespace, path)
elif (
isinstance(target, ast.Subscript)
and isinstance(target.value, ast.Name)
and target.value.id in namespace
and isinstance(namespace[target.value.id], dict)
):
key = try_eval(
node=target.slice,
namespace=namespace,
path=path,
)
if key is None:
continue
namespace[target.value.id][key] = None
return namespace
def get_key_node(node: "ast.Call") -> "ast.expr | None":
"""
Get attribute key if the node is either a `Span.set_data()` method call or a `set_data_normalized()` function call.
"""
if isinstance(node.func, ast.Attribute) and node.func.attr == "set_data":
return node.args[0]
if isinstance(node.func, ast.Name) and node.func.id == "set_data_normalized":
return node.args[1]
return None
class SetDataKeysCollector(ast.NodeVisitor):
"""
AST traversal that collects all attribute keys passed to functions that set an attribute on a span.
A best-effort name resolution evaluates expressions given the namespace and by tracing for loop
variables.
"""
def __init__(self, namespace: "dict[str, Any]", path: "Path") -> None:
self.namespace = namespace
self.path = path
self.for_stack: "list[ast.For]" = []
self.resolved: "list[ResolvedSetDataKey]" = []
self.unresolved: "list[UnresolvedSetDataKey]" = []
def visit_For(self, node: "ast.For") -> None:
self.for_stack.append(node)
self.generic_visit(node)
self.for_stack.pop()
def visit_Call(self, node: "ast.Call") -> None:
key_node = get_key_node(node)
if key_node is not None:
self._resolve(node, key_node)
self.generic_visit(node)
def _resolve(self, node: "ast.Call", key_node: "ast.expr") -> None:
direct = try_eval(key_node, self.namespace, self.path)
if direct is not None:
self.resolved.append(
ResolvedSetDataKey(
value=direct,
path=self.path,
line_number=node.lineno,
)
)
return
if self.for_stack:
set_data_keys = self._eval_via_loop(key_node)
if set_data_keys:
self.resolved += [
ResolvedSetDataKey(
value=value, path=self.path, line_number=node.lineno
)
for value in set_data_keys
]
return
# The key is considered unresolved as neither direct evaluation nor resolution via loop variables worked.
self.unresolved.append(
UnresolvedSetDataKey(
argument_expression=ast.unparse(key_node),
path=self.path,
line_number=node.lineno,
)
)
def _eval_via_loop(self, key_node: "ast.expr") -> "list[str] | None":
for_node = self.for_stack[-1]
# Trick: build a list comprehension that mirrors the for loop statement.
list_comprehension = ast.ListComp(
elt=key_node,
generators=[
ast.comprehension(
target=for_node.target,
iter=for_node.iter,
ifs=[],
is_async=0,
)
],
)
ast.fix_missing_locations(
list_comprehension
) # Adds information required to call compile.
try:
values = eval(
compile(ast.Expression(body=list_comprehension), self.path, "eval"),
self.namespace,
)
except NameError:
return None
return values
def format_unknown_resolved_attributes(keys: "list[ResolvedSetDataKey]") -> "str":
lines = [
"The following resolved string attribute names are not in sentry-conventions:\n"
]
for key in keys:
lines.append(f"{key.value} ({key.path}:{key.line_number})")
return "\n".join(lines) + "\n"
def format_unresolved_attributes(keys: "list[UnresolvedSetDataKey]") -> "str":
lines = [
"The following unresolved expressions for attribute names may not be in sentry-conventions:\n"
]
for key in keys:
lines.append(f"{key.argument_expression} ({key.path}:{key.line_number})")
return "\n".join(lines)
def main():
# Includes special attributes (with double underscores), but only proper attributes should match.
convention_keys = vars(ATTRIBUTE_NAMES).values()
all_resolved: "list[ResolvedSetDataKey]" = []
all_unresolved: "list[UnresolvedSetDataKey]" = []
for path in Path("sentry_sdk").rglob("*.py"):
tree = ast.parse(path.read_text(), path)
# A limited namespace is used to resolve keys as resolution is best-effort and some keys depend on runtime values.
# In practice, span attribute names are often stored as dictionary keys in the same file.
namespace = build_file_namespace(
tree=tree,
namespace={"SPANDATA": SPANDATA},
path=path,
)
collector = SetDataKeysCollector(namespace=namespace, path=path)
collector.visit(tree)
all_resolved += collector.resolved
all_unresolved += collector.unresolved
unknown_resolved_keys = [
resolved for resolved in all_resolved if resolved.value not in convention_keys
]
truly_unresolved = []
for unresolved_set_data in all_unresolved:
patterns = ALLOWED_EXPRESSION_PATTERNS_FOR_UNRESOLVED_KEYS.get(
unresolved_set_data.path
)
if patterns and any(
re.search(p, unresolved_set_data.argument_expression) for p in patterns
):
continue
truly_unresolved.append(unresolved_set_data)
if unknown_resolved_keys or truly_unresolved:
exc = Exception("Undocumented attributes not in the allow-list detected.")
if unknown_resolved_keys:
exc.add_note(format_unknown_resolved_attributes(unknown_resolved_keys))
if truly_unresolved:
exc.add_note(format_unresolved_attributes(truly_unresolved))
raise exc
if __name__ == "__main__":
main()