Attribute AI user metrics ClickHouse query to the root namespace

What does this MR do and why?

The aiUserMetrics GraphQL field returns a ClickHouseAggregatedRelation, and the ClickHouse query only runs later inside the pagination connection, so it was sent without a root_namespace_id in its log_comment. This MR passes context_attributes: { namespace: namespace } when building the relation in UserMetricsResolver, the same pattern used for job analytics in !255731 (merged). ClickHouseAggregatedConnection#execute_query then runs the query inside Gitlab::ApplicationContext.with_context, so the cost can be tracked per root namespace.

The per-user feature metric queries in Types::Analytics::AiMetrics::UserMetricsType#fetch_metrics_for_user_ids run outside the connection and are not covered here. They can be attributed in a follow-up by wrapping the select in Gitlab::ApplicationContext.with_context(namespace: ai_metrics_params[:namespace]).

Related to #630890.

References

Same pattern as !255731 (merged).

Screenshots or screen recordings

Not applicable — backend change.

How to set up and validate locally

  1. Run bundle exec rspec ee/spec/requests/api/graphql/analytics/ai_analytics/ai_user_metrics_spec.rb.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist.

This contribution was prepared with the help of an AI assistant. I reviewed the changes and take responsibility for them.

Edited by Anish Kumar

Merge request reports

Loading
Loading