internal DotNetRuntimeStatsCollector(ImmutableHashSet <IEventSourceStatsCollector> statsCollectors, Action <Exception> errorHandler, bool enabledDebugging, TCollectorRegistry registry)
        {
            _statsCollectors  = statsCollectors;
            _enabledDebugging = enabledDebugging;
            _errorHandler     = errorHandler ?? (e => { });
            _registry         = registry;
            lock (_lockInstance)
            {
                if (Instances.ContainsKey(registry))
                {
                    throw new InvalidOperationException(".NET runtime metrics are already being collected. Dispose() of your previous collector before calling this method again.");
                }

                Instances.Add(registry, this);
            }
        }
        public void RegisterMetrics(TCollectorRegistry registry)
        {
#if PROMV2
            var metrics = new MetricFactory(registry);
#elif PROMV3
            var metrics = Metrics.WithCustomRegistry(registry);
#endif

            foreach (var sc in _statsCollectors)
            {
                sc.RegisterMetrics(metrics);
            }

            // Metrics have been registered, start the event listeners
            _eventListeners = _statsCollectors
                              .Select(sc => new DotNetEventListener(sc, _errorHandler, _enabledDebugging))
                              .ToArray();

            SetupConstantMetrics(metrics);
        }