Skip to content

chore(flask): emit pre-request web event - #19816

Open
litianningdatadog wants to merge 1 commit into
tianning.li/1-runtime-identity-refreshfrom
tianning.li/2-flask-web-request-starting-event
Open

chore(flask): emit pre-request web event#19816
litianningdatadog wants to merge 1 commit into
tianning.li/1-runtime-identity-refreshfrom
tianning.li/2-flask-web-request-starting-event

Conversation

@litianningdatadog

@litianningdatadog litianningdatadog commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Description

The MicroVM /run request 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_STARTING event 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-event to tianning.li/2-flask-web-request-starting-event led to PR close while it could no longer be associated with another branch.

Testing

  • scripts/lint fmt ddtrace/contrib/internal/flask/patch.py
  • scripts/lint format_check ddtrace/contrib/_events/web_framework.py ddtrace/contrib/internal/flask/patch.py tests/contrib/flask/test_microvm_identity_refresh.py
  • scripts/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.server support.

@litianningdatadog litianningdatadog added the changelog/no-changelog A changelog entry is not required for this PR. label Aug 23, 2026
@litianningdatadog
litianningdatadog requested a review from a team as a code owner August 23, 2026 20:53
@litianningdatadog litianningdatadog added the aws-microvm Work related to AWS MicroVM onboarding label Aug 23, 2026
@litianningdatadog
litianningdatadog requested a review from a team as a code owner August 23, 2026 20:53
@litianningdatadog
litianningdatadog requested review from brettlangdon, dubloom and emmettbutler and a lite review from Copilot and removed request for juanjux August 23, 2026 20:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 as REQUEST_METHOD and 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"))
)
@datadog-datadog-us1-prod

datadog-datadog-us1-prod Bot commented Aug 23, 2026

Copy link
Copy Markdown

Tests

🔄 Datadog auto-retried 3 jobs - 1 passed on retry View in Datadog

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: d59e112 | Docs | View more details | Give us feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

aws-microvm Work related to AWS MicroVM onboarding changelog/no-changelog A changelog entry is not required for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants