Investigation into Ruby Linter Rules for Fields
Objective
Investigate and implement a dynamic linting approach for enforcing field logging standards in the GitLab codebase, using runtime warnings and a custom TODO list instead of static RuboCop rules.
Background
As part of defining field logging standards for developers, we need a mechanism to identify and track unknown/non-standard logging fields across the codebase. Initial investigation ruled out RuboCop (static linting) due to the dynamic nature of logging field construction (see example).
Approach
Dynamic linting via runtime inspection:
- Hook into
json-logger'sformat_messagemethod to inspect logging field keys at runtime - Compare emitted fields against an allow list (standard fields + TODO list)
- Emit warnings for unknown fields during development and CI
Configuration-based management:
- Maintain a YAML configuration file (
field_standard_config.yml) with three categories:-
allow_list: Approved standard fields (e.g.,correlation_id,gl_user_id,gl_user_name) -
ignore: System fields to ignore (e.g.,time,severity,message) -
todo: Fields currently in use but not yet standardized (temporary allowlist)
-
Implementation Details
Reference MR: gitlab-org/gitlab!214212
The implementation:
-
Prepends a
Checkermodule toLabkit::Logging::JsonLogger -
Intercepts
format_messageto parse JSON output and extract field keys - Compares fields against the combined allow list
-
Warns once per unique set of unknown fields (deduplication via
Set) - Only runs in non-production environments
Example warning output:
Logger Gitlab::Database::LoadBalancing::Logger emitted unknown fields: ["event", "model", "start_service_discovery"]
Logger Gitlab::AuthLogger emitted unknown fields: ["gitlab_throttle_user_allowlist"]
Next Steps
- Compile comprehensive TODO list: Run CI pipelines and parse warning output to identify all existing non-standard fields
-
Categorize fields: Review discovered fields and determine which should be:
- Standardized and moved to
allow_list - Deprecated and removed from code
- Temporarily kept in
todolist
- Standardized and moved to
- Create remediation plan: For each TODO field, create issues/MRs to either standardize or remove
- Monitor and iterate: Track progress on reducing TODO list over time
Related Resources
- Parent Epic: https://gitlab.com/groups/gitlab-org/developer-experience/-/epics/235
- Implementation MR: gitlab-org/gitlab!214212
- Labkit JSON Logger: https://gitlab.com/gitlab-org/ruby/gems/labkit-ruby/-/blob/master/lib/labkit/logging/json_logger.rb
- Labkit Fields: https://gitlab.com/gitlab-org/ruby/gems/labkit-ruby/-/blob/master/lib/labkit/fields.rb
Edited by Mohga Gamea