Flagsmith / Flagsmith/flagsmith
Emit CLI usage events from an API middleware
- Dominant language
- Python
- Stars
- 6.6k
- Forks
- 567
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 121
Description
Add middleware to `api/telemetry/middleware.py`, emitting `cli.request.made` once per authenticated CLI request. The middleware holds no state and reads no cache or database.
### Acceptance criteria
- A request whose user-agent matches the CLI pattern, from an authenticated user, emits `cli.request.made` at INFO on the `cli` logger with `cli.version`, `cli.os`, `cli.arch`, `organisation.id`, `status` and `amplitude.user_id`.
- `amplitude.user_id` is `str(request.user.uuid)`.
- `status` is `error` when the response status code is 400 or above, `success` otherwise. The event is emitted after the response is obtained, so a 500 is still recorded.
- A master API key request emits the event with `organisation.id` resolved from the key's organisation. `organisation.id` resolution logic mirrors, or reuses, `MCPUsageLoggerMiddleware`.
- An unauthenticated request emits nothing, whatever its user-agent.
- A user-agent that does not match the pattern emits nothing.
- `cli.version` passes through unchanged.
Contributor guide
Research direction
Start in api/telemetry/middleware.py and read MCPUsageLoggerMiddleware to understand the existing organisation.id resolution path. Trace the authenticated request and response flow, then verify each acceptance criterion, including INFO logging, 4xx/5xx status handling, master API keys, and no-op unauthenticated or non-CLI requests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api, backend, observability
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 76/100