Ejemplo n.º 1
0
        /// <summary>
        /// Creates or updates alert resource.
        /// </summary>
        /// <param name="name">Name of the web test.</param>
        /// <param name="webTest"><see cref="WebTestElement"/> instance from configuration.</param>
        /// <param name="client"><see cref="IInsightsManagementClient"/> instance.</param>
        /// <param name="webTestResource"><see cref="ResourceBaseExtended"/> instance as a Web Test resource.</param>
        /// <param name="insightsResource"><see cref="ResourceBaseExtended"/> instance as an Application Insights resource.</param>
        /// <returns>Returns <c>True</c>, if the web test resource creted/updated successfully; otherwise returns <c>False</c>.</returns>
        public async Task <bool> CreateOrUpdateAlertsAsync(string name, WebTestElement webTest, IInsightsManagementClient client, ResourceBaseExtended webTestResource, ResourceBaseExtended insightsResource)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

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

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

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

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

#if !TEST
            Console.WriteLine($"Processing alert for {name} started ...");
#endif
            var parameters = (new RuleCreateOrUpdateParameters()
            {
                Location = InsightsAlertResourceLocation
            })
                             .AddTags(webTestResource, insightsResource)
                             .AddProperties(name, webTest, webTestResource, insightsResource);

            var result = await client.AlertOperations.CreateOrUpdateRuleAsync(this._appInsights.ResourceGroup, parameters).ConfigureAwait(false);

            if (!IsAcceptableHttpStatusCode(result.StatusCode, new[] { HttpStatusCode.OK, HttpStatusCode.Created }))
            {
                throw new HttpResponseException(result.StatusCode);
            }

#if !TEST
            Console.WriteLine($"Processing alert for {name} completed");
#endif
            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds properties for web test alert.
        /// </summary>
        /// <param name="parameters"><see cref="RuleCreateOrUpdateParameters"/> instance.</param>
        /// <param name="name">Name of web test.</param>
        /// <param name="element"><see cref="WebTestElement"/> instance from App.config/Web.config.</param>
        /// <param name="webTest"><see cref="ResourceBaseExtended"/> instance representing web test resource.</param>
        /// <param name="insights"><see cref="ResourceBaseExtended"/> instance representing Application Insights resource.</param>
        /// <returns>Returns the <see cref="RuleCreateOrUpdateParameters"/> instance with properties added.</returns>
        public static RuleCreateOrUpdateParameters AddProperties(this RuleCreateOrUpdateParameters parameters, string name, WebTestElement element, ResourceBaseExtended webTest, ResourceBaseExtended insights)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

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

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

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

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

            var alertName = $"{name}-{insights.Name}-alert".ToLowerInvariant();

            var action = new RuleEmailAction()
            {
                SendToServiceOwners = element.Alerts.SendAlertToAdmin
            };

            if (!element.Alerts.Recipients.IsNullOrEmpty())
            {
                action.CustomEmails = element.Alerts.Recipients;
            }

            var source = new RuleMetricDataSource()
            {
                MetricName = AlertMetricName, ResourceUri = webTest.Id
            };
            var condition = new LocationThresholdRuleCondition()
            {
                DataSource          = source,
                FailedLocationCount = element.Alerts.AlertLocationThreshold,
                WindowSize          = TimeSpan.FromMinutes((int)element.Alerts.TestAlertFailureTimeWindow),
            };
            var rule = new Rule()
            {
                Name            = alertName,
                Description     = string.Empty,
                IsEnabled       = element.Alerts.IsEnabled,
                LastUpdatedTime = DateTime.UtcNow,
                Actions         = { action },
                Condition       = condition,
            };

            parameters.Properties = rule;

            return(parameters);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates or updates web test resource.
        /// </summary>
        /// <param name="name">Name of the web test.</param>
        /// <param name="url">URL of the web test.</param>
        /// <param name="authType"><see cref="AuthType"/> value.</param>
        /// <param name="accessToken">Access token value.</param>
        /// <param name="webTest"><see cref="WebTestElement"/> instance from configuration.</param>
        /// <param name="client"><see cref="IResourceManagementClient"/> instance.</param>
        /// <param name="insightsResource"><see cref="ResourceBaseExtended"/> instance as an Application Insights resource.</param>
        /// <returns>Returns <c>True</c>, if the web test resource creted/updated successfully; otherwise returns <c>False</c>.</returns>
        public async Task <GenericResourceExtended> CreateOrUpdateWebTestAsync(string name, string url, AuthType authType, string accessToken, WebTestElement webTest, IResourceManagementClient client, ResourceBaseExtended insightsResource)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

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

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

            // TODO: for now it only supports PING test.
            if (webTest.TestType != TestType.UrlPingTest)
            {
                throw new InvalidOperationException("Invalid web test type");
            }

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

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

#if !TEST
            Console.WriteLine($"Processing web test for {name} started ...");
#endif
            var resource = new WebTestResource(name, url, insightsResource, webTest.TestType);
            resource.CreateWebTestProperties(webTest.TestLocations, webTest.Status, webTest.Frequency, webTest.SuccessCriteria.Timeout, webTest.ParseDependentRequests, webTest.RetriesForWebTestFailure, authType: authType, accessToken: accessToken);

            var result = await client.Resources.CreateOrUpdateAsync(this._appInsights.ResourceGroup, resource.ResourceIdentity, resource).ConfigureAwait(false);

            if (!IsAcceptableHttpStatusCode(result.StatusCode, new[] { HttpStatusCode.OK, HttpStatusCode.Created }))
            {
                throw new HttpResponseException(result.StatusCode);
            }

#if !TEST
            Console.WriteLine($"Processing web test for {name} completed");
#endif
            return(result.Resource);
        }