Exemplo n.º 1
0
        public async Task <OpsGenieResult> CreateAsync(OpsgenieAlert alert)
        {
            if (alert == null)
            {
                throw new ArgumentNullException(nameof(alert));
            }

            var content = new StringContent(
                JsonSerializer.Serialize(alert, SerializerOptions),
                _utf8Encoding,
                "application/json");

            var response = await _httpClient.PostAsync(OpsgenieCreateAlertUrl, content);

            var responseBody = await response.Content.ReadAsStringAsync();

            var result = new OpsGenieResult
            {
                StatusCode   = (int)response.StatusCode,
                Ok           = response.IsSuccessStatusCode,
                HttpResponse = response,
                ResponseBody = responseBody
            };

            try
            {
                var opsGenieResponse =
                    JsonSerializer.Deserialize <OpsGenieResponse>(responseBody,
                                                                  new JsonSerializerOptions {
                    PropertyNameCaseInsensitive = true
                });

                result.Response = opsGenieResponse;
            }
            catch (Exception ex)
            {
                result.Ok    = false;
                result.Error = ex;
            }


            return(result);
        }
Exemplo n.º 2
0
        public async Task OnAsync(Event <LogEventData> evt)
        {
            if (evt == null)
            {
                throw new ArgumentNullException(nameof(evt));
            }
            var suppressDiff = DateTime.UtcNow - _lastAlert;

            if (suppressDiff.TotalSeconds < _suppressionTime.TotalSeconds)
            {
                return;
            }

            var message      = _generateMessage.Render(evt);
            var description  = _generateDescription.Render(evt);
            var priority     = ComputePriority(evt);
            var tags         = ComputeTags(evt);
            var responders   = ComputeResponders(evt);
            var notification = Guid.NewGuid().ToString("n");

            var alert = new OpsgenieAlert(
                message,
                evt.Id,
                description,
                priority.ToString(),
                responders,
                new Dictionary <string, string>
            {
                { "Seq Host Name", Host.InstanceName },
                { "Seq Host URL", Host.BaseUri },
                { "Seq App Instance", App.Title },
                { "Seq App Instance Id", App.Id },
                { "Seq ID", evt.Id },
                {
                    "Seq URL",
                    !string.IsNullOrEmpty(evt.Id) && !string.IsNullOrEmpty(Host.BaseUri)
                            ? string.Concat(Host.BaseUri, "#/events?filter=@Id%20%3D%20'", evt.Id,
                                            "'&amp;show=expanded")
                            : ""
                },
                { "Seq Timestamp UTC", evt.TimestampUtc.ToString("O") },
                { "Seq Event Type", evt.EventType.ToString() },
                { "Seq Notification Id", notification }
            },
                Host.BaseUri,
                tags);

            var log    = Log.ForContext("Notification", notification);
            var result = new OpsGenieResult();

            try
            {
                // Log our intent to alert OpsGenie with details that could be re-fired to another app if needed
                log.ForContext("Alert", alert, destructureObjects: true)
                .Debug("Sending alert to OpsGenie API");

                result = await ApiClient.CreateAsync(alert);

                //Log the result with details that could be re-fired to another app if needed
                log
                .ForContext("Alert", alert, destructureObjects: true)
                .ForContext("Result", result, destructureObjects: true)
                .Debug("OpsGenie API responded with {Response}: {ResultCode}/{Reason}", result.Response.Result,
                       result.HttpResponse.StatusCode, result.HttpResponse.ReasonPhrase);
                _lastAlert = DateTime.UtcNow;
            }

            catch (Exception ex)
            {
                //Log an error which could be fired to another app (eg. alert via email of an OpsGenie alert failure, or raise a Jira) and include details of the alert
                log
                .ForContext("Alert", alert, destructureObjects: true)
                .ForContext("Result", result, destructureObjects: true)
                .Error(ex, "OpsGenie alert creation failed");
            }
        }