Make the Flask integration points work on Flask 2.1+ / 3.x - #50
Open
BinaryFiddler wants to merge 2 commits into
Open
BinaryFiddler wants to merge 2 commits into
BinaryFiddler wants to merge 2 commits into
Conversation
Three spots assume Flask 1.x and break on a modern Flask. Discord's smite services consume osprey-worker and are moving to Flask 3.1, which surfaced all three; none of these changes alter behavior on the current Flask 1.x pin. - ddtrace middleware read `flask.signals.signals_available`, removed in Flask 3.0 when blinker became a hard dependency. On Flask 3 this is an AttributeError in `TraceMiddleware.__init__`, so any app calling `ddtrace_utils.init_app()` fails at startup. `self.use_signals` is already assigned earlier in the constructor, so the feature-detect just goes away. - `_patch_render` monkeypatched `flask.templating._render` with the 1.x signature `(template, context, app)`. Flask 2.2 reordered it to `(app, template, context)`, so the patched function received its arguments transposed and template rendering broke. - `ui_api`'s `_register_with_prefix` registered the same blueprint object twice (bare and under /api) under one name. Flask 2.1+ raises `ValueError: The name '<x>' is already registered for this blueprint`. The bare registration stays unnamed so its endpoint names -- what `url_for()` resolves against -- are unchanged; only the /api copy takes a suffixed name. Both URL paths keep routing exactly as before. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The previous approach gave the /api registration its own `name=` to satisfy Flask 2.1's uniqueness check. That routes correctly, but it renames the endpoint for every /api route (`alerts.create_alert` -> `alerts-api.create_alert`), silently breaking anything keyed on `request.endpoint` -- audit/exemption lookups and ddtrace span resource names among them. Under Flask 1.x both paths shared one endpoint name, so consumers never had to care which prefix a request arrived on. Register the blueprint once, then mirror its rules under /api reusing the same endpoint and view function. `add_url_rule` allows a repeated endpoint when the view function is identical, so the served URL set, `url_for()` output and `request.endpoint` are all byte-identical to the Flask 1.x behavior. Measured against Flask 1.1.4 with the consuming service's real blueprints: 0 routes lost/gained, 0 url_for changes, and 0 of 252 paths change `request.endpoint` (the named-registration approach changed 125). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Three spots in
osprey_workerassume Flask 1.x and break on a modern Flask. Discord's smite services consumeosprey-workerand are moving to Flask 3.1 (companion PR: discord/discord#311325), which surfaced all three.None of these change behavior on the current Flask 1.x pin — they are version-compatibility fixes.
1.
flask.signals.signals_available— removed in Flask 3.0TraceMiddleware.__init__read it to feature-detect blinker. Flask 3.0 made blinker a hard dependency and deleted the attribute, so any app callingddtrace_utils.init_app()dies at startup:self.use_signalsis already assigned earlier in the constructor, so the feature-detect (and its now-unreachable_blinker_not_installed_msg) simply goes away. Verified:hasattrisTrueon 1.1.4,Falseon 3.1.3.2.
flask.templating._renderwas reordered in Flask 2.2_patch_rendermonkeypatched it with the 1.x signature, so the patched function received its arguments transposed:inspect.signature(flask.templating._render)(template, context, app)(app, template, context)Re-checked end-to-end after the fix:
render_template_string('hi {{ n }}', n=42)returns'hi 42'and the trace span still fires, on both versions.3. Duplicate blueprint registration —
ValueErroron Flask 2.1+ui_api's_register_with_prefixregistered the same blueprint object twice (bare + under/api) under one name:Giving the
/apicopy its ownname=satisfies Flask, but renames the endpoint for every/apiroute (alerts.create_alert→alerts-api.create_alert), silently breaking anything keyed onrequest.endpoint— audit/exemption lookups and ddtrace span resource names among them. Under Flask 1.x both paths shared one endpoint, so consumers never had to care which prefix a request arrived on. (In the consuming smite service this would have leaked PII into an audit log; found by Bugbot there.)So: register the blueprint once, then mirror its rules under
/apireusing the same endpoint and view function.add_url_rulepermits a repeated endpoint when the view function is identical.Diffed across both Flask versions using the consuming service's real 18 blueprints / 125 routes:
name=approach(rule, methods)pairs servedurl_for()endpoints resolvablerequest.endpointchangedVerification
smite_ui_api) on Flask 3.1.3 / Werkzeug 3.1.8, which previously failed at Long-running UDF support: per-stream window for async worker/coordinator #1./apitwin and vice versa./_health→ 200,/watchers/meta→ 200; every probed route identical bare vs/api.ruff check+ruff format --checkclean.🤖 Generated with Claude Code