/// <summary>
        /// Serializes an <see cref="Incident"/> instance based on the specified <see cref="AlertNotificationData"/> instance.
        /// </summary>
        /// <param name="monitorName"><see cref="string"/> name of relevant monitor.</param>
        /// <param name="data">Specified <see cref="AlertNotificationData"/> instance.</param>
        /// <param name="logger">OPTIONAL: <see cref="ILogger"/> handle.</param>
        /// <returns><see cref="string"/> representing serialized <see cref="Incident"/></returns>
        public string GetIncidentDescription(string monitorName, AlertNotificationData data, ILogger?logger = null)
        {
            Incident incident = new Incident
            {
                MonitorName   = monitorName,
                OcurrenceDate = DateTime.UtcNow,
                HowDetected   = Detection.Monitoring,
                Symptoms      = new List <Symptom>
                {
                    new Symptom
                    {
                        Details = new CommonContent
                        {
                            Description = $"{data.Message ?? string.Empty}",
                            Labels      = new string[]
                            {
                                "monitoring failure"
                            }
                        }
                    }
                }
            };

            try
            {
                return(_serializer.GetString(incident));
            }
            catch (Exception e)
            {
                logger?.LogError($"Problem serializing {typeof(Incident)} instance.  Error: {e}");
                return(string.Empty);
            }
        }
Example #2
0
        public void TestNominativeSerialization()
        {
            AlertNotificationData grafanaInfo    = TestUtilities.DeserializeAlertNotificationDataInstance();
            IncidentSerializer    jsonSerializer = new IncidentSerializer();

            Assert.IsNotNull(jsonSerializer);
            TestUtilities.AssertIncidentDescription(jsonSerializer.GetIncidentDescription("test alert", grafanaInfo));
        }
        internal static AlertNotificationData DeserializeAlertNotificationDataInstance()
        {
            string grafanaJson = GetJsonContent("SampleAlertNotificationData.json");
            AlertNotificationData grafanaInfo = JsonConvert.DeserializeObject <AlertNotificationData>(grafanaJson);

            Assert.IsNotNull(grafanaInfo);
            Assert.AreEqual(grafanaInfo.Message, "Some operations returned an unsuccessful http response code");
            return(grafanaInfo);
        }
Example #4
0
        public void TestNominativeJsonSerializerSerialization()
        {
            AlertNotificationData grafanaInfo = TestUtilities.DeserializeAlertNotificationDataInstance();
            Incident       incident           = TestUtilities.CreateIncident(grafanaInfo.Message);
            JsonSerializer jsonSerializer     = new JsonSerializer();

            Assert.IsNotNull(jsonSerializer);
            TestUtilities.AssertIncidentDescription(jsonSerializer.GetString(incident));
        }
Example #5
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            using StreamReader requestStream = new StreamReader(req.Body);
            string requestBody = await requestStream.ReadToEndAsync().ConfigureAwait(false);

            if (string.IsNullOrWhiteSpace(requestBody))
            {
                throw new InvalidOperationException("Request body shouldn't be empty");
            }

            AlertNotificationData data = JsonConvert.DeserializeObject <AlertNotificationData>(requestBody);

            if (data.State != AlertState.Alerting)
            {
                throw new ArgumentOutOfRangeException("Alert state is not \"Alerting\". This notification shouldn't have been sent to this function.");
            }

            VssCredentials             credentials    = new VssBasicCredential("", _devdivAdoPAT);
            WorkItemTrackingHttpClient workItemClient = new WorkItemTrackingHttpClient(_devdivCollectionUri, credentials);
            ProjectHttpClient          projectClient  = new ProjectHttpClient(_devdivCollectionUri, credentials);
            TeamProject devdivProject = await projectClient.GetProject(ProjectName).ConfigureAwait(false);

            if (devdivProject == null)
            {
                throw new ProjectDoesNotExistException($"The project \"{ProjectName}\" was not found or you do not have permission to access it.");
            }

            string          alertMessage         = data.Message ?? "Alert triggered";
            int             alertingMonitorCount = data.MatchingAlerts?.Length ?? 0;
            List <WorkItem> createdWorkItems     = new List <WorkItem>();

            for (int i = 0; i < alertingMonitorCount; i++)
            {
                AlertEvaluation?match = data.MatchingAlerts ![i];