Improve detection of running environment

Currently we detect the running environment by looking at loaded constants, like:

  • defined?(::Puma)
  • defined?(::Unicorn::Worker)
  • Sidekiq.server?
  • defined?(::Rails::Console)

This is flaky, as someone and somehow could load a module and result our detection to be broken.\

We should rather make this very clear how it is executed, by knowing this the most top-level way.

I noticed that problem by debugging tests for !17962 (merged). They were failing as it was assuming that we run on Puma due to Puma being loaded by lib/gitlab/metrics/samplers/puma_sampler.rb, that was doing require 'puma/state_file' which was definitely causing our detection to fail once application was preloaded.

Assignee Loading
Time tracking Loading