Beispiel #1
0
        /// <summary>
        /// Initiates an asynchronous operation for running the Smart Detector analysis on the specified resources.
        /// </summary>
        /// <param name="analysisRequest">The analysis request data.</param>
        /// <param name="tracer">
        /// A tracer used for emitting telemetry from the Smart Detector's execution. This telemetry will be used for troubleshooting and
        /// monitoring the Smart Detector's executions.
        /// </param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for a task to complete.</param>
        /// <returns>
        /// A <see cref="Task"/> that represents the asynchronous operation, returning the Alerts detected for the target resources.
        /// </returns>
        public async Task <List <Alert> > AnalyzeResourcesAsync(AnalysisRequest analysisRequest, ITracer tracer, CancellationToken cancellationToken)
        {
            tracer.TraceInformation("Analyzing the specified resources...");
            $if$("$dataType$" == "Log Analytics")
            // Get the Log Analytics client
            ITelemetryDataClient dataClient = await analysisRequest.AnalysisServicesFactory.CreateLogAnalyticsTelemetryDataClientAsync(new List <ResourceIdentifier>() { analysisRequest.TargetResources.First() }, cancellationToken);

            $else$
            // Get the Application Insights client
            ITelemetryDataClient dataClient = await analysisRequest.AnalysisServicesFactory.CreateApplicationInsightsTelemetryDataClientAsync(new List <ResourceIdentifier>() { analysisRequest.TargetResources.First() }, cancellationToken);

            $endif$
            // Run the query
            IList <DataTable> dataTables = await dataClient.RunQueryAsync(@"$tableName$ | count", cancellationToken);

            // Process the query results and create alerts
            List <Alert> alerts = new List <Alert>();

            if (dataTables[0].Rows.Count > 0)
            {
                alerts.Add(new $alertName$("Title", analysisRequest.TargetResources.First(), Convert.ToInt32(dataTables[0].Rows[0]["Count"])));
            }

            tracer.TraceInformation($"Created {alerts.Count()} alerts");
            return(alerts);
        }
Beispiel #2
0
        /// <summary>
        /// The method runs the sample analysis calls
        /// </summary>
        /// <param name="analysisRequest">The analysis request object</param>
        /// <param name="tracer">used to save trace messages</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns>The smart signal result containing the result items</returns>
        public async Task <SmartSignalResult> AnalyzeResourcesAsync(AnalysisRequest analysisRequest, ITracer tracer, CancellationToken cancellationToken)
        {
            SmartSignalResult smartSignalResult = new SmartSignalResult();

            // 1. Fetch from Application Insights client - name of an application with maximum request in the last 24h
            ITelemetryDataClient applicationInsightsDataClient = await analysisRequest.AnalysisServicesFactory.CreateApplicationInsightsTelemetryDataClientAsync(
                analysisRequest.TargetResources, cancellationToken);

            IList <DataTable> applicationInsightsResult = await applicationInsightsDataClient.RunQueryAsync(HighestAppApplicationInsightsQuery, cancellationToken);

            long   countReqByAppName = 0;
            string appName           = "N/A";

            if (applicationInsightsResult.Count > 0)
            {
                countReqByAppName = Convert.ToInt64(applicationInsightsResult[0].Rows[0]["countReqByAppName"]);
                appName           = Convert.ToString(applicationInsightsResult[0].Rows[0]["appName"]);
                tracer.TraceInformation($"App {appName} has high request count of {countReqByAppName}");
            }
            else
            {
                tracer.TraceError("Failed to perform the query in Application Insights");
            }

            // 2. Fetch from Log Analytics - time of highest CPU in the last 24h
            ITelemetryDataClient logAnalyticsDataClient = await analysisRequest.AnalysisServicesFactory.CreateLogAnalyticsTelemetryDataClientAsync(
                analysisRequest.TargetResources, cancellationToken);

            IList <DataTable> logAnalyticsResult = await logAnalyticsDataClient.RunQueryAsync(MaximumProcessorTimeLogAnalyticsQuery, cancellationToken);

            double   highestProcessorTimePercent = 0;
            DateTime timeOfHighestProcessorTime  = new DateTime();

            if (logAnalyticsResult.Count > 0)
            {
                highestProcessorTimePercent = Convert.ToDouble(logAnalyticsResult[0].Rows[0]["CounterValue"]);
                timeOfHighestProcessorTime  = Convert.ToDateTime(logAnalyticsResult[0].Rows[0]["TimeGenerated"]);
                tracer.TraceInformation($"The highest value of % Processor Time {highestProcessorTimePercent} appeared at {timeOfHighestProcessorTime}");
            }
            else
            {
                tracer.TraceError("Failed to perform the query in Log Analytics");
            }

            analysisRequest.TargetResources.ForEach(resourceIdentifier => smartSignalResult.ResultItems.Add(
                                                        new HighRequestCountSignalResultItem("High Processing Time Percentage(LA) and Request Count(AI)", appName, countReqByAppName, highestProcessorTimePercent, timeOfHighestProcessorTime, resourceIdentifier)));
            return(smartSignalResult);
        }
Beispiel #3
0
        /// <summary>
        /// Initiates an asynchronous operation for running the Smart Detector analysis on the specified resources.
        /// </summary>
        /// <param name="analysisRequest">The analysis request data.</param>
        /// <param name="tracer">
        /// A tracer used for emitting telemetry from the Smart Detector's execution. This telemetry will be used for troubleshooting and
        /// monitoring the Smart Detector's executions.
        /// </param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for a task to complete.</param>
        /// <returns>
        /// A <see cref="Task"/> that represents the asynchronous operation, returning the Alerts detected for the target resources.
        /// </returns>
        public async Task <List <Alert> > AnalyzeResourcesAsync(AnalysisRequest analysisRequest, ITracer tracer, CancellationToken cancellationToken)
        {
            tracer.TraceInformation("Analyzing the specified resources...");
            $if$("$dataType$" == "Log Analytics")
            // Get the Log Analytics client
            ITelemetryDataClient dataClient = await analysisRequest.AnalysisServicesFactory.CreateLogAnalyticsTelemetryDataClientAsync(new List <ResourceIdentifier>() { analysisRequest.RequestParameters.TargetResources.First() }, cancellationToken);

            $else$
            // Get the Application Insights client
            ITelemetryDataClient dataClient = await analysisRequest.AnalysisServicesFactory.CreateApplicationInsightsTelemetryDataClientAsync(new List <ResourceIdentifier>() { analysisRequest.RequestParameters.TargetResources.First() }, cancellationToken);

            $endif$
            // Run the query
            IList <DataTable> dataTables = await dataClient.RunQueryAsync(@"$tableName$ | count", cancellationToken);

            // Process the query results and create alerts
            List <Alert> alerts = new List <Alert>();

            if (dataTables[0].Rows.Count > 0)
            {
                // Query the count over time chart
                IList <DataTable> countOverTimeDataTables = await dataClient.RunQueryAsync("$query$", cancellationToken);

                // And create the alert
                var alert = new $alertName$("Title", analysisRequest.RequestParameters.TargetResources.First(), Convert.ToInt32(dataTables[0].Rows[0]["Count"]))
                {
                    CountChart = countOverTimeDataTables[0].Rows.Cast <DataRow>().Select(row => new ChartPoint(row["timestamp"], row["Count"])).ToList()
                };

                alerts.Add(alert);
            }

            tracer.TraceInformation($"Created {alerts.Count()} alerts");
            return(alerts);
        }
    }