Beispiel #1
0
        public async Task GetAlertsWithTokenCredential()
        {
            MetricsAdvisorClient client = GetMetricsAdvisorClient(useTokenCredential: true);

            var options = new GetAlertsOptions(SamplingStartTime, SamplingEndTime, AlertQueryTimeMode.AnomalyTime);

            var alertCount = 0;

            await foreach (AnomalyAlert alert in client.GetAlertsAsync(AlertConfigurationId, options))
            {
                Assert.That(alert, Is.Not.Null);
                Assert.That(alert.Id, Is.Not.Null.And.Not.Empty);
                Assert.That(alert.Timestamp, Is.Not.EqualTo(default(DateTimeOffset)));
                Assert.That(alert.CreatedTime, Is.Not.EqualTo(default(DateTimeOffset)));
                Assert.That(alert.ModifiedTime, Is.Not.EqualTo(default(DateTimeOffset)));
                Assert.That(alert.Timestamp, Is.InRange(SamplingStartTime, SamplingEndTime));

                if (++alertCount >= MaximumSamplesCount)
                {
                    break;
                }
            }

            Assert.That(alertCount, Is.GreaterThan(0));
        }
Beispiel #2
0
        public async Task QueryDetectedAnomaliesAndTriggeredAlerts()
        {
            string endpoint        = MetricsAdvisorUri;
            string subscriptionKey = MetricsAdvisorSubscriptionKey;
            string apiKey          = MetricsAdvisorApiKey;
            var    credential      = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);

            var client = new MetricsAdvisorClient(new Uri(endpoint), credential);

            string anomalyAlertConfigurationId = AlertConfigurationId;

            #region Snippet:QueryDetectedAnomaliesAndTriggeredAlerts
            //@@ string anomalyAlertConfigurationId = "<anomalyAlertConfigurationId>";

            var startTime = DateTimeOffset.Parse("2020-01-01T00:00:00Z");
            var endTime   = DateTimeOffset.UtcNow;
            var options   = new GetAlertsOptions(startTime, endTime, TimeMode.AnomalyTime);

            int alertCount = 0;

            await foreach (AnomalyAlert alert in client.GetAlertsAsync(anomalyAlertConfigurationId, options))
            {
                Console.WriteLine($"Alert at timestamp: {alert.Timestamp}");
                Console.WriteLine($"Id: {alert.Id}");
                Console.WriteLine($"Anomalies that triggered this alert:");

                await foreach (DataAnomaly anomaly in client.GetAnomaliesForAlertAsync(anomalyAlertConfigurationId, alert.Id))
                {
                    Console.WriteLine("  Anomaly:");
                    Console.WriteLine($"    Status: {anomaly.Status.Value}");
                    Console.WriteLine($"    Severity: {anomaly.Severity}");
                    Console.WriteLine($"    Series key:");

                    foreach (KeyValuePair <string, string> keyValuePair in anomaly.SeriesKey.AsDictionary())
                    {
                        Console.WriteLine($"      Dimension '{keyValuePair.Key}': {keyValuePair.Value}");
                    }

                    Console.WriteLine();
                }

                // Print at most 3 alerts.
                if (++alertCount >= 3)
                {
                    break;
                }
            }
            #endregion
        }
Beispiel #3
0
        public async Task GetAlerts(string timeModeName)
        {
            MetricsAdvisorClient client = GetMetricsAdvisorClient();

            AlertQueryTimeMode timeMode = timeModeName switch
            {
                "anomalyTime" => AlertQueryTimeMode.AnomalyTime,
                "createdTime" => AlertQueryTimeMode.CreatedTime,
                "modifiedTime" => AlertQueryTimeMode.ModifiedTime,
                _ => throw new ArgumentOutOfRangeException("Invalid test case.")
            };

            var options = new GetAlertsOptions(SamplingStartTime, SamplingEndTime, timeMode);

            var alertCount = 0;

            await foreach (AnomalyAlert alert in client.GetAlertsAsync(AlertConfigurationId, options))
            {
                Assert.That(alert, Is.Not.Null);
                Assert.That(alert.Id, Is.Not.Null.And.Not.Empty);
                Assert.That(alert.Timestamp, Is.Not.EqualTo(default(DateTimeOffset)));
                Assert.That(alert.CreatedTime, Is.Not.EqualTo(default(DateTimeOffset)));
                Assert.That(alert.ModifiedTime, Is.Not.EqualTo(default(DateTimeOffset)));

                DateTimeOffset filteredTime = timeModeName switch
                {
                    "anomalyTime" => alert.Timestamp,
                    "createdTime" => alert.CreatedTime,
                    "modifiedTime" => alert.ModifiedTime,
                    _ => throw new ArgumentOutOfRangeException("Invalid test case.")
                };

                Assert.That(filteredTime, Is.InRange(SamplingStartTime, SamplingEndTime));

                if (++alertCount >= MaximumSamplesCount)
                {
                    break;
                }
            }

            Assert.That(alertCount, Is.GreaterThan(0));
        }
Beispiel #4
0
        public async Task GetAlertsAsync()
        {
            string endpoint        = MetricsAdvisorUri;
            string subscriptionKey = MetricsAdvisorSubscriptionKey;
            string apiKey          = MetricsAdvisorApiKey;
            var    credential      = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);

            var client = new MetricsAdvisorClient(new Uri(endpoint), credential);

            #region Snippet:GetAlertsAsync
#if SNIPPET
            string anomalyAlertConfigurationId = "<anomalyAlertConfigurationId>";
#else
            string anomalyAlertConfigurationId = AlertConfigurationId;
#endif

            var startTime = DateTimeOffset.Parse("2020-01-01T00:00:00Z");
            var endTime   = DateTimeOffset.UtcNow;
            var options   = new GetAlertsOptions(startTime, endTime, AlertQueryTimeMode.AnomalyTime)
            {
                TopCount = 5
            };

            int alertCount = 0;

            await foreach (AnomalyAlert alert in client.GetAlertsAsync(anomalyAlertConfigurationId, options))
            {
                Console.WriteLine($"Alert created at: {alert.CreatedTime}");
                Console.WriteLine($"Alert at timestamp: {alert.Timestamp}");
                Console.WriteLine($"Id: {alert.Id}");
                Console.WriteLine();

                // Print at most 5 alerts.
                if (++alertCount >= 5)
                {
                    break;
                }
            }
            #endregion
        }