Mercurial > prosody-modules
annotate mod_sentry/sentry.lib.lua @ 6299:5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
diff --git a/mod_voipms/README.md b/mod_voipms/README.md
new file mode 100644
--- /dev/null
+++ b/mod_voipms/README.md
@@ -0,0 +1,51 @@
+---
+labels:
+- 'Stage-Alpha'
+- 'Type-Web'
+summary: Send and receive SMS/MMS via VoIP.ms APIs.
+rockspec:
+ build:
+ modules:
+ mod_voipms: mod_voipms.lua
+...
+
+Introduction
+============
+
+This is a Prosody module to map JIDs to DIDs on VoIP.ms and support sending/receiving SMS/MMS.
+
+Configuration
+=============
+
+| option | type | default |
+|-----------------------|--------|---------|
+| voipms\_api\_username | string | nil |
+| voipms\_api\_password | string | nil |
+| voipms\_query\_key | string | nil |
+| voipms\_jid\_map | table | nil |
+```
+VirtualHost "sms.example.com"
+modules_enabled = {
+ "voipms";
+}
+voipms_api_username = john@example.com -- E-mail registered with VoIP.ms
+voipms_api_password = abcd1234 -- API password configured in VoIP.ms
+voipms_query_key = some_query_key -- query param 'key' part of your URL callback
+voipms_jid_map = {
+ ["your_jid@your_domain.com"] = "+1234567890"
+}
+```
+
+HTTP
+====
+
+The module is served on Prosody's default HTTP ports at the path /voipms. More details on configuring HTTP modules in Prosody can be found in the HTTP documentation.
+
+VoIP.ms Webhook URL
+===================
+
+This module receives the VoIP.ms Webhook URL (POST) at the /voipms endpoint. It uses the sendSMS/sendMMS GET methods against the VoIP.ms APIs. This is an example webhook to use in VoIP.ms:
+
+```
+https://sms.example.com/voipms?key=some_query_key
+```
diff --git a/mod_voipms/mod_voipms.lua b/mod_voipms/mod_voipms.lua
new file mode 100644
--- /dev/null
+++ b/mod_voipms/mod_voipms.lua
@@ -0,0 +1,166 @@
+local http = require "net.http"
+local json = require "util.json"
+local st = require "util.stanza"
+
+local api_username = module:get_option_string("voipms_api_username")
+local api_password = module:get_option_string("voipms_api_password")
+local query_key = module:get_option("voipms_query_key")
+local jid_map = module:get_option("voipms_jid_map") or {}
+local rest_endpoint = "https://voip.ms/api/v1/rest.php"
+
+if not api_username or not api_password or not query_key then
+ module:log("error", "Missing required config values (voipms_api_username, voipms_api_password, voipms_query_key)")
+ return
+end
+
+module:depends("http")
+
+local function normalize_number(num)
+ if not num then return nil end
+ if num:sub(1, 1) ~= "+" then
+ return "+1" .. num
+ end
+ return num
+end
+
+local function extract_query_key(event)
+ return (event.request.url.query or ""):match("key=([^&]+)")
+end
+
+module:provides("http", {
+ route = {
+ ["POST"] = function(event)
+ local req = event.request
+ local body = req.body or ""
+
+ if extract_query_key(event) ~= query_key then
+ module:log("warn", "Unauthorized webhook: missing or invalid key")
+ return { status_code = 403 }
+ end
+
+ local json_payload, err = json.decode(body)
+ if not json_payload then
+ module:log("warn", "Invalid JSON: %s", err or "unknown error")
+ return { status_code = 400 }
+ end
+
+ local payload = json_payload.data and json_payload.data.payload
+ if not payload then
+ module:log("warn", "Missing payload in JSON")
+ return { status_code = 400 }
+ end
+
+ local from = payload.from and payload.from.phone_number
+ local to_list = payload.to or {}
+ local to = #to_list > 0 and to_list[1].phone_number or nil
+
+ if not from or not to then
+ module:log("warn", "Missing phone numbers (from: %s, to: %s)", tostring(from), tostring(to))
+ return { status_code = 400 }
+ end
+
+ local normalized_to = normalize_number(to)
+ local target_jid = nil
+
+ for jid, did in pairs(jid_map) do
+ if normalize_number(did) == normalized_to then
+ target_jid = jid
+ break
+ end
+ end
+
+ if not target_jid then
+ module:log("warn", "No JID mapping for DID %s", normalized_to)
+ return { status_code = 404 }
+ end
+
+ local normalized_from = normalize_number(from)
+ local message_text = payload.text or ""
+
+ local message = st.message({
+ from = normalized_from .. "@" .. module.host,
+ to = target_jid,
+ type = "chat"
+ }):tag("body"):text(message_text):up()
+
+ if payload.media and #payload.media > 0 then
+ for _, media_item in ipairs(payload.media) do
+ if media_item.url then
+ message_text = message_text .. "\n" .. media_item.url
+ message:tag("x", { xmlns = "jabber:x:oob" })
+ :tag("url"):text(media_item.url):up():up()
+ end
+ end
+ end
+
+ module:send(message)
+ module:log("info", "Delivered SMS from %s to %s", normalized_from, target_jid)
+
+ return { status_code = 204 }
+ end
+ }
+})
+
+module:hook("message/bare", function(event)
+ local stanza = event.stanza
+ if stanza.attr.type ~= "chat" then return end
+
+ local from_jid = stanza.attr.from
+ local to_jid = stanza.attr.to
+ local body = stanza:get_child_text("body")
+ if not body or body == "" then return end
+
+ local from_number = jid_map[from_jid]
+ if not from_number then
+ module:log("warn", "No DID mapping for JID %s", from_jid)
+ return
+ end
+
+ local to_number = to_jid:match("^([^@]+)")
+ if not to_number then
+ module:log("warn", "Malformed JID in message.to: %s", to_jid)
+ return
+ end
+
+ to_number = normalize_number(to_number)
+
+ local media_urls = {}
+ for line in body:gmatch("[^\r\n]+") do
+ if line:match("^https?://") then
+ table.insert(media_urls, line)
+ end
+ end
+
+ local method = (#media_urls > 0) and "sendMMS" or "sendSMS"
+ local query = {
+ api_username = api_username,
+ api_password = api_password,
+ method = method,
+ did = from_number,
+ dst = to_number,
+ message = body
+ }
+
+ if method == "sendMMS" then
+ for i, url in ipairs(media_urls) do
+ query["media_url[" .. (i - 1) .. "]"] = url
+ end
+ end
+
+ local query_str = http.formencode(query)
+
+ http.request(rest_endpoint .. "?" .. query_str, {
+ method = "GET";
+ }, function(response_body, code)
+ if code == 200 then
+ local resp, err = json.decode(response_body)
+ if not resp or resp.status ~= "success" then
+ module:log("error", "Failed to send %s: %s", method, err or (resp and resp.status) or "unknown")
+ else
+ module:log("info", "Sent %s from %s to %s", method, from_number, to_number)
+ end
+ else
+ module:log("error", "HTTP error sending %s: code %s", method, tostring(code))
+ end
+ end)
+end)
| author | Chaz <menel@snikket.de> |
|---|---|
| date | Mon, 21 Jul 2025 23:57:37 +0200 |
| parents | cb3de818ff55 |
| children |
| rev | line source |
|---|---|
|
4288
26bc1ae88ccb
mod_sentry: Ensure frames gets encoded as a JSON array, even if empty
Matthew Wild <mwild1@gmail.com>
parents:
4287
diff
changeset
|
1 local array = require "util.array"; |
|
4283
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local hex = require "util.hex"; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local random = require "util.random"; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local url = require "socket.url"; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local datetime = require "util.datetime".datetime; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local http = require 'net.http' |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local json = require "util.json"; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local errors = require "util.error"; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local promise = require "util.promise"; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local unpack = unpack or table.unpack -- luacheck: ignore |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 local user_agent = ("prosody-mod-%s/%s"):format((module.name:gsub("%W", "-")), (prosody.version:gsub("[^%w.-]", "-"))); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local function generate_event_id() |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 return hex.to(random.bytes(16)); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 local function get_endpoint(server, name) |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 return ("%s/api/%d/%s/"):format(server.base_uri, server.project_id, name); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 -- Parse a DSN string |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 -- https://develop.sentry.dev/sdk/overview/#parsing-the-dsn |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 local function parse_dsn(dsn_string) |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local parsed = url.parse(dsn_string); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 if not parsed then |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 return nil, "unable to parse dsn (url)"; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 local path, project_id = parsed.path:match("^(.*)/(%d+)$"); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 if not path then |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 return nil, "unable to parse dsn (path)"; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 local base_uri = url.build({ |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 scheme = parsed.scheme; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 host = parsed.host; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 port = parsed.port; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 path = path; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 }); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 return { |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 base_uri = base_uri; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 public_key = parsed.user; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 project_id = project_id; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 }; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 local function get_error_data(instance_id, context) |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 local data = { |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 instance_id = instance_id; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 }; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 for k, v in pairs(context) do |
|
4287
2c4157785b42
mod_sentry: Don't include tracebacks in extra context
Matthew Wild <mwild1@gmail.com>
parents:
4286
diff
changeset
|
52 if k ~= "traceback" then |
|
2c4157785b42
mod_sentry: Don't include tracebacks in extra context
Matthew Wild <mwild1@gmail.com>
parents:
4286
diff
changeset
|
53 data[k] = tostring(v); |
|
2c4157785b42
mod_sentry: Don't include tracebacks in extra context
Matthew Wild <mwild1@gmail.com>
parents:
4286
diff
changeset
|
54 end |
|
4283
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 return data; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 local function error_to_sentry_exception(e) |
|
4285
e67081d1f835
mod_sentry: Support for including stack frames in exception events
Matthew Wild <mwild1@gmail.com>
parents:
4284
diff
changeset
|
60 local exception = { |
|
4283
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 type = e.condition or (e.code and tostring(e.code)) or nil; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 value = e.text or tostring(e); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 context = e.source; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 mechanism = { |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 type = "generic"; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 description = "Prosody error object"; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 synthetic = not not e.context.wrapped_error; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 data = get_error_data(e.instance_id, e.context); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 }; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 }; |
|
4285
e67081d1f835
mod_sentry: Support for including stack frames in exception events
Matthew Wild <mwild1@gmail.com>
parents:
4284
diff
changeset
|
71 local traceback = e.context.traceback; |
|
e67081d1f835
mod_sentry: Support for including stack frames in exception events
Matthew Wild <mwild1@gmail.com>
parents:
4284
diff
changeset
|
72 if traceback and type(traceback) == "table" then |
|
4288
26bc1ae88ccb
mod_sentry: Ensure frames gets encoded as a JSON array, even if empty
Matthew Wild <mwild1@gmail.com>
parents:
4287
diff
changeset
|
73 local frames = array(); |
|
4289
de91b7cb7342
mod_sentry: Fix to correctly iterate over traceback
Matthew Wild <mwild1@gmail.com>
parents:
4288
diff
changeset
|
74 for i = #traceback, 1, -1 do |
|
4285
e67081d1f835
mod_sentry: Support for including stack frames in exception events
Matthew Wild <mwild1@gmail.com>
parents:
4284
diff
changeset
|
75 local frame = traceback[i]; |
|
e67081d1f835
mod_sentry: Support for including stack frames in exception events
Matthew Wild <mwild1@gmail.com>
parents:
4284
diff
changeset
|
76 table.insert(frames, { |
|
e67081d1f835
mod_sentry: Support for including stack frames in exception events
Matthew Wild <mwild1@gmail.com>
parents:
4284
diff
changeset
|
77 ["function"] = frame.info.name; |
|
e67081d1f835
mod_sentry: Support for including stack frames in exception events
Matthew Wild <mwild1@gmail.com>
parents:
4284
diff
changeset
|
78 filename = frame.info.short_src; |
|
e67081d1f835
mod_sentry: Support for including stack frames in exception events
Matthew Wild <mwild1@gmail.com>
parents:
4284
diff
changeset
|
79 lineno = frame.info.currentline; |
|
e67081d1f835
mod_sentry: Support for including stack frames in exception events
Matthew Wild <mwild1@gmail.com>
parents:
4284
diff
changeset
|
80 }); |
|
e67081d1f835
mod_sentry: Support for including stack frames in exception events
Matthew Wild <mwild1@gmail.com>
parents:
4284
diff
changeset
|
81 end |
|
4286
64f400a38a30
mod_sentry: Fix syntax for inclusion of stack traces in events
Matthew Wild <mwild1@gmail.com>
parents:
4285
diff
changeset
|
82 exception.stacktrace = { |
|
64f400a38a30
mod_sentry: Fix syntax for inclusion of stack traces in events
Matthew Wild <mwild1@gmail.com>
parents:
4285
diff
changeset
|
83 frames = frames; |
|
64f400a38a30
mod_sentry: Fix syntax for inclusion of stack traces in events
Matthew Wild <mwild1@gmail.com>
parents:
4285
diff
changeset
|
84 }; |
|
4285
e67081d1f835
mod_sentry: Support for including stack frames in exception events
Matthew Wild <mwild1@gmail.com>
parents:
4284
diff
changeset
|
85 end |
|
e67081d1f835
mod_sentry: Support for including stack frames in exception events
Matthew Wild <mwild1@gmail.com>
parents:
4284
diff
changeset
|
86 return exception; |
|
4283
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 local sentry_event_methods = {}; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 local sentry_event_mt = { __index = sentry_event_methods }; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 function sentry_event_methods:set(key, value) |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 self.event[key] = value; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 return self; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 function sentry_event_methods:tag(tag_name, tag_value) |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 local tags = self.event.tags; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 if not tags then |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 tags = {}; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 self.event.tags = tags; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 if type(tag_name) == "string" then |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 tags[tag_name] = tag_value; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 else |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 for k, v in pairs(tag_name) do |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 tags[k] = v; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 return self; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 function sentry_event_methods:extra(key, value) |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 local extra = self.event.extra; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 if not extra then |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 extra = {}; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 self.event.extra = extra; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 if type(key) == "string" then |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 extra[key] = tostring(value); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 else |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 for k, v in pairs(key) do |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 extra[k] = tostring(v); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 return self; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 function sentry_event_methods:message(text) |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 return self:set("message", { formatted = text }); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 function sentry_event_methods:add_exception(e) |
|
4284
b7045af1e5b7
mod_sentry: Fix typo in method name
Matthew Wild <mwild1@gmail.com>
parents:
4283
diff
changeset
|
134 if errors.is_err(e) then |
|
4291
a0d20e378670
mod_sentry: Pull default message from error text or wrapped error (if string)
Matthew Wild <mwild1@gmail.com>
parents:
4289
diff
changeset
|
135 if not self.event.message then |
|
a0d20e378670
mod_sentry: Pull default message from error text or wrapped error (if string)
Matthew Wild <mwild1@gmail.com>
parents:
4289
diff
changeset
|
136 if e.text then |
|
a0d20e378670
mod_sentry: Pull default message from error text or wrapped error (if string)
Matthew Wild <mwild1@gmail.com>
parents:
4289
diff
changeset
|
137 self:message(e.text); |
|
a0d20e378670
mod_sentry: Pull default message from error text or wrapped error (if string)
Matthew Wild <mwild1@gmail.com>
parents:
4289
diff
changeset
|
138 elseif type(e.context.wrapped_error) == "string" then |
|
a0d20e378670
mod_sentry: Pull default message from error text or wrapped error (if string)
Matthew Wild <mwild1@gmail.com>
parents:
4289
diff
changeset
|
139 self:message(e.context.wrapped_error); |
|
a0d20e378670
mod_sentry: Pull default message from error text or wrapped error (if string)
Matthew Wild <mwild1@gmail.com>
parents:
4289
diff
changeset
|
140 end |
|
a0d20e378670
mod_sentry: Pull default message from error text or wrapped error (if string)
Matthew Wild <mwild1@gmail.com>
parents:
4289
diff
changeset
|
141 end |
|
4292
c13b8003ee5c
mod_sentry: Pull message from error *before* we mutate it into something different...
Matthew Wild <mwild1@gmail.com>
parents:
4291
diff
changeset
|
142 e = error_to_sentry_exception(e); |
|
4283
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 elseif type(e) ~= "table" or not (e.type and e.value) then |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 e = error_to_sentry_exception(errors.coerce(nil, e)); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 local exception = self.event.exception; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 if not exception or not exception.values then |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 exception = { values = {} }; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 self.event.exception = exception; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 table.insert(exception.values, e); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 return self; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 function sentry_event_methods:add_breadcrumb(crumb_timestamp, crumb_type, crumb_category, message, data) |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 local crumbs = self.event.breadcrumbs; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 if not crumbs then |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 crumbs = { values = {} }; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 self.event.breadcrumbs = crumbs; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 local crumb = { |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 timestamp = crumb_timestamp and datetime(crumb_timestamp) or self.timestamp; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 type = crumb_type; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 category = crumb_category; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 message = message; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 data = data; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 }; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 table.insert(crumbs.values, crumb); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 return self; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 function sentry_event_methods:add_http_request_breadcrumb(http_request, message) |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 local request_id_message = ("[Request %s]"):format(http_request.id); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 message = message and (request_id_message.." "..message) or request_id_message; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 return self:add_breadcrumb(http_request.time, "http", "net.http", message, { |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 url = http_request.url; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 method = http_request.method or "GET"; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 status_code = http_request.response and http_request.response.code or nil; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 }); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 function sentry_event_methods:set_request(http_request) |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 return self:set("request", { |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 method = http_request.method; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 url = url.build(http_request.url); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 headers = http_request.headers; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 env = { |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 REMOTE_ADDR = http_request.ip; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 }; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 }); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 function sentry_event_methods:send() |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 return self.server:send(self.event); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
201 local sentry_mt = { } |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 sentry_mt.__index = sentry_mt |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
204 local function new(conf) |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 local server = assert(parse_dsn(conf.dsn)); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 return setmetatable({ |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 server = server; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
208 endpoints = { |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
209 store = get_endpoint(server, "store"); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
210 }; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
211 insecure = conf.insecure; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
212 tags = conf.tags or nil, |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
213 extra = conf.extra or nil, |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
214 server_name = conf.server_name or "undefined"; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
215 logger = conf.logger; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
216 }, sentry_mt); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
217 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
218 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
219 local function resolve_sentry_response(response) |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
220 if response.code == 200 and response.body then |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
221 local data = json.decode(response.body); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
222 return data; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
223 end |
|
4995
cb3de818ff55
mod_sentry: Log warning when server returns unexpected response
Matthew Wild <mwild1@gmail.com>
parents:
4292
diff
changeset
|
224 module:log("warn", "Unexpected response from server: %d: %s", response.code, response.body); |
|
4283
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 return promise.reject(response); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
227 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
228 function sentry_mt:send(event) |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
229 local json_payload = json.encode(event); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
230 local response_promise, err = self:_request(self.endpoints.store, "application/json", json_payload); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
231 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
232 if not response_promise then |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
233 module:log("warn", "Failed to submit to Sentry: %s %s", err, json); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
234 return nil, err; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
235 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
236 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
237 return response_promise:next(resolve_sentry_response), event.event_id; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
238 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
239 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
240 function sentry_mt:_request(endpoint_url, body_type, body) |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
241 local auth_header = ("Sentry sentry_version=7, sentry_client=%s, sentry_timestamp=%s, sentry_key=%s") |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
242 :format(user_agent, datetime(), self.server.public_key); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
243 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
244 return http.request(endpoint_url, { |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
245 headers = { |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
246 ["X-Sentry-Auth"] = auth_header; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
247 ["Content-Type"] = body_type; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
248 ["User-Agent"] = user_agent; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
249 }; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
250 insecure = self.insecure; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
251 body = body; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
252 }); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
253 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
254 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
255 function sentry_mt:event(level, source) |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
256 local event = setmetatable({ |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
257 server = self; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
258 event = { |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
259 event_id = generate_event_id(); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
260 timestamp = datetime(); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
261 platform = "lua"; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
262 server_name = self.server_name; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
263 logger = source or self.logger; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
264 level = level; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
265 }; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
266 }, sentry_event_mt); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
267 if self.tags then |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
268 event:tag(self.tags); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
269 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
270 if self.extra then |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
271 event:extra(self.extra); |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
272 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
273 return event; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
274 end |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
275 |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
276 return { |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
277 new = new; |
|
2ae71126e379
mod_sentry: New module to forward errors to a Sentry server
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
278 }; |
