class Pagerduty::EventsApiV1::Incident

Attributes

incident_key[R]

Public Class Methods

new(config) click to toggle source

@option (see Pagerduty::EventsApiV1#initialize)

@option config [String] incident_key Identifies the incident to which

this trigger event should be applied. If there's no open
(i.e. unresolved) incident with this key, a new one will be created.
If there's already an open incident with a matching key, this event
will be appended to that incident's log. The event key provides an
easy way to "de-dup" problem reports. If this field isn't provided,
PagerDuty will automatically open a new incident with a unique key.
The maximum length is 255 characters.
# File lib/pagerduty/events_api_v1.rb, line 133
def initialize(config)
  @integration_key = config.fetch(:integration_key) do
    raise ArgumentError "integration_key not provided"
  end
  @incident_key = config[:incident_key]
  @transport = Pagerduty::HttpTransport.new(
    path:  "/generic/2010-04-15/create_event.json",
    proxy: config[:http_proxy],
  )
end

Public Instance Methods

acknowledge(description = nil, details = nil) click to toggle source

Acknowledge the referenced incident. While an incident is acknowledged, it won’t generate any additional notifications, even if it receives new trigger events. Send PagerDuty an acknowledge event when you know someone is presently working on the problem.

@example Acknowledge the incident

incident.acknowledge

@example Acknowledge, providing a description and extra details

incident.acknowledge(
  "Engineers are investigating the incident",
  {
    ping_time: "1700ms",
    load_avg:  0.71,
  }
)

@param [String] description Text that will appear in the incident’s log

associated with this event.

@param [Hash] details An arbitrary hash containing any data you’d like

included in the incident log.

@return [Pagerduty::EventsApiV1::Incident] self

@raise [PagerdutyException] If PagerDuty responds with a status that is

not "success"
# File lib/pagerduty/events_api_v1.rb, line 218
def acknowledge(description = nil, details = nil)
  modify_incident("acknowledge", description, details)
end
resolve(description = nil, details = nil) click to toggle source

Resolve the referenced incident. Once an incident is resolved, it won’t generate any additional notifications. New trigger events with the same incident_key as a resolved incident won’t re-open the incident. Instead, a new incident will be created. Send PagerDuty a resolve event when the problem that caused the initial trigger event has been fixed.

@example Resolve the incident

incident.resolve

@example Resolve, providing a description and extra details

incident.resolve(
  "A fix has been deployed and the service has recovered",
  {
    ping_time: "130ms",
    load_avg:  0.23,
  }
)

@param [String] description Text that will appear in the incident’s log

associated with this event.

@param [Hash] details An arbitrary hash containing any data you’d like

included in the incident log.

@return [Pagerduty::EventsApiV1::Incident] self

@raise [PagerdutyException] If PagerDuty responds with a status that is

not "success"
# File lib/pagerduty/events_api_v1.rb, line 251
def resolve(description = nil, details = nil)
  modify_incident("resolve", description, details)
end
trigger(description, options = {}) click to toggle source

Send PagerDuty a trigger event to report a new or ongoing problem. When PagerDuty receives a trigger event, it will either open a new incident, or add a new trigger log entry to an existing incident, depending on the provided incident_key.

@example Trigger or update an incident

incident.trigger(
  "<A description of the event or outage>"
)

@example Trigger or update an incident, providing more context

incident.trigger(
  "FAILURE for production/HTTP on machine srv01.acme.com",
  client:     "Sample Monitoring Service",
  client_url: "https://monitoring.service.com",
  contexts:   [
    {
      type: "link",
      href: "http://acme.pagerduty.com",
      text: "View the incident on PagerDuty",
    },
    {
      type: "image",
      src:  "https://chart.googleapis.com/chart.png",
    }
  ],
  details:    {
    ping_time: "1500ms",
    load_avg:  0.75,
  },
)

@param (see Pagerduty::EventsApiV1#trigger) @option (see Pagerduty::EventsApiV1#trigger)

# File lib/pagerduty/events_api_v1.rb, line 178
def trigger(description, options = {})
  if options.key?(:incident_key)
    raise ArgumentError, "incident_key provided"
  end

  options = options.merge(description: description)
  options[:incident_key] = @incident_key unless @incident_key.nil?
  response = api_call("trigger", options)
  @incident_key = response["incident_key"]
  self
end

Private Instance Methods

api_call(event_type, args) click to toggle source
# File lib/pagerduty/events_api_v1.rb, line 265
def api_call(event_type, args)
  args = args.merge(
    service_key: @integration_key,
    event_type:  event_type,
  )
  response = @transport.send_payload(args)
  unless response["status"] == "success"
    raise PagerdutyException.new(self, response, response["message"])
  end

  response
end
modify_incident(event_type, description, details) click to toggle source
# File lib/pagerduty/events_api_v1.rb, line 257
def modify_incident(event_type, description, details)
  options = { incident_key: incident_key }
  options[:description] = description if description
  options[:details] = details if details
  api_call(event_type, options)
  self
end