Пример #1
0
        public async Task CreateHookForReceivingAnomalyAlerts()
        {
            string endpoint        = MetricsAdvisorUri;
            string subscriptionKey = MetricsAdvisorSubscriptionKey;
            string apiKey          = MetricsAdvisorApiKey;
            var    credential      = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);

            var adminClient = new MetricsAdvisorAdministrationClient(new Uri(endpoint), credential);

            #region Snippet:CreateHookForReceivingAnomalyAlerts
            string hookName      = "Sample hook";
            var    emailsToAlert = new List <string>()
            {
                "*****@*****.**",
                "*****@*****.**"
            };

            var emailHook = new EmailHook(hookName, emailsToAlert);

            Response <AlertingHook> response = adminClient.CreateHook(emailHook);

            AlertingHook hook = response.Value;

            Console.WriteLine($"Hook ID: {hook.Id}");
            #endregion

            // Delete the created hook to clean up the Metrics Advisor resource. Do not perform this
            // step if you intend to keep using the hook.

            await adminClient.DeleteHookAsync(hook.Id);
        }
        public async Task HookOperations()
        {
            var adminClient = GetMetricsAdvisorAdministrationClient();

            AlertingHook createdEmailHook = await adminClient.CreateHookAsync(new EmailHook(Recording.GenerateAlphaNumericId("test"), new List <string> {
                "*****@*****.**"
            }) { Description = $"{nameof(EmailHook)} description" }).ConfigureAwait(false);

            EmailHook getEmailHook = (await adminClient.GetHookAsync(createdEmailHook.Id).ConfigureAwait(false)).Value as EmailHook;

            getEmailHook.Description = "updated description";
            getEmailHook.EmailsToAlert.Add($"{Recording.GenerateAlphaNumericId("user")}@contoso.com");

            await adminClient.UpdateHookAsync(getEmailHook.Id, getEmailHook).ConfigureAwait(false);

            AlertingHook createdWebHook = await adminClient.CreateHookAsync(new WebHook(Recording.GenerateAlphaNumericId("test"), "http://contoso.com") { Description = $"{nameof(WebHook)} description" }).ConfigureAwait(false);

            createdWebHook.Description = "updated description";

            await adminClient.UpdateHookAsync(createdEmailHook.Id, createdEmailHook).ConfigureAwait(false);

            WebHook getWebHook = (await adminClient.GetHookAsync(createdWebHook.Id).ConfigureAwait(false)).Value as WebHook;

            getWebHook.Description    = "updated description";
            getWebHook.CertificateKey = Recording.GenerateAlphaNumericId("key");

            List <AlertingHook> hooks = await adminClient.GetHooksAsync(new GetHooksOptions { HookNameFilter = getWebHook.Name }).ToEnumerableAsync().ConfigureAwait(false);

            Assert.That(getEmailHook.Id, Is.EqualTo(createdEmailHook.Id));
            Assert.That(getEmailHook.Name, Is.EqualTo(createdEmailHook.Name));
            Assert.That(hooks, Is.Not.Empty);
            Assert.That(hooks.Any(h => h.Name == getWebHook.Name), $"hooks should contain name {createdEmailHook.Name}, but contained names: {string.Join(",", hooks.Select(h => h.Name))}");

            await adminClient.DeleteHookAsync(createdEmailHook.Id).ConfigureAwait(false);
        }
Пример #3
0
        public async Task GetHookAsync()
        {
            string endpoint        = MetricsAdvisorUri;
            string subscriptionKey = MetricsAdvisorSubscriptionKey;
            string apiKey          = MetricsAdvisorApiKey;
            var    credential      = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);

            var adminClient = new MetricsAdvisorAdministrationClient(new Uri(endpoint), credential);

            string hookId = HookId;

            Response <AlertingHook> response = await adminClient.GetHookAsync(hookId);

            AlertingHook hook = response.Value;

            Console.WriteLine($"Hook name: {hook.Name}");
            Console.WriteLine($"Hook description: {hook.Description}");

            Console.WriteLine($"Hook administrators:");
            foreach (string admin in hook.Administrators)
            {
                Console.WriteLine($" - {admin}");
            }

            if (hook.HookType == HookType.Email)
            {
                EmailHook emailHook = hook as EmailHook;

                Console.WriteLine("Emails to alert:");
                foreach (string email in emailHook.EmailsToAlert)
                {
                    Console.WriteLine($" - {email}");
                }
            }
            else if (hook.HookType == HookType.Webhook)
            {
                WebHook webHook = hook as WebHook;

                Console.WriteLine($"Username: {webHook.Username}");
            }
        }