Add feature_category to database query log comments

What does this MR do and why?

Query log comments carry application, correlation_id, endpoint_id and database info, but not feature category, even though it's already available in the Labkit context for every web request and Sidekiq job. That makes it hard to attribute a problematic query seen in pg_stat_activity or slow-query logs to a feature and owning team.

Add a feature_category tag to the query log tags, read from the Labkit context the same way as endpoint_id. It's appended last because Gitlab::Database::StatActivitySampler parses the comment with a positional regexp, so inserting a tag mid-list would break its db_config_database capture.

Resolves #523080 (closed).

How to set up and validate locally

  1. bin/rails runner 'Labkit::Context.with_context(feature_category: "source_code_management") { puts ApplicationRecord.connection.select_value("SELECT query FROM pg_stat_activity WHERE pid = pg_backend_pid()") }'
  2. The comment on the query now ends with feature_category:source_code_management (after db_config_name)
Supporting evidence
  • Live GDK check: before the change the comment had no feature_category despite the context carrying one; after, it reads /*application:web,correlation_id:...,endpoint_id:...,db_config_database:gitlabhq_development,db_config_name:main,feature_category:source_code_management,line:...*/
  • Ran Gitlab::Database::StatActivitySampler's exact regexp against a comment with the trailing tag in PostgreSQL — it still captures {application, endpoint, database} correctly; a mid-list placement was also tested and does NULL the database capture, confirming why the tag must be appended last
  • Cost is one thread-local hash read per query, same as endpoint_id — negligible; adds roughly 25–60 bytes per query inside PostgreSQL's 1024-byte track_activity_query_size window, same property as every existing tag; pg_stat_statements query IDs are computed post-parse so this adds no fingerprint cardinality
  • Rails omits nil tags, so console/runner queries without a feature category simply won't show the tag, same as endpoint_id today
  • RuboCop clean on all four changed Ruby files; bin/rspec spec/lib/gitlab/query_logs_spec.rb → 11 examples, 0 failures; bin/rspec spec/lib/gitlab/query_logs/tags_spec.rb spec/lib/gitlab/database/stat_activity_sampler_spec.rb → 13 examples, 0 failures; scripts/lint-doc.sh passed on the docs page; changed spec runtime unchanged vs master
  • Updated doc/development/database/database_query_comments.md (both metadata lists, an explanation line, and the three example comments), categories validated against config/feature_categories.yml
  • Possible follow-up for the database health group (not this MR): teach StatActivitySampler to capture feature_category as a sampling dimension

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist.

Merge request reports

Loading
Loading