Add rate limit logging to subscription usage GraphQL fields

What does this MR do and why?

Adds rate limit logging to Query.subscriptionUsage and Query.trialUsage.

Both fields share one set of counters, because they use the same client. Each call is counted per user in 3 windows (minute, hour, day). The log line also records the root namespace.

A call over a threshold is logged as Subscription usage rate limit exceeded with throttled: false in additional_details, and still succeeds. The call is only rejected when the enforce_subscription_usage_rate_limits feature flag (gitlab_com_derisk, actor: root namespace) is enabled. The flag is off by default.

The check runs after authorization, so unauthorized calls are not counted. When enforcement is enabled, a rejected call does not build the CustomersDot client.

The check lives in a new concern, GitlabSubscriptions::SubscriptionUsageRateLimitable, so other GitLab Credits fields that call CustomersDot can reuse it. The including class can choose its own rate limit keys and enforce flag. This will be re-used in future MRs, the budget cap mutations use it in !267593 (merged).

This follows the pattern of other place slike Gitlab::Ci::Lint#ci_lint_rate_limited?: count on every call, log when over the threshold, and enforce only behind a flag.

The check uses throttled? rather than throttled_request?, so log-only hits are not written to the auth log as throttled requests.

Thresholds

Window Threshold
minute 300
hour 10,000
day 40,000

The thresholds are based on production usage. The daily window is a fixed window. If enforcement is enabled, a caller over the daily threshold is rejected until the next UTC day.

Rollout

  1. Merge with the flag off. Calls over a threshold are logged when it reaches production.
  2. Review the logs and gitlab_labkit_rate_limiter_rule_evaluations_total{rule=~"limit_subscription_usage_.*", result="block"} for 1 week.
  3. Adjust any threshold that a legitimate caller would exceed.
  4. Decide on enforcement in the rollout issue. Rollback: disable the flag.

Self-managed: after the gitlab_com_derisk flag is removed, the same thresholds apply to self-managed instances. No normal instance gets near them.

References

How to set up and validate locally

  1. In ee/lib/ee/gitlab/application_rate_limiter/labkit_adapter/supported_rate_limits.rb, set the subscription_usage_per_minute limit to 2 and restart Rails.

  2. As a group owner on a SaaS namespace with a subscription (GITLAB_SIMULATE_SAAS=1), run this query 3 times in GraphiQL:

    query {
      subscriptionUsage(namespacePath: "your-group") {
        startDate
        endDate
      }
    }
  3. The third call still returns a result, and log/application_json.log contains Subscription usage rate limit exceeded with additional_details exceeded_rate_limits: 'subscription_usage_per_minute', throttled: false.

  4. In the Rails console, run Feature.enable(:enforce_subscription_usage_rate_limits).

  5. Run the query again. It returns the throttled error.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist.

Edited by Roy Zwambag

Merge request reports

Loading
Loading