Add rate limit log and feature flag to billable user change preview

What does this MR do and why?

  • Add a per-user rate limit to the gitlabSubscriptionsPreviewBillableUserChange GraphQL field. The limit comes from the preview_billable_user_change_limit application setting, which defaults to 0 and disables the limit.
  • Rate limit is behind the preview_billable_user_change_rate_limit feature flag.
    • When disabled, requests over the limit are only logged.
    • When enabled, requests over the limit return a RateLimitedError with extensions.error_type: rate_limited.
  • gitlabSubscriptionsPreviewBillableUserChange is called in the following frontend components. A follow-up MR will update all frontend callers to show the error in the UI:

Follow-up

After this MR is merged and deployed, a production change request sets preview_billable_user_change_limit to 60 on GitLab.com. Until then, nothing is logged, because the limiter skips a limit of 0. The change request is tracked in the rollout issue.

The threshold is an initial value that will be adjusted as we receive log data before globally enabling the feature flag.

Expected rate_limit_state when the setting is above 0:

Scenario Feature Flag Rule and result following applimiter_gitlab_subscriptions_preview_billable_user_change:
Under the limit Off limit_subscriptions_preview_billable_user_change_by_user_log:allow
Over the limit, request succeeds Off limit_subscriptions_preview_billable_user_change_by_user_log:log
Under the limit On limit_subscriptions_preview_billable_user_change_by_user:allow
Over the limit, request rejected On limit_subscriptions_preview_billable_user_change_by_user:block

Stacked MRs

Order MR What it does
1 This MR backend - per-user rate limit on the field, behind preview_billable_user_change_rate_limit
2 !272001 frontend - show the rate limit error in the three callers

References

Screenshots or screen recordings

Before After
637060-rate-limit-graphql-before 637060-rate-limit-graphql-after

How to set up and validate locally

  1. In rails console, set the application setting limit to 2:

    ApplicationSetting.current.update!(preview_billable_user_change_limit: 2)
  2. As an owner of a top-level group, run this query in GraphiQL 3 times:

    query {
      group(fullPath: "<group_path>") {
        gitlabSubscriptionsPreviewBillableUserChange(addUserIds: [<user_id>], role: DEVELOPER) {
          newBillableUserCount
        }
      }
    }

    All 3 return data. In log/development_json.log, the third request's rate_limit_state includes applimiter_gitlab_subscriptions_preview_billable_user_change:limit_subscriptions_preview_billable_user_change_by_user_log:log.

  3. In the Rails console, enable the flag for your user:

    Feature.enable(:preview_billable_user_change_rate_limit, User.find_by_username('<username>'))
  4. Run the query 3 more times. The third returns null with the error This endpoint has been requested too many times. Try again later. and extensions.error_type set to rate_limited.

    In log/development_json.log, the third request's rate_limit_state includes applimiter_gitlab_subscriptions_preview_billable_user_change:limit_subscriptions_preview_billable_user_change_by_user:block.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Sandra To

Merge request reports

Loading
Loading