Attribute AI metrics user_metrics ClickHouse query with log_comment
Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.
Summary
ee/app/graphql/resolvers/analytics/ai_metrics/user_metrics_resolver.rb returns a ClickHouse::Client::QueryBuilder / ClickHouseAggregatedRelation whose query is executed later in the GraphQL pagination connection, and it is currently not attributed to a root namespace in its ClickHouse log_comment.
The ClickHouse guideline asks user-facing queries to be wrapped so root_namespace_id reaches system.query_log, letting FinOps attribute ClickHouse cost per root namespace.
Proposed change
Same one-line pattern already applied to job analytics in !255731 (merged): set attribution_namespace on the returned relation in the resolver (to a namespace record, e.g. the group/project namespace in scope), so ClickHouseAggregatedConnection#execute_query wraps the deferred query in Gitlab::ApplicationContext.with_context(namespace:).
References
- Follow-up from !255731 (merged) (job analytics attribution).
- Parent work: gitlab-org/gitlab#627521.
- Doc: https://docs.gitlab.com/development/database/clickhouse/clickhouse_within_gitlab/#query-attribution-with-log_comment