Last active
February 20, 2020 10:02
-
-
Save dgl/17bb90a91867b1b5a0e3e782aa9a6cb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local hipchat = import "hipchat.jsonnet"; | |
local params = hipchat.defaultParameters; | |
hipchat.notify({}, params + { | |
APIURL: "http://my.hipchat.local/", | |
AuthToken: "xxxx==", | |
RoomID: 42, | |
from: "alertmanager", | |
message: "test", | |
notify: "blah", | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local defaultParameters = { | |
message_format: "text", | |
color: "", | |
}; | |
# No URL encoding built-in to Jsonnet, so here's one: | |
local urlEncode(q) = | |
std.join("", [if | |
(c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') | |
then c | |
else "%%%02X" % (std.codepoint(c)) | |
for c in std.stringChars(q)]); | |
local generateURL(parameters) = | |
("%(APIURL)sv2/room/%(RoomID)s/notification?auth_token=" % parameters) + urlEncode(parameters.AuthToken); | |
local generateBody(data, parameters) = { | |
body: std.manifestJsonEx({ | |
from: parameters.from, | |
notify: parameters.notify, | |
message: if parameters.message_format == 'html' then | |
parameters.html.message else parameters.message, | |
message_format: parameters.message_format, | |
color: parameters.color, | |
}, "") | |
}.body; | |
local hipchatRequest(data, parameters) = { | |
request: { | |
url: generateURL(parameters), | |
method: "POST", | |
headers: { | |
"Content-type": "application/json" | |
}, | |
body: generateBody(data, parameters), | |
}, | |
response: { | |
accept: { | |
status: [200, 201, 202, 204], | |
}, | |
retry: { | |
status: [429] + std.range(500, 599), | |
} | |
} | |
}; | |
{ | |
defaultParameters: defaultParameters, | |
notify: hipchatRequest, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local pagerduty = import "pagerduty.jsonnet"; | |
local params = pagerduty.defaultParameters; | |
local data = { | |
status: "firing", | |
groupKey: '{}:{alertname="foo"}', | |
}; | |
pagerduty.notify(data, params + { }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To be implemented in native code | |
local util = { sha256(x):: x }; | |
# These defaults match: https://prometheus.io/docs/alerting/configuration/#pagerduty_config | |
local defaultParameters = { | |
# Not set to global.pagerduty_url as a default, as it's assumed in this design | |
# the core doesn't know about notification method types. | |
url: 'https://events.pagerduty.com/v2/enqueue', | |
# Only supporting events v2. Another type could easily be split into a | |
# different file rather than adding conditionals all over this one. | |
routing_key: "", | |
client: '{{ template "pagerduty.default.client" . }}', | |
client_url: '{{ template "pagerduty.default.clientURL" . }}', | |
description: '{{ template "pagerduty.default.description" .}}', | |
// n.b.: Documented as default, actually set in code if empty? Probably | |
// doesn't make a difference here. | |
severity: "error", | |
details: { | |
firing: '{{ template "pagerduty.default.instances" .Alerts.Firing }}', | |
resolved: '{{ template "pagerduty.default.instances" .Alerts.Resolved }}', | |
num_firing: '{{ .Alerts.Firing | len }}', | |
num_resolved: '{{ .Alerts.Resolved | len }}' | |
}, | |
class: null, | |
component: null, | |
group: null, | |
images: [ | |
// {href: "", src: "", alt: ""}, | |
], | |
links: [ | |
// {href: "", text: ""}, | |
], | |
}; | |
local truncate(s) = | |
if std.length(s) < 1024 then s else std.substr(s, 0, 1024) + "..."; | |
local makeRequest(data, parameters) = { | |
url: parameters.url, | |
method: "POST", | |
headers: { | |
"Content-type": "application/json", | |
}, | |
body: std.manifestJsonEx({ | |
client: parameters.client, | |
client_url: parameters.client_url, | |
routing_key: parameters.routing_key, | |
// n.b.: need to add a sha256 function to jsonnet, see https://github.com/google/jsonnet/issues/516 | |
// Can just add one in native code though. | |
dedup_key: util.sha256(data.groupKey), | |
event_action: if data.status == 'firing' then 'trigger' else 'resolve', | |
images: parameters.images, | |
links: parameters.links, | |
payload: std.prune({ | |
summary: truncate(parameters.description), | |
source: parameters.client, | |
severity: parameters.severity, | |
custom_details: parameters.details, | |
class: parameters.class, | |
component: parameters.component, | |
group: parameters.group, | |
}), | |
}, " "), | |
}; | |
local notify(data, parameters) = { | |
request: makeRequest(data, parameters), | |
response: { | |
// https://v2.developer.pagerduty.com/docs/events-api-v2#api-response-codes--retry-logic | |
accept: { | |
status: [202], | |
}, | |
retry: { | |
status: [429] + std.range(500, 599), | |
}, | |
errors: { | |
// TODO: error extraction | |
// Potentially native handling of JSON?, like say | |
// json_error_fields: ["message", "errors"] | |
}, | |
} | |
}; | |
{ | |
options: { | |
apiVersion: 1, | |
// Unknown fields in parameters should be errors? | |
strictParameters: true, | |
}, | |
defaultParameters: defaultParameters, | |
notify: notify, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment