public MetricsGroup(MetricsLogger metricsLogger, params string[] dimensionGroups)
 {
     _metricsLogger   = metricsLogger;
     _dimensionGroups = dimensionGroups.Select(x =>
                                               Dimensions.ExtendAndSerializeDimensions(metricsLogger.GetRootDimensions(), x))
                        .Where(x => !string.IsNullOrEmpty(x))
                        .ToList();
 }
        private static MetricsLogger GetOrAddRootMetricsLogger(string name)
        {
            if (MetricsLoggers.ContainsKey(name))
            {
                return(MetricsLoggers[name]);
            }

            var logger = new MetricsLogger(name);

            MetricsLoggers.Add(name, logger);

            return(logger);
        }
        public static void Shutdown()
        {
            _flushSchedulerHandler?.Dispose();

            _instance          = null;
            _httpClient        = null;
            _rootMetricsLogger = null;

            MetricsLoggers.Clear();

            while (MetricsQueue.TryDequeue(out _))
            {
            }
        }
        public static void Initialize(string serviceName, string env, string hostInfo)
        {
            if (!Dimensions.IsValidName(serviceName))
            {
                throw new ArgumentException(nameof(serviceName));
            }

            if (_instance != null)
            {
                return;
            }

            lock (Locker)
            {
                if (_instance != null)
                {
                    return;
                }

                _instance = new MetricsManager(serviceName, env, hostInfo);

                _environment = env;
                _hostInfo    = hostInfo;
                _serviceName = serviceName;

                var envDimension = GetEnvironmentDimension(env);

                LoadOptionsFromConfigFile();

                _rootMetricsLogger = GetOrAddRootMetricsLogger($"service={serviceName}{envDimension}");
                _httpClient        = ConfigureHttpClient();

                if (!Options.IsManualFlush)
                {
                    try
                    {
                        _flushSchedulerHandler = InitializeFlushScheduler();
                    }
                    catch (Exception e)
                    {
                        Logger.LogError(e.ToString());
                    }
                }

                Logger.LogInformation($"{nameof(MetricsManager)} successfuly initialized at {DateTime.UtcNow} UTC.");
            }
        }
        internal static void FlushMetricsLogger(MetricsLogger logger)
        {
            logger.FlushToString((str) => { MetricsQueue.Enqueue(str); });

            _rootMetricsLogger?.FlushToString((str) => { MetricsQueue.Enqueue(str); });
        }