Skip to content

Commit

Permalink
Add Hetrix Tools Integration (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
armiiller authored May 1, 2024
1 parent a3112a1 commit b1997db
Show file tree
Hide file tree
Showing 6 changed files with 404 additions and 0 deletions.
160 changes: 160 additions & 0 deletions app/models/pager_tree/integrations/hetrix_tools/v3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
module PagerTree::Integrations
class HetrixTools::V3 < Integration
OPTIONS = [
{key: :authentication_token, type: :string, default: nil}
]
store_accessor :options, *OPTIONS.map { |x| x[:key] }.map(&:to_s), prefix: "option"

after_initialize do
self.option_authentication_token ||= nil
end

def adapter_should_block_incoming?(request)
self.option_authentication_token.present? && (request.headers["Authorization"] != "Bearer #{self.option_authentication_token}")
end

def adapter_supports_incoming?
true
end

def adapter_supports_outgoing?
false
end

def adapter_incoming_can_defer?
true
end

def adapter_thirdparty_id
try("_adapter_thirdparty_id_#{_webhook_type}") || SecureRandom.hex(16)
end

def adapter_action
try("_adapter_action_#{_webhook_type}") || :other
end

def _adapter_action_uptime
case adapter_incoming_request_params.dig("monitor_status")
when "online" then :resolve
when "offline" then :create
else
:other
end
end

def _adapter_action_blacklist
:create
end

def _adapter_action_resource_usage
:create
end

def adapter_process_create
Alert.new(
title: _title,
description: _description,
thirdparty_id: adapter_thirdparty_id,
dedup_keys: [],
additional_data: _additional_datums
)
end

def _adapter_thirdparty_id_uptime
adapter_incoming_request_params.dig("monitor_id")
end

def _adapter_thirdparty_id_blacklist
SecureRandom.hex(16)
end

def _adapter_thirdparty_id_resource_usage
adapter_incoming_request_params.dig("monitor_id")
end

def _webhook_type
@_webhook_type ||= if _webhook_type_uptime?
:uptime
elsif _webhook_type_resource_usage?
:resource_usage
elsif _webhook_type_blacklist?
:blacklist
else
:unknown
end
end

def _webhook_type_uptime?
adapter_incoming_request_params.dig("monitor_errors").present?
end

def _webhook_type_blacklist?
json = adapter_incoming_request_params.dig("_json")
json.present? && json.is_a?(Array)
end

def _webhook_type_resource_usage?
adapter_incoming_request_params.dig("resource_usage").present?
end

def _title
try("_title_#{_webhook_type}") || "HetrixTools Alert"
end

def _title_uptime
"#{adapter_incoming_request_params.dig("monitor_name")} is #{adapter_incoming_request_params.dig("monitor_status")}"
end

def _title_blacklist
"Blacklist Alert"
end

def _title_resource_usage
"#{adapter_incoming_request_params.dig("monitor_name")} usage alert"
end

def _description
try("_description_#{_webhook_type}") || "No description provided"
end

def _description_uptime
"<p>#{adapter_incoming_request_params.dig("monitor_target")} is #{adapter_incoming_request_params.dig("monitor_status")}</p>" +
adapter_incoming_request_params.dig("monitor_errors").map { |k, v| "<p>#{k}: #{v}</p>" }.join("")
end

def _description_blacklist
adapter_incoming_request_params.dig("_json").map { |x| "#{x["monitor"]} (#{x["blacklisted_now"]})" }.join("<br/>")
end

def _description_resource_usage
[
"<p>Resource Type: #{adapter_incoming_request_params.dig("resource_usage", "resource_type")}</p>",
"<p>Current Usage: #{adapter_incoming_request_params.dig("resource_usage", "current_usage")}</p>",
"<p>Average Usage: #{adapter_incoming_request_params.dig("resource_usage", "average_usage")} / #{adapter_incoming_request_params.dig("resource_usage", "average_minutes")}m</p>"
].join("")
end

def _additional_datums
try("_additional_datums_#{_webhook_type}") || []
end

def _additional_datums_uptime
[
AdditionalDatum.new(format: "datetime", label: "Timestamp", value: Time.at(adapter_incoming_request_params.dig("timestamp"))),
AdditionalDatum.new(format: "text", label: "Monitor Type", value: adapter_incoming_request_params.dig("monitor_type")),
AdditionalDatum.new(format: "link", label: "Monitor Target", value: adapter_incoming_request_params.dig("monitor_target"))
]
end

def _additional_datums_blacklist
[]
end

def _additional_datums_resource_usage
[
AdditionalDatum.new(format: "text", label: "Resource Type", value: adapter_incoming_request_params.dig("resource_usage", "resource_type")),
AdditionalDatum.new(format: "text", label: "Current Usage", value: adapter_incoming_request_params.dig("resource_usage", "current_usage"))
]
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div class="form-group group">
<%= form.label :option_authentication_token %>
<%= form.text_field :option_authentication_token, class: "form-control" %>
<p class="form-hint"><%== t(".option_authentication_token_hint_html") %></p>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="sm:col-span-2">
<dt class="text-sm font-medium text-gray-500">
<%= t("activerecord.attributes.pager_tree/integrations/hetrix_tools/v3.option_authentication_token") %>
</dt>
<dd class="mt-1 text-sm text-gray-900">
<div class="flex items-center gap-2">
<p class="text-sm truncate">
<%= mask integration.option_authentication_token %>
</p>
</div>
</dd>
</div>
6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ en:
option_form_phone_required_hint_html: "Should the phone field be required to submit?"
option_form_description_required_hint_html: "Should the description field be required to submit?"
option_form_urgency_required_hint_html: "Should the urgency field be required to submit?"
hetrix_tools:
v3:
form_options:
option_authentication_token_hint_html: "Authentication Token to be used to authenticate requests from Hetrix Tools servers (optional, <a href=\"https://docs.hetrixtools.com/webhook-authentication-token/\" target=\"_blank\">see docs</a>)"
honeybadger:
v3:
form_options:
Expand Down Expand Up @@ -161,6 +165,8 @@ en:
option_form_phone_required: "Phone Required?"
option_form_description_required: "Description Required?"
option_form_urgency_required: "Urgency Required?"
"pager_tree/integrations/hetrix_tools/v3":
option_authentication_token: "Authentication Token"
"pager_tree/integrations/honeybadger/v3":
option_token: "Honeybadger Token"
"pager_tree/integrations/jira_server/v3":
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/pager_tree/integrations/integrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ healthchecks_v3:
type: "PagerTree::Integrations::Healthchecks::V3"
# options: no_options

hetrix_tools_v3:
type: "PagerTree::Integrations::HetrixTools::V3"
# options: no_options

honeybadger_v3:
type: "PagerTree::Integrations::Honeybadger::V3"
options:
Expand Down
Loading

0 comments on commit b1997db

Please sign in to comment.