Rate limit Issue Analytics JSON requests

What does this MR do and why?

The Issue Analytics JSON endpoints (group and project) have no rate limit, so one client, signed in or not, can repeat chart requests against a large public group or project as often as it likes. On GitLab.com a single request for a very large group holds a replica for the full 15 s statement timeout. This MR adds an issues_analytics rate limit: 60 requests a minute by default, counted per user and group or project, or per IP address and group or project for anonymous callers. HTML page loads are not limited.

The limit ships in log mode behind the issues_analytics_rate_limit_enforce feature flag, which is disabled by default. While the flag is disabled, requests over the limit are only logged. After the flag is enabled, requests over the limit return 429.

Part of https://gitlab.com/gitlab-org/gitlab/-/work_items/636126 (Project Bulkhead, https://gitlab.com/gitlab-org/gitlab/-/work_items/635036). This is the rate limit MR of the three in the issue's plan. It is a backstop against repeated or scripted calls. It does not make a single request cheaper, so it does not fix the timeouts at the default 12-month range.

Changes

  • New issues_analytics rule in the EE rate limit registry, defined with staged_rule. Its characteristics are user ip namespace and its period is one minute. The group controller passes the group as the namespace, and the project controller passes the project namespace.
  • New issues_analytics_rate_limit_enforce feature flag (gitlab_com_derisk, default_enabled: false). The actor is the root ancestor of the namespace, so enabling the flag for a top-level group also covers its subgroups and projects.
  • New issues_analytics_limit setting in the rate_limits JSONB column: default 60, 0 disables the limit. It is exposed on the application settings API and in the JSON schema, and documented in the settings API docs, the OpenAPI file and the Issue Analytics page.
  • Both controllers check the limit in a before_action that runs after the authorization checks and only for JSON requests.

Design notes

  • Rolled out in log mode. The rate limits guide asks new keys to start in log mode behind a flag, so that we can watch rate_limit_state and gitlab_labkit_rate_limiter_rule_evaluations_total on GitLab.com before blocking anyone. The Insights limit (!267890 (merged)) shipped without a flag because staged_rule did not exist yet. After the rollout, staged_rule can be replaced with a plain rule and the flag removed.
  • Plain 429 response. After the flag is enabled, the endpoint returns the standard throttle message, the same as other controller limits.
  • Chart behavior on a 429. Only the Premium chart (issues_analytics_chart.vue) calls the JSON endpoint. On a failed request its store shows the existing "An error occurred while loading chart data" alert, and the chart falls back to the empty state. The enhanced Ultimate chart uses GraphQL and is not affected. No frontend change in this MR.
Verification
  • ee/spec/requests/groups/issues_analytics_controller_spec.rb and ee/spec/requests/projects/analytics/issues_analytics_controller_spec.rb: with the flag enabled, anonymous callers get 429 after the limit; each IP, each group or project, and a signed-in user count separately; 0 disables the limit. With the flag disabled, requests over the limit still return 200.
  • ee/spec/support/shared_examples/controllers/analytics/issues_analytics/shared_issues_analytics_examples.rb: HTML requests are not limited. This runs in both controller specs.
  • Registry, ApplicationSetting and settings API specs updated for the new key and setting.
  • Passing locally: the controller and request specs for both controllers, the CE and EE registry specs (116 examples in total), RuboCop and the docs linters.
  • Tested in GDK with the limit set to 2, before the feature flag was added: requests 1 and 2 returned chart data, request 3 returned a 429, and the endpoint recovered after a minute. The HTML page was not limited. The manual run with the flag in place (enabled and disabled) is covered by the request specs and not yet repeated by hand.

How to test

  1. In a GDK with a licensed group that has issues, enable the flag: Feature.enable(:issues_analytics_rate_limit_enforce) in a Rails console.
  2. Set the limit to 1: PUT /api/v4/application/settings?issues_analytics_limit=1 as an admin.
  3. Request /groups/<group>/-/issues_analytics.json twice within a minute. The second request should return 429.
  4. /groups/<group>/-/issues_analytics (the HTML page) should still load.
  5. Run Feature.disable(:issues_analytics_rate_limit_enforce). Repeated requests should no longer return 429, and rate_limit_state in the request log should show the _log rule.
  6. Set issues_analytics_limit=0. Repeated requests should no longer be limited with the flag enabled.

Not covered here

  • The default 12-month load on the largest groups, discussed on the issue. That needs a cheaper query, not a rate limit.
  • Enabling the flag. That follows the rollout on GitLab.com.

Checklist

  • Specs added
  • Docs updated
  • Add the MR link to the "Introduced in" history lines in the docs
  • Review the log-mode metrics on GitLab.com, then enable issues_analytics_rate_limit_enforce

🤖 Generated with Claude Code

Edited by Fardeen Khan

Merge request reports

Loading
Loading