Skip to content

feat: emit one analytics event per datastore action / dump request - #17

Merged
Gutts-n merged 2 commits into
mainfrom
feat/analytics-events
Aug 17, 2026
Merged

feat: emit one analytics event per datastore action / dump request#17
Gutts-n merged 2 commits into
mainfrom
feat/analytics-events

Conversation

@sagargg

@sagargg sagargg commented Aug 17, 2026

Copy link
Copy Markdown
Member

Summary

  • New datastore/analytics.py: a pure-ASGI AnalyticsMiddleware that records every /api/3/action/* and /datastore/dump/* request as one bare JSON line on the datastore.analytics logger — the same event shape ckanext-analytics emits (one shared BigQuery table, told apart by the service field). 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.
  • Attribution rides on the authorize step: Decision.subject now carries the acting CKAN username (instead of the credential hash), and RequestContext.authorize stashes user / resource / dataset / organization on 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.
  • When authorize can't name the resource, the middleware falls back to whatever the caller sent — dump path segment, query string resource_id, or a capped (64 KB) read of the POST body.
  • Gated by ANALYTICS_ENABLED (default false — the middleware stays unmounted). The flag is documented in .env.example; the test suite forces it on in conftest.py the same way it forces AUTH_TYPE.

Test plan

  • pytest — 438 passed (includes new tests/test_analytics.py: full field set, auth-decision attribution, error/crash statuses, ingress header handling, disable paths, emitter isolation).
  • Suite also passes with ANALYTICS_ENABLED=false in the environment (CI-like, no developer .env).
  • ruff check datastore tests clean.

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).
@coderabbitai

coderabbitai Bot commented Aug 17, 2026

Copy link
Copy Markdown

Important

Review available on request

  • 🔍 Trigger review

Reviews should be triggered manually for repositories with fewer than 10 stars. Select Trigger review above or comment @coderabbitai review to review the latest changes. For a full review, comment @coderabbitai full review.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 3f2bdbd5-8abf-4e84-bbd4-59a6cbe7363a


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Gutts-n
Gutts-n merged commit 0ab21b9 into main Aug 17, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants