public async Task GetAnomaliesForAlert(bool useTokenCredential)
        {
            MetricsAdvisorClient client = GetMetricsAdvisorClient(useTokenCredential);

            var anomalyCount = 0;

            await foreach (DataPointAnomaly anomaly in client.GetAnomaliesForAlertAsync(AlertConfigurationId, AlertId))
            {
                Assert.That(anomaly, Is.Not.Null);
                Assert.That(anomaly.DataFeedId, Is.Not.Null.And.Not.Empty);
                Assert.That(anomaly.MetricId, Is.Not.Null.And.Not.Empty);
                Assert.That(anomaly.DetectionConfigurationId, Is.Not.Null.And.Not.Empty);
                Assert.That(anomaly.Timestamp, Is.Not.EqualTo(default(DateTimeOffset)));
                Assert.That(anomaly.CreatedOn, Is.Not.EqualTo(default(DateTimeOffset)));
                Assert.That(anomaly.LastModified, Is.Not.EqualTo(default(DateTimeOffset)));
                Assert.That(anomaly.Status, Is.Not.EqualTo(default(AnomalyStatus)));
                Assert.That(anomaly.Severity, Is.Not.EqualTo(default(AnomalySeverity)));

                ValidateSeriesKey(anomaly.SeriesKey);

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

            Assert.That(anomalyCount, Is.GreaterThan(0));
        }
Beispiel #2
0
        public async Task GetAnomaliesForAlertAsync()
        {
            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:GetAnomaliesForAlertAsync
#if SNIPPET
            string alertConfigurationId = "<alertConfigurationId>";
            string alertId = "<alertId>";
#else
            string alertConfigurationId = AlertConfigurationId;
            string alertId = AlertId;
#endif

            var options = new GetAnomaliesForAlertOptions()
            {
                MaxPageSize = 3
            };

            int anomalyCount = 0;

            await foreach (DataPointAnomaly anomaly in client.GetAnomaliesForAlertAsync(alertConfigurationId, alertId, options))
            {
                Console.WriteLine($"Anomaly detection configuration ID: {anomaly.DetectionConfigurationId}");
                Console.WriteLine($"Data feed ID: {anomaly.DataFeedId}");
                Console.WriteLine($"Metric ID: {anomaly.MetricId}");
                Console.WriteLine($"Anomaly value: {anomaly.Value}");

                if (anomaly.ExpectedValue.HasValue)
                {
                    Console.WriteLine($"Anomaly expected value: {anomaly.ExpectedValue}");
                }

                Console.WriteLine($"Anomaly at timestamp: {anomaly.Timestamp}");
                Console.WriteLine($"Anomaly detected at: {anomaly.CreatedTime}");
                Console.WriteLine($"Status: {anomaly.Status}");
                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 anomalies.
                if (++anomalyCount >= 3)
                {
                    break;
                }
            }
            #endregion
        }
Beispiel #3
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
        }
        public async Task GetAnomaliesForAlertSetsNullExpectedValue()
        {
            using Stream responseBody = CreateAnomalyJsonStream(expectedValue: null);
            MockResponse mockResponse = new MockResponse(200)
            {
                ContentStream = responseBody
            };

            MetricsAdvisorClient client = CreateInstrumentedClient(mockResponse);

            await foreach (DataPointAnomaly anomaly in client.GetAnomaliesForAlertAsync(FakeGuid, "alertId"))
            {
                Assert.That(anomaly.ExpectedValue, Is.Null);
            }
        }
        public async Task GetAnomaliesForAlertSetsValue()
        {
            double originalValue = 3.14;

            using Stream responseBody = CreateAnomalyJsonStream(value: originalValue);
            MockResponse mockResponse = new MockResponse(200)
            {
                ContentStream = responseBody
            };

            MetricsAdvisorClient client = CreateInstrumentedClient(mockResponse);

            await foreach (DataPointAnomaly anomaly in client.GetAnomaliesForAlertAsync(FakeGuid, "alertId"))
            {
                Assert.That(anomaly.Value, Is.EqualTo(originalValue));
            }
        }