Repository navigation
Conversation
Describe API requests in the request context of flagsmith-common, so every event and span emitted while handling them carries who makes them and what they act on, without call sites passing it along: - `user.id`: the user's UUID, for users. Master API keys aren't users, so get none. - `organisation.id`: the master API key's organisation, on authentication; for users, the organisation the permission layer resolves, replacing the span attribute it used to set. Users in several organisations are not guessed at. - `project.id`: from the `project_pk` URL kwarg. DRF authenticates API requests inside the view, so they're bound by wrapping `APIView.perform_authentication`, rather than in a middleware. `environment.id` is not bound: resolving it from `environment_api_key` costs a query, or an environment cache lookup, per request, and views already resolve the environment in many different places. Also: - List `RequestContextMiddleware` first in `MIDDLEWARE`, under its new name, replacing the deprecated `RouteLoggerMiddleware` alias. - `ClientUsageLoggerMiddleware` reads the organisation from the request context, so `telemetry.spans` goes. - `code_references.scan.created` no longer passes `organisation__id`, as the request context always carries the same one.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #9041 +/- ##
========================================
Coverage 98.93% 98.93%
========================================
Files 1716 1720 +4
Lines 72808 72961 +153
========================================
+ Hits 72031 72184 +153
Misses 777 777 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Bind what an API request acts on from its URL, best effort, rather than from whichever permission checks its view happens to run, which left the organisation unbound on many routes, e.g. listing a project's features: - `project_pk`: the project, and its organisation, with a primary key lookup. - `feature_pk`: the feature. - `environment_api_key`: the environment, its project and organisation, with one lookup. Most of these views load the project or environment anyway, but in too many different places to reuse. Deleted entities resolve to nothing. The permission layer no longer binds the organisation.
Split resolving what an API request acts on in two: - `map_view_kwargs_to_resolved_ids` maps a view's URL kwargs to the IDs they carry, without querying. - `telemetry.services.get_resolved_ids` resolves, from the database, only the IDs missing that the ones present imply: an environment's project and organisation, a feature's project and organisation, a project's organisation. Resolving a feature's project also binds the project and organisation on routes that only identify the environment by ID, e.g. publishing an environment feature version. The environment's API key is used to resolve its IDs, but isn't bound.
…ppers` Also resolve the organisation of soft-deleted projects, as including them is harmless, and simplify `get_resolved_ids`' docstring.
There was a problem hiding this comment.
0 issues found across 2 files (changes from recent commits).
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Requires human review: Auto-approval blocked by 4 unresolved issues from previous reviews.
Turn on auto-fix | Re-trigger cubic
@emyller Sorry for not communicating this clearly, but I did explore a bunch of alternatives before stopping in this solution, and it's actually not bad, IMO. There's no way for a middleware to access Regarding |
`str.isdigit` accepts characters, e.g. `²`, that `int` can't parse, so a router-captured `project_pk` or `feature_pk` like that failed the request with a `ValueError`. Accept decimal characters only.
There was a problem hiding this comment.
0 issues found across 2 files (changes from recent commits).
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Requires human review: Auto-approval blocked by 3 unresolved issues from previous reviews.
Turn on auto-fix | Re-trigger cubic
There was a problem hiding this comment.
0 issues found across 8 files (changes from recent commits).
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Requires human review: Auto-approval blocked by 2 unresolved issues from previous reviews.
Turn on auto-fix | Re-trigger cubic
Per review, they were unneeded or, about the environment's API key, misleading.
7323e54 to
f45f114
Compare
There was a problem hiding this comment.
0 issues found across 7 files (changes from recent commits).
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Requires human review: Auto-approval blocked by 2 unresolved issues from previous reviews.
Turn on auto-fix | Re-trigger cubic
…tributes` It holds more than IDs, e.g. the environment's API key, and "resolved IDs" didn't say what for. Rename the functions producing it to match: `map_view_kwargs_to_telemetry_attributes`, and `get_resolved_telemetry_attributes`.
There was a problem hiding this comment.
0 issues found across 6 files (changes from recent commits).
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Requires human review: Auto-approval blocked by 2 unresolved issues from previous reviews.
Turn on auto-fix | Re-trigger cubic
Bind it as `environment.key` on routes identifying the environment by its API key, alongside `environment.id`. The key is public, and `environment.key` is how events already report it, e.g. feature dependency events.
There was a problem hiding this comment.
All reported issues were addressed across 8 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
Turn on auto-fix | Re-trigger cubic
There was a problem hiding this comment.
0 issues found across 2 files (changes from recent commits).
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Requires human review: Auto-approval blocked by 2 unresolved issues from previous reviews.
Turn on auto-fix | Re-trigger cubic
1a1bc6a to
c063801
Compare
Map `organisation_pk` to `organisation.id`, like `project_pk` to `project.id`. It's in the URL, so it needs no query.
There was a problem hiding this comment.
0 issues found across 2 files (changes from recent commits).
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Requires human review: Auto-approval blocked by 1 unresolved issue from previous reviews.
Turn on auto-fix | Re-trigger cubic
Django calls `ready` again when it sets up its apps again, e.g. in tests overriding installed apps, which wrapped `APIView.perform_authentication` once more each time, resolving and binding request context repeatedly.
There was a problem hiding this comment.
0 issues found across 2 files (changes from recent commits).
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Requires human review: Auto-approval blocked by 1 unresolved issue from previous reviews.
Turn on auto-fix | Re-trigger cubic
…context # Conflicts: # api/telemetry/services.py
My apologies, my review sounded like you have not considered other alternatives.
I wouldn't aim at structlog's contextvar:
The practical effect I'd expect is that in every log emitted, we check whether the stored request has an authenticated user, and enrich the log context with it if it's not already. Monkey-patching the exact moment looks smart but it hurts traceability. Mixins are out of question — we're not even 100% CBV. |
emyller
left a comment
There was a problem hiding this comment.
After an offline discussion, I still keep my preference for a contextvar + log processor approach, but won't hold my opinion against monkey-patching strongly. Thanks for addressing the other comments!
Thanks for submitting a PR! Please check the boxes below:
docs/if required so people know about the feature.Changes
In this PR, we add auto-resolution for the following attributes in our structured logs:
organisation.idproject.idfeature.idenvironment.idenvironment.keyThe above attributes now get resolved automagically for any authenticated request, and bound to all logs emitted during the current request lifetime.
user.idstr(request.user.uuid). Master API keys aren't users, so get none (as the issue says)organisation.idorganisation_pkURL kwargorganisation.idrequest.user.key.organisation_idproject.id,organisation.idproject_pkorganisation_idfeature.idfeature_pkenvironment.key,environment.id,project.id,organisation.idenvironment_api_keyThe performance hit is estimated at around +11 ms p95 for our busiest endpoints.
We also stop relying on OTel spans as vessels for request context, and use flagsmith-common's utilities instead.