feat: emit one analytics event per datastore action / dump request - #17
Merged
Conversation
Adds datastore/analytics.py: a pure-ASGI AnalyticsMiddleware that records every /api/3/action/* and /datastore/dump/* request as one JSON log line on the datastore.analytics logger - the same event shape ckanext-analytics emits, told apart by the service field. Handled errors keep their status; an unhandled crash is recorded as a 500 before it propagates. Attribution rides on the authorize step: the provider's decision now carries the acting username, and RequestContext stashes user / resource / dataset / organization on the request scope for the middleware to pick up (the CKAN provider already fetched them on the way to its verdict). The resource reference falls back to whatever the caller sent - dump path segment, query string, or capped POST body. Gated by ANALYTICS_ENABLED (default false, middleware unmounted).
|
Important Review available on request
Reviews should be triggered manually for repositories with fewer than 10 stars. Select Trigger review above or comment ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Summary
datastore/analytics.py: a pure-ASGIAnalyticsMiddlewarethat records every/api/3/action/*and/datastore/dump/*request as one bare JSON line on thedatastore.analyticslogger — the same event shape ckanext-analytics emits (one shared BigQuery table, told apart by theservicefield). Handled error responses keep their real status; an unhandled crash is recorded as a 500 before it propagates. Probes, docs and the welcome page are excluded by definition.Decision.subjectnow carries the acting CKAN username (instead of the credential hash), andRequestContext.authorizestashesuser/resource/dataset/organizationon the request scope for the middleware — the CKAN provider already fetched those on the way to its verdict, so the event costs nothing extra. Under jwt / anonymous auth only the JWT subject (if any) and the raw reference are recorded.resource_id, or a capped (64 KB) read of the POST body.ANALYTICS_ENABLED(defaultfalse— the middleware stays unmounted). The flag is documented in.env.example; the test suite forces it on inconftest.pythe same way it forcesAUTH_TYPE.Test plan
pytest— 438 passed (includes newtests/test_analytics.py: full field set, auth-decision attribution, error/crash statuses, ingress header handling, disable paths, emitter isolation).ANALYTICS_ENABLED=falsein the environment (CI-like, no developer.env).ruff check datastore testsclean.