fix: Preload SLI implementation modules on initialization

What does this MR do and why?

Resolves Initialization of SLI metrics is broken in Rails (gitlab-com/gl-infra/observability/team#4127 - closed).

With this change all SLI modules are loaded in memory, independent of the runtime. However, they are only enabled for its specific runtime -- labels will load only for enabled implementations.

The change is mostly to get around an issue with Rails' preloading mechanism. Development, test, and production environments do preloading slightly different among them. There are tests, e.g. https://gitlab.com/gitlab-org/gitlab/-/blob/9648e41315faa491bd2f6f96b6014d5a597b2fcb/spec/lib/gitlab/metrics_spec.rb#L286 and https://gitlab.com/gitlab-org/gitlab/-/blob/9648e41315faa491bd2f6f96b6014d5a597b2fcb/ee/spec/lib/gitlab/metrics_spec.rb#L6, to guarantee the classes are correctly loaded, but I'm afraid we can't verify if they were already loaded or loaded on-demand 🤷🏻. The way I got to verify that is improving the logging and making sure the on-demand behaviour wasn't happening in development. If classes are being registered during boot in development, they will be registered on boot in production as well.

The enabled SLIs can be inspected in the logs:

$ tail -f log/application_json.log | grep "Gitlab::Metrics"
{"severity":"INFO","time":"2025-03-13T16:49:50.939Z","message":"Gitlab::Metrics::SliConfig registering Gitlab::Metrics::RequestsRackMiddleware, runtime=puma"}
{"severity":"INFO","time":"2025-03-13T16:49:53.119Z","message":"Gitlab::Metrics::SliConfig registering Gitlab::Metrics::RequestsRackMiddleware, runtime=sidekiq"}
{"severity":"INFO","time":"2025-03-13T16:49:55.225Z","message":"Gitlab::Metrics: preloading path(s) /Users/hercules/GitLab/gdk/gitlab/lib/gitlab/metrics/*_slis.rb, /Users/hercules/GitLab/gdk/gitlab/ee/lib/gitlab/metrics/*_slis.rb"}
{"severity":"INFO","time":"2025-03-13T16:49:55.227Z","message":"Gitlab::Metrics::SliConfig registering Gitlab::Metrics::GlobalSearchSlis, runtime=puma"}
{"severity":"INFO","time":"2025-03-13T16:49:55.227Z","message":"Gitlab::Metrics::SliConfig registering Gitlab::Metrics::GlqlSlis, runtime=puma"}
{"severity":"INFO","time":"2025-03-13T16:49:55.228Z","message":"Gitlab::Metrics::SliConfig registering Gitlab::Metrics::LooseForeignKeysSlis, runtime=puma"}
{"severity":"INFO","time":"2025-03-13T16:49:55.229Z","message":"Gitlab::Metrics::SliConfig registering Gitlab::Metrics::SecurityScanSlis, runtime=puma"}
{"severity":"INFO","time":"2025-03-13T16:49:55.230Z","message":"Gitlab::Metrics::SliConfig registering Gitlab::Metrics::GlobalSearchIndexingSlis, runtime=puma"}
{"severity":"INFO","time":"2025-03-13T16:49:55.230Z","message":"Gitlab::Metrics::SliConfig: enabling Gitlab::Metrics::RequestsRackMiddleware, runtime=puma"}
{"severity":"INFO","time":"2025-03-13T16:49:55.230Z","message":"Gitlab::Metrics::SliConfig: enabling Gitlab::Metrics::GlobalSearchSlis, runtime=puma"}
{"severity":"INFO","time":"2025-03-13T16:49:55.230Z","message":"Gitlab::Metrics::SliConfig: enabling Gitlab::Metrics::GlqlSlis, runtime=puma"}
{"severity":"INFO","time":"2025-03-13T16:49:55.230Z","message":"Gitlab::Metrics::SliConfig enabled SLIs: Gitlab::Metrics::RequestsRackMiddleware, Gitlab::Metrics::GlobalSearchSlis, Gitlab::Metrics::GlqlSlis, runtime=puma"}
{"severity":"INFO","time":"2025-03-13T16:49:59.170Z","message":"Gitlab::Metrics: preloading path(s) /Users/hercules/GitLab/gdk/gitlab/lib/gitlab/metrics/*_slis.rb, /Users/hercules/GitLab/gdk/gitlab/ee/lib/gitlab/metrics/*_slis.rb"}
{"severity":"INFO","time":"2025-03-13T16:49:59.172Z","message":"Gitlab::Metrics::SliConfig registering Gitlab::Metrics::GlobalSearchSlis, runtime=sidekiq"}
{"severity":"INFO","time":"2025-03-13T16:49:59.172Z","message":"Gitlab::Metrics::SliConfig registering Gitlab::Metrics::GlqlSlis, runtime=sidekiq"}
{"severity":"INFO","time":"2025-03-13T16:49:59.173Z","message":"Gitlab::Metrics::SliConfig registering Gitlab::Metrics::LooseForeignKeysSlis, runtime=sidekiq"}
{"severity":"INFO","time":"2025-03-13T16:49:59.174Z","message":"Gitlab::Metrics::SliConfig registering Gitlab::Metrics::SecurityScanSlis, runtime=sidekiq"}
{"severity":"INFO","time":"2025-03-13T16:49:59.174Z","message":"Gitlab::Metrics::SliConfig registering Gitlab::Metrics::GlobalSearchIndexingSlis, runtime=sidekiq"}
{"severity":"INFO","time":"2025-03-13T16:49:59.174Z","message":"Gitlab::Metrics::SliConfig: enabling Gitlab::Metrics::LooseForeignKeysSlis, runtime=sidekiq"}
{"severity":"INFO","time":"2025-03-13T16:49:59.174Z","message":"Gitlab::Metrics::SliConfig: enabling Gitlab::Metrics::SecurityScanSlis, runtime=sidekiq"}
{"severity":"INFO","time":"2025-03-13T16:49:59.174Z","message":"Gitlab::Metrics::SliConfig: enabling Gitlab::Metrics::GlobalSearchIndexingSlis, runtime=sidekiq"}
{"severity":"INFO","time":"2025-03-13T16:49:59.174Z","message":"Gitlab::Metrics::SliConfig enabled SLIs: Gitlab::Metrics::LooseForeignKeysSlis, Gitlab::Metrics::SecurityScanSlis, Gitlab::Metrics::GlobalSearchIndexingSlis, runtime=sidekiq"}

References

Screenshots or screen recordings

Before After

How to set up and validate locally

  1. Tail the logs to inspect which SLI is being loaded like this:
    $ tail -f log/application_json.log | grep "Gitlab::Metrics"
  2. Then start the gdk and wait to see the log entries :
    $ gdk start

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Hercules Merscher

Merge request reports

Loading