You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(tornado): match http routes in correct order and missing routes certain regex patterns (#17515)
## Description
Two fixes to `_find_route`, the helper that determines the `http.route` tag for Tornado requests.
**1. Route matching order in nested applications**
`_find_route` pushes sub-rules of a nested `Application` onto the front of the processing deque with `extendleft`. Because `extendleft` reverses the input sequence as it inserts, the routes were processed in reverse declaration order — a catch-all defined after a specific route would incorrectly win. Fix: `extendleft(reversed(rule.target.rules))`.
**2. `http.route` missing for complex regex patterns**
Tornado's `PathMatches._path` is `None` whenever the route regex cannot be reversed (e.g. patterns containing non-capturing groups `(?:...)`, lookaheads, or lookbehinds). Previously `http.route` was absent for those routes. This adds `_regex_to_route`, a `lru_cache`-backed helper that converts any regex pattern to a route string: capturing groups become `%s` (consistent with Tornado's own format), non-capturing constructs are kept verbatim.
Both issues were surfaced by Streamlit, which mounts a nested Tornado application with non-trivial route patterns.
**3. Endpoint discovery at startup used to send the wrong path format
`_collect_endpoints` previously sent the regex pattern as the route which does not match the `http.route` behavior.
The discrepancy leads to duplicated endpoints in the Datadog UI.
## Testing
- `test_nested_application_route_order`: nested app with a specific route before a catch-all; asserts the specific route wins. Fails without fix 1.
- `test_complex_pattern_route`: covers a pure non-capturing group route and a mixed non-capturing + capturing group route; asserts `http.route` is populated and capturing groups are rendered as `%s`. Fails without fix 2.
- `test_regex_to_route`: parametrised unit tests for `_regex_to_route` in isolation, covering anchors, positional/named capturing groups, non-capturing groups, lookaheads, nested groups, and escaped parens.
## Risks
None. Both changes only affect route tag computation; request dispatch is handled by Tornado independently.
- This changes only adds `http.route` values for cases that were previously not supported.
## Additional Notes
- Slightly refactored the appsec telemetry tests to allow for the fact that tornado does not support typed path parameters in the route.
Co-authored-by: florentin.labelle <florentin.labelle@datadoghq.com>
0 commit comments