Files
ubicloud/model/page.rb
Enes Cakir a2cd35fcaf Add extra data and severity to pages
The `severity` field allows us to designate the alert's severity level.
PagerDuty uses this field to assess the alert's urgency. The severity
field can be assigned one of the following values: `critical`, `error`,
`warning`, `info` [^1]. In the past, we assigned the `error` value to
all alerts.

Additionally, I added the `extra_data` parameter. This facilitates the
addition of extra details to incidents, along with related resources.
For instance, we can include utilization in capacity incidents.

While I usually appreciate the splat operator `*tag_parts`, its
comprehension becomes challenging as the number of parameters increases.
Therefore, I modified it to accept a list of tags.

[^1]: https://support.pagerduty.com/docs/dynamic-notifications#severity-and-urgency-mapping
2024-07-12 14:36:29 +03:00

53 lines
1.3 KiB
Ruby

# frozen_string_literal: true
require_relative "../model"
require "pagerduty"
require "openssl"
class Page < Sequel::Model
dataset_module do
def active
where(resolved_at: nil)
end
end
include SemaphoreMethods
include ResourceMethods
semaphore :resolve
def pagerduty_client
@@pagerduty_client ||= Pagerduty.build(integration_key: Config.pagerduty_key, api_version: 2)
end
def trigger
return unless Config.pagerduty_key
links = []
details.fetch("related_resources", []).each do |ubid|
links << {href: Config.pagerduty_log_link.gsub("<ubid>", ubid), text: "View #{ubid} Logs"} if Config.pagerduty_log_link
end
incident = pagerduty_client.incident(OpenSSL::HMAC.hexdigest("SHA256", "ubicloud-page-key", tag))
incident.trigger(summary: summary, severity: severity, source: "clover", custom_details: details, links: links)
end
def resolve
update(resolved_at: Time.now)
return unless Config.pagerduty_key
incident = pagerduty_client.incident(OpenSSL::HMAC.hexdigest("SHA256", "ubicloud-page-key", tag))
incident.resolve
end
def self.generate_tag(*tag_parts)
tag_parts.join("-")
end
def self.from_tag_parts(*tag_parts)
tag = Page.generate_tag(tag_parts)
Page.active.where(tag: tag).first
end
end