public static async Task RegisterAsync(
            IConfigurationRoot configuration,
            string monitoringServiceUrl,
            ILog log)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            if (log == null)
            {
                throw new ArgumentNullException(nameof(log));
            }

            string disableAutoRegistrationStr = configuration[DisableAutoRegistrationEnvVarName];

            if (bool.TryParse(disableAutoRegistrationStr, out bool disableAutoRegistration) && disableAutoRegistration)
            {
                log.WriteMonitor("Auto-registration in monitoring", "", $"Auto-registration is disabled");
                return;
            }

            if (string.IsNullOrWhiteSpace(monitoringServiceUrl))
            {
                throw new ArgumentNullException(nameof(monitoringServiceUrl));
            }

            string podTag = configuration[PodNameEnvVarName] ?? "";

            try
            {
                string myMonitoringUrl = configuration[MyMonitoringUrlEnvVarName];
                if (string.IsNullOrWhiteSpace(myMonitoringUrl))
                {
                    myMonitoringUrl = MissingEnvVarUrl;
                    log.WriteMonitor(
                        "Auto-registration in monitoring",
                        podTag,
                        $"{MyMonitoringUrlEnvVarName} environment variable is not found. Using {myMonitoringUrl} for monitoring registration");
                }
                string myMonitoringName = configuration[MyMonitoringNameEnvVarName];
                if (string.IsNullOrWhiteSpace(myMonitoringName))
                {
                    myMonitoringName = PlatformServices.Default.Application.ApplicationName;
                }
                var monitoringService = new MonitoringServiceFacade(monitoringServiceUrl);

                try
                {
                    var monitoringRegistration = await monitoringService.GetService(myMonitoringName);

                    if (monitoringRegistration?.Url?.ToLower() == myMonitoringUrl.ToLower())
                    {
                        log.WriteMonitor("Auto-registration in monitoring", podTag, $"Service is already registered in monitoring with such url. Skipping.");
                        return;
                    }

                    if (monitoringRegistration.Url != MissingEnvVarUrl)
                    {
                        log.WriteMonitor(
                            "Auto-registration in monitoring",
                            podTag,
                            $"There is a registration for {myMonitoringName} on another url - {monitoringRegistration.Url}. Can't register on {myMonitoringUrl}.");

                        myMonitoringUrl = MissingEnvVarUrl;
                        string instanceTag = string.IsNullOrEmpty(podTag) ? Guid.NewGuid().ToString() : podTag;
                        myMonitoringName = $"{myMonitoringName}-{instanceTag}";
                    }
                }
                catch
                {
                    //Duplicated registration is not found - proceed with usual registration
                }

                await monitoringService.MonitorUrl(
                    new UrlMonitoringObjectModel
                {
                    Url         = myMonitoringUrl,
                    ServiceName = myMonitoringName,
                });

                log.WriteMonitor("Auto-registration in monitoring", podTag, $"Auto-registered in Monitoring with name {myMonitoringName} on {myMonitoringUrl}");
            }
            catch (Exception ex)
            {
                log.WriteMonitor("Auto-registration in monitoring", podTag, ex.ToAsyncString());
            }
        }
 public MonitoringJob(Lykke.MonitoringServiceApiCaller.MonitoringServiceFacade monitoringServiceFacade, ILog logger)
 {
     _monitoringServiceFacade = monitoringServiceFacade;
     _logger  = logger;
     _version = Assembly.GetEntryAssembly().GetName().Version;
 }
        /// <summary>
        /// Registers calling application in monitoring service based on application url from environemnt variable.
        /// </summary>
        /// <param name="configuration">Application configuration that is used for environemnt variable search.</param>
        /// <param name="monitoringServiceUrl">Monitoring service url.</param>
        /// <param name="healthNotifier">Health notifier</param>
        /// <returns></returns>
        public static async Task RegisterInMonitoringServiceAsync(
            [NotNull] this IConfigurationRoot configuration,
            [NotNull] string monitoringServiceUrl,
            [NotNull] IHealthNotifier healthNotifier)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var disableAutoRegistrationStr = configuration[DisableAutoRegistrationEnvVarName];

            if (bool.TryParse(disableAutoRegistrationStr, out bool disableAutoRegistration) && disableAutoRegistration)
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(monitoringServiceUrl))
            {
                throw new ArgumentException("Argument is empty", nameof(monitoringServiceUrl));
            }

            if (healthNotifier == null)
            {
                throw new ArgumentNullException(nameof(healthNotifier));
            }

            var podTag = configuration[PodNameEnvVarName] ?? string.Empty;

            try
            {
                var myMonitoringUrl = configuration[MyMonitoringUrlEnvVarName];
                if (string.IsNullOrWhiteSpace(myMonitoringUrl))
                {
                    myMonitoringUrl = MissingEnvVarUrl;
                    healthNotifier.Notify(
                        $"{MyMonitoringUrlEnvVarName} environment variable is not found. Using {myMonitoringUrl} for monitoring registration",
                        podTag);
                }

                var myMonitoringName = configuration[MyMonitoringNameEnvVarName];
                if (string.IsNullOrWhiteSpace(myMonitoringName))
                {
                    myMonitoringName = PlatformServices.Default.Application.ApplicationName;
                }

                var monitoringService = new MonitoringServiceFacade(monitoringServiceUrl);

                try
                {
                    var monitoringRegistration = await monitoringService.GetService(myMonitoringName);

                    if (monitoringRegistration.Url == myMonitoringUrl)
                    {
                        return;
                    }

                    healthNotifier.Notify($"There is a registration for {myMonitoringName} in monitoring service!", podTag);

                    myMonitoringUrl = MissingEnvVarUrl;
                    var instanceTag = string.IsNullOrEmpty(podTag) ? Guid.NewGuid().ToString() : podTag;
                    myMonitoringName = $"{myMonitoringName}-{instanceTag}";
                }
                catch
                {
                    //Duplicated registration is not found - proceed with usual registration
                }

                await monitoringService.MonitorUrl(
                    new UrlMonitoringObjectModel
                {
                    Url         = myMonitoringUrl,
                    ServiceName = myMonitoringName,
                });

                healthNotifier.Notify($"Auto-registered in Monitoring with name {myMonitoringName} on {myMonitoringUrl}", podTag);
            }
            catch (Exception ex)
            {
                healthNotifier.Notify(ex.ToAsyncString(), podTag);
            }
        }