chore(flask): emit pre-request web event - #19816
Open
litianningdatadog wants to merge 1 commit into
Open
Conversation
litianningdatadog
requested review from
brettlangdon,
emmettbutler,
juanjux,
rachelyangdog and
wantsui
and removed request for
a team,
brettlangdon,
emmettbutler and
rachelyangdog
August 23, 2026 20:53
litianningdatadog
requested review from
brettlangdon,
dubloom and
emmettbutler
and
a lite review from Copilot
and removed request for
juanjux
August 23, 2026 20:54
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a new internal web-framework event (WebFrameworkEvents.WEB_REQUEST_STARTING) and emits it from the Flask integration at WSGI entry, intended to let downstream logic observe method/path before Flask request context and the root web span are created (motivated by the MicroVM /run identity-refresh ordering constraint).
Changes:
- Add
WebFrameworkEvents.WEB_REQUEST_STARTING = "web.request.starting". - Emit the event from
ddtrace.contrib.internal.flask.patch.patched_wsgi_app()as soon asREQUEST_METHODand path info are available in the WSGI environ. - Add Flask tests asserting the event is emitted (including for 404s) and that emission happens before the Flask WSGI middleware runs.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
ddtrace/contrib/_events/web_framework.py |
Adds the new WEB_REQUEST_STARTING enum value. |
ddtrace/contrib/internal/flask/patch.py |
Dispatches the new pre-request event at Flask WSGI entry. |
tests/contrib/flask/test_microvm_identity_refresh.py |
Adds coverage validating the event is emitted and ordered before tracing/middleware. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+42
to
+54
| events = [] | ||
| environ = {"REQUEST_METHOD": "POST", "PATH_INFO": REQUEST_STARTING_PATH, "SCRIPT_NAME": ""} | ||
|
|
||
| def start_response(status, headers, exc_info=None): | ||
| pass | ||
|
|
||
| def wrapped(environ, start_response): | ||
| return [] | ||
|
|
||
| def dispatch(name, args): | ||
| if name == WebFrameworkEvents.WEB_REQUEST_STARTING.value: | ||
| events.append("starting") | ||
|
|
Comment on lines
+395
to
+397
| core.dispatch( | ||
| WebFrameworkEvents.WEB_REQUEST_STARTING.value, (environ.get("REQUEST_METHOD"), environ.get("PATH_INFO")) | ||
| ) |
This was referenced Aug 23, 2026
|
🔄 Datadog auto-retried 3 jobs - 1 passed on retry 🔗 Commit SHA: d59e112 | Docs | View more details | Give us feedback! |
This was referenced Aug 23, 2026
This was referenced Aug 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The MicroVM
/runrequest has to be observed before a web root span reads runtime identity. This PR keeps the request-starting event coverage scoped to Flask.This adds a
WebFrameworkEvents.WEB_REQUEST_STARTINGevent and emits it from Flask once method and path are known, before request context/root span creation. The event is intentionally not tied to MicroVMs or runtime-id refresh in this PR.This PR replaces #19779 as renaming the branch from
tianning.li/2-web-request-starting-eventtotianning.li/2-flask-web-request-starting-eventled to PR close while it could no longer be associated with another branch.Testing
scripts/lint fmt ddtrace/contrib/internal/flask/patch.pyscripts/lint format_check ddtrace/contrib/_events/web_framework.py ddtrace/contrib/internal/flask/patch.py tests/contrib/flask/test_microvm_identity_refresh.pyscripts/run-tests tests/contrib/flask/test_microvm_identity_refresh.py --venv 1c6c710 -- -- -q tests/contrib/flask/test_microvm_identity_refresh.py(fails during local pytest setup with stale native extension:ImportError: cannot import name 'process_metrics' from 'ddtrace.internal.native._native')Risks
Low. The event is emitted on Flask request entry, but it has no listener in this PR. The no-listener path should be cheap.
Release note
None. This PR adds an internal event emitter with no customer-visible listener. Use
changelog/no-changelog.Additional Notes
Depends on #19778. Follow-up PR adds stdlib
http.serversupport.