tg_error_notifier 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +12 -0
- data/lib/tg_error_notifier/configuration.rb +13 -1
- data/lib/tg_error_notifier/notifier.rb +36 -1
- data/lib/tg_error_notifier/version.rb +1 -1
- data/lib/tg_error_notifier.rb +5 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 785aa28f5a62476e0f827430090ff0dad300edd8277b54672b88f3b89a7ee730
|
|
4
|
+
data.tar.gz: 680dea5d7f8440e07b41e5df415483e35a0aed5b8adbf0b5f9169302c1306422
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 001f1b8fb2fb679ea77acb1d1761f6fe67f3736b08be8be1fa5c9541e45818b137a37b9c39ea4ce7d59ede8cc654bdd9689be76959a4364253add6fccb4b0535
|
|
7
|
+
data.tar.gz: 58ccd0356a6699a0fafff07bd90d461a8a3654db0d00813faa5a3fc7b826593f737924756127fa809d7bf1be65eb1420460b7731cb9cf46349d19160f10291cc
|
data/README.md
CHANGED
|
@@ -90,3 +90,15 @@ end
|
|
|
90
90
|
- `{ sent: true, status: :sent, code: 200 }`
|
|
91
91
|
- `{ sent: false, status: :skipped, reason: "missing_chat_id" }`
|
|
92
92
|
- `{ sent: false, status: :failed, reason: "telegram_api_error", code: 400, body: "..." }`
|
|
93
|
+
|
|
94
|
+
## Manual message
|
|
95
|
+
```ruby
|
|
96
|
+
TgErrorNotifier.capture_message(
|
|
97
|
+
"Background sync started",
|
|
98
|
+
level: :info,
|
|
99
|
+
source: "custom",
|
|
100
|
+
context: { feature: "sync", user_id: current_user&.id }
|
|
101
|
+
)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
`capture_message` returns the same diagnostic hash format as `capture_exception`.
|
|
@@ -15,7 +15,11 @@ module TgErrorNotifier
|
|
|
15
15
|
:read_timeout,
|
|
16
16
|
:logger,
|
|
17
17
|
:include_backtrace,
|
|
18
|
-
:active_job_enabled
|
|
18
|
+
:active_job_enabled,
|
|
19
|
+
:proxy_addr,
|
|
20
|
+
:proxy_port,
|
|
21
|
+
:proxy_user,
|
|
22
|
+
:proxy_pass
|
|
19
23
|
|
|
20
24
|
def initialize
|
|
21
25
|
@enabled = true
|
|
@@ -36,6 +40,14 @@ module TgErrorNotifier
|
|
|
36
40
|
@logger = nil
|
|
37
41
|
@include_backtrace = true
|
|
38
42
|
@active_job_enabled = true
|
|
43
|
+
@proxy_addr = nil
|
|
44
|
+
@proxy_port = nil
|
|
45
|
+
@proxy_user = nil
|
|
46
|
+
@proxy_pass = nil
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def proxy?
|
|
50
|
+
proxy_addr.present? && proxy_port.present?
|
|
39
51
|
end
|
|
40
52
|
end
|
|
41
53
|
end
|
|
@@ -30,6 +30,20 @@ module TgErrorNotifier
|
|
|
30
30
|
{ sent: false, status: :failed, reason: e.class.name, error: e.message }
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
+
def notify_message(message:, level:, source:, context: {})
|
|
34
|
+
enabled_check = enabled_status
|
|
35
|
+
unless enabled_check[:enabled]
|
|
36
|
+
log("skipped: #{enabled_check[:reason]}")
|
|
37
|
+
return { sent: false, status: :skipped, reason: enabled_check[:reason] }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
payload = build_message_payload(message: message, level: level, source: source, context: context)
|
|
41
|
+
send_payload(payload)
|
|
42
|
+
rescue StandardError => e
|
|
43
|
+
log("notify_message failed: #{e.class}: #{e.message}")
|
|
44
|
+
{ sent: false, status: :failed, reason: e.class.name, error: e.message }
|
|
45
|
+
end
|
|
46
|
+
|
|
33
47
|
private
|
|
34
48
|
|
|
35
49
|
attr_reader :config
|
|
@@ -61,7 +75,11 @@ module TgErrorNotifier
|
|
|
61
75
|
request["Content-Type"] = "application/json"
|
|
62
76
|
request.body = payload.to_json
|
|
63
77
|
|
|
64
|
-
http =
|
|
78
|
+
http = if config.proxy?
|
|
79
|
+
Net::HTTP.new(uri.host, uri.port, resolve(config.proxy_addr), resolve(config.proxy_port).to_i, resolve(config.proxy_user), resolve(config.proxy_pass))
|
|
80
|
+
else
|
|
81
|
+
Net::HTTP.new(uri.host, uri.port)
|
|
82
|
+
end
|
|
65
83
|
http.use_ssl = uri.scheme == "https"
|
|
66
84
|
http.open_timeout = config.open_timeout
|
|
67
85
|
http.read_timeout = config.read_timeout
|
|
@@ -105,6 +123,23 @@ module TgErrorNotifier
|
|
|
105
123
|
formatted.join("\n")
|
|
106
124
|
end
|
|
107
125
|
|
|
126
|
+
def build_message_payload(message:, level:, source:, context: {})
|
|
127
|
+
text = [
|
|
128
|
+
"<b>ℹ️ #{escape(resolve(config.app_name).to_s)}: #{escape(resolve(config.environment).to_s)}</b>",
|
|
129
|
+
"<b>Source:</b> #{escape(source.to_s)}",
|
|
130
|
+
"<b>Level:</b> <code>#{escape(level.to_s)}</code>",
|
|
131
|
+
"<b>Message:</b> #{escape(message.to_s)}",
|
|
132
|
+
context_block(context)
|
|
133
|
+
].compact.join("\n")
|
|
134
|
+
|
|
135
|
+
{
|
|
136
|
+
chat_id: resolve(config.chat_id),
|
|
137
|
+
text: truncate(text),
|
|
138
|
+
parse_mode: "HTML",
|
|
139
|
+
disable_web_page_preview: true
|
|
140
|
+
}
|
|
141
|
+
end
|
|
142
|
+
|
|
108
143
|
def truncate(text)
|
|
109
144
|
return text if text.length <= MAX_MESSAGE_LENGTH
|
|
110
145
|
|
data/lib/tg_error_notifier.rb
CHANGED
|
@@ -27,6 +27,11 @@ module TgErrorNotifier
|
|
|
27
27
|
notify(exception: exception, source: source, context: context)
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
+
# API similar to Sentry.capture_message("text")
|
|
31
|
+
def capture_message(message, level: :info, source: "manual", context: {})
|
|
32
|
+
notifier.notify_message(message: message, level: level, source: source, context: context)
|
|
33
|
+
end
|
|
34
|
+
|
|
30
35
|
private
|
|
31
36
|
|
|
32
37
|
def notifier
|