AppBeat Blog

Perfect Monitoring for Your Cloud

How to develop PagerDuty support in C#

We are currently working on version of AppBeat which will have support for PagerDuty (third party incident management tool).

Their API is very easy to use. Here is code example from AppBeat which sends PagerDuty notifications:

namespace AppBeat.Core.Notifications
{
    class PagerDuty
    {
        //NOTE: this is dispatched in separate thread, exception handling is done by NotificationManager
        public static bool Send(ThirdPartyDispatcher.CustomNotification notification)
        {
            var settings = (Types.CustomNotificationPagerDuty)notification.Settings;

            if (string.IsNullOrWhiteSpace(settings.ServiceKey))
            {
                throw new Exception("ServiceKey is required!");
            }

            string incidentKey = Guid.NewGuid().Normalize();

            var req = new PagerDutyReq()
            {
                service_key = settings.ServiceKey,
                description = notification.ShortMessage,
                incident_key = incidentKey,
                details = new { Message = notification?.Details?.Output?.Details },
                event_type = "trigger",
                client = "AppBeat",
                client_url = "https://appbeat.io"
            };

            using (var wc = new WebClient())
            {
                var res = JsonConvert.DeserializeObject<PagerDutyRes>(wc.UploadString("https://events.pagerduty.com/generic/2010-04-15/create_event.json", JsonConvert.SerializeObject(req)));
                return res.IsOk();
            }
        }

        class PagerDutyReq
        {
            public string service_key
            {
                get; set;
            }

            public string event_type
            {
                get; set;
            }

            public string incident_key
            {
                get; set;
            }

            public string description
            {
                get; set;
            }

            public string client
            {
                get; set;
            }

            public string client_url
            {
                get; set;
            }

            public object details
            {
                get; set;
            }
        }

        class PagerDutyRes
        {
            public string status
            {
                get; set;
            }

            public string message
            {
                get; set;
            }

            public string incident_key
            {
                get; set;
            }

            public bool IsOk()
            {
                return string.Compare(status, "success", StringComparison.InvariantCultureIgnoreCase) == 0;
            }
        }
    }
}

To use this code you must first create service on PagerDuty with API integration. You then receive unique service key which you use in your API. It can't be more simple than this :)

Comments are closed