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_analyticsrule in the EE rate limit registry, defined withstaged_rule. Its characteristics areuser ip namespaceand 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_enforcefeature 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_limitsetting in therate_limitsJSONB column: default60,0disables 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_actionthat 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_stateandgitlab_labkit_rate_limiter_rule_evaluations_totalon GitLab.com before blocking anyone. The Insights limit (!267890 (merged)) shipped without a flag becausestaged_ruledid not exist yet. After the rollout,staged_rulecan 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.rbandee/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;0disables 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,
ApplicationSettingand 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
- In a GDK with a licensed group that has issues, enable the flag:
Feature.enable(:issues_analytics_rate_limit_enforce)in a Rails console. - Set the limit to 1:
PUT /api/v4/application/settings?issues_analytics_limit=1as an admin. - Request
/groups/<group>/-/issues_analytics.jsontwice within a minute. The second request should return 429. /groups/<group>/-/issues_analytics(the HTML page) should still load.- Run
Feature.disable(:issues_analytics_rate_limit_enforce). Repeated requests should no longer return 429, andrate_limit_statein the request log should show the_logrule. - 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