dead_bro 0.2.22 → 0.2.24

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 14aa1509e04c9bf1848883cc5f2a12e7783e5ac4c123c9bcddde301948e30bfb
4
- data.tar.gz: b3ec01b52a1e63efda701520a014f040fff96cedcbf36fe0827f4a7e892aac90
3
+ metadata.gz: 2123fe4aad1331c3a33afdf2e5f9cffcfac75f5893982522569da053fc4ea403
4
+ data.tar.gz: a00a450442279e95799fb6ddd3e2d55b4b18e8ed26926f55c09ea076f12d02eb
5
5
  SHA512:
6
- metadata.gz: 0c1f24b1718fa9aacf383d22e3b6982e76e7c5aa2c1ccf989d9c411bb0771b0a5fae60d9a780bbe845fac84cafc1a7f876b305005b9e5f3c07f82a7005b0775b
7
- data.tar.gz: 469f652352cb2544b882b82dc528dced3e4725a34da033a14dd54b6e7742691a189eed1079dac1684e2da24ba587a955d4fcf3034c1c50f6a2f0d9bf514b2311
6
+ metadata.gz: ba0645dca3b3552e6c8b4aaef11e8f008e7822ba5d67ff9b8133c05bfe21b0a719252f655f7a893e99369aafe4c6e2ac3c886a372bc353b48c044d22b83d7f36
7
+ data.tar.gz: dffa7888b43a8642fb9ee32ec9334f8cb13dab4c176353171a533780d086eca99257198e0f9a4d0609cc6897a5d51a7591213da3442929c82b2bdf00aa65d738
data/README.md CHANGED
@@ -14,13 +14,21 @@ Add to your Gemfile:
14
14
  gem "dead_bro", git: "https://github.com/rubydevro/dead_bro.git"
15
15
  ```
16
16
 
17
+ Then run the install generator to create the initializer:
18
+
19
+ ```sh
20
+ bin/rails generate dead_bro:install
21
+ ```
22
+
23
+ This creates `config/initializers/dead_bro.rb`. Set `DEAD_BRO_API_KEY` in your environment and you're done.
24
+
17
25
  ## Usage
18
26
 
19
27
  By default, if Rails is present, DeadBro auto-subscribes to `process_action.action_controller` and posts metrics asynchronously.
20
28
 
21
29
  ### Required: API key in your app
22
30
 
23
- Add an initializer (for example `config/initializers/dead_bro.rb`) and set your API key from the environment, [Rails credentials](https://guides.rubyonrails.org/security.html#custom-credentials), or another secret store:
31
+ The install generator above creates `config/initializers/dead_bro.rb` for you. If you prefer to add it manually, create the file and set your API key from the environment, [Rails credentials](https://guides.rubyonrails.org/security.html#custom-credentials), or another secret store:
24
32
 
25
33
  ```ruby
26
34
  DeadBro.configure do |config|
@@ -372,27 +372,6 @@ module DeadBro
372
372
  nil
373
373
  end
374
374
 
375
- def process_hostname
376
- if defined?(ProcessInfo)
377
- begin
378
- ProcessInfo.safe_hostname
379
- rescue
380
- default_hostname
381
- end
382
- else
383
- default_hostname
384
- end
385
- rescue
386
- default_hostname
387
- end
388
-
389
- def default_hostname
390
- require "socket"
391
- Socket.gethostname
392
- rescue
393
- "unknown"
394
- end
395
-
396
375
  def safe_collect
397
376
  yield
398
377
  rescue => e
@@ -2,7 +2,6 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require "rbconfig"
5
- require "socket"
6
5
 
7
6
  module DeadBro
8
7
  module Collectors
@@ -20,7 +19,7 @@ module DeadBro
20
19
  {
21
20
  kind: DeadBro.process_kind,
22
21
  pid: Process.pid,
23
- hostname: safe_hostname,
22
+ hostname: DeadBro.safe_hostname,
24
23
  boot_time: rails_boot_time,
25
24
  uptime_s: uptime_seconds(now),
26
25
  ruby_version: RUBY_VERSION,
@@ -52,12 +51,6 @@ module DeadBro
52
51
  false
53
52
  end
54
53
 
55
- def safe_hostname
56
- Socket.gethostname
57
- rescue
58
- "unknown"
59
- end
60
-
61
54
  def rails_boot_time
62
55
  return nil unless defined?(Rails)
63
56
 
@@ -86,15 +86,7 @@ module DeadBro
86
86
  end
87
87
 
88
88
  def redis_key(key)
89
- env = DeadBro.env
90
- host = begin
91
- require "socket"
92
- Socket.gethostname
93
- rescue
94
- "unknown"
95
- end
96
-
97
- "dead_bro:metrics:#{env}:#{host}:#{key}"
89
+ "dead_bro:metrics:#{DeadBro.env}:#{DeadBro.safe_hostname}:#{key}"
98
90
  end
99
91
 
100
92
  def file_path(key)
@@ -180,6 +180,9 @@ module DeadBro
180
180
  return stripped
181
181
  end
182
182
 
183
+ revision = read_revision_file
184
+ return revision unless revision.nil? || revision.empty?
185
+
183
186
  DeadBro.process_deploy_id
184
187
  end
185
188
 
@@ -249,6 +252,19 @@ module DeadBro
249
252
 
250
253
  private
251
254
 
255
+ # Capistrano (and similar) write the deployed git SHA to REVISION in the app root.
256
+ def read_revision_file
257
+ return nil unless defined?(Rails) && Rails.respond_to?(:root)
258
+
259
+ path = Rails.root.join("REVISION")
260
+ return nil unless path.file?
261
+
262
+ stripped = File.read(path).strip
263
+ stripped.empty? ? nil : stripped
264
+ rescue
265
+ nil
266
+ end
267
+
252
268
  # Turn a list of user-facing patterns into {pattern, has_hash, regex}
253
269
  # entries. Regex is nil when the pattern is a plain literal (cheaper eq
254
270
  # compare). Compiling up-front removes per-request regex allocation.
@@ -105,6 +105,7 @@ module DeadBro
105
105
  job_id: data[:job].job_id,
106
106
  queue_name: data[:job].queue_name,
107
107
  arguments: safe_arguments(data[:job].arguments),
108
+ started_at: started.utc.iso8601(3),
108
109
  duration_ms: duration_ms,
109
110
  queue_duration_ms: queue_duration_ms,
110
111
  db_connection_wait_ms: db_connection_stats[:wait_ms],
@@ -114,7 +115,7 @@ module DeadBro
114
115
  status: "completed",
115
116
  sql_queries: sql_queries,
116
117
  rails_env: DeadBro.env,
117
- host: safe_host,
118
+ host: DeadBro.safe_hostname,
118
119
  process_kind: DeadBro.process_kind,
119
120
  memory_usage: memory_usage_mb,
120
121
  gc_stats: gc_stats,
@@ -201,6 +202,7 @@ module DeadBro
201
202
  job_id: data[:job].job_id,
202
203
  queue_name: data[:job].queue_name,
203
204
  arguments: safe_arguments(data[:job].arguments),
205
+ started_at: started.utc.iso8601(3),
204
206
  duration_ms: duration_ms,
205
207
  queue_duration_ms: queue_duration_ms,
206
208
  db_connection_wait_ms: db_connection_stats[:wait_ms],
@@ -213,7 +215,7 @@ module DeadBro
213
215
  message: exception&.message&.to_s&.[](0, 1000),
214
216
  backtrace: Array(exception&.backtrace).first(50),
215
217
  rails_env: DeadBro.env,
216
- host: safe_host,
218
+ host: DeadBro.safe_hostname,
217
219
  process_kind: DeadBro.process_kind,
218
220
  memory_usage: memory_usage_mb,
219
221
  gc_stats: gc_stats,
@@ -287,18 +289,6 @@ module DeadBro
287
289
  []
288
290
  end
289
291
 
290
- def self.safe_host
291
- if defined?(Rails) && Rails.respond_to?(:application)
292
- begin
293
- Rails.application.class.module_parent_name
294
- rescue
295
- ""
296
- end
297
- else
298
- ""
299
- end
300
- end
301
-
302
292
  def self.memory_usage_mb
303
293
  DeadBro::MemoryHelpers.rss_mb
304
294
  rescue
@@ -67,7 +67,7 @@ module DeadBro
67
67
  def collect_and_send_stats
68
68
  payload = {
69
69
  environment: DeadBro.env,
70
- host: process_hostname,
70
+ host: DeadBro.safe_hostname,
71
71
  pid: Process.pid,
72
72
  process_kind: DeadBro.process_kind,
73
73
  current_time: Time.now.utc.iso8601,
@@ -89,13 +89,6 @@ module DeadBro
89
89
  @client.post_monitor_stats(payload)
90
90
  end
91
91
 
92
- def process_hostname
93
- require "socket"
94
- Socket.gethostname
95
- rescue
96
- "unknown"
97
- end
98
-
99
92
  def log_error(message)
100
93
  msg = "[DeadBro::Monitor] #{message}"
101
94
  if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
@@ -136,7 +136,7 @@ module DeadBro
136
136
  status: data[:status],
137
137
  duration_ms: duration_ms,
138
138
  rails_env: DeadBro.env,
139
- host: safe_host,
139
+ host: DeadBro.safe_hostname,
140
140
  process_kind: DeadBro.process_kind,
141
141
  params: safe_params(data),
142
142
  user_agent: safe_user_agent(data),
@@ -165,10 +165,11 @@ module DeadBro
165
165
  method: data[:method],
166
166
  path: safe_path(data),
167
167
  status: data[:status],
168
+ started_at: started.utc.iso8601(3),
168
169
  duration_ms: duration_ms,
169
170
  view_runtime_ms: data[:view_runtime],
170
171
  db_runtime_ms: data[:db_runtime],
171
- host: safe_host,
172
+ host: DeadBro.safe_hostname,
172
173
  rails_env: DeadBro.env,
173
174
  process_kind: DeadBro.process_kind,
174
175
  params: safe_params(data),
@@ -230,18 +231,6 @@ module DeadBro
230
231
  ""
231
232
  end
232
233
 
233
- def self.safe_host
234
- if defined?(Rails) && Rails.respond_to?(:application)
235
- begin
236
- Rails.application.class.module_parent_name
237
- rescue
238
- ""
239
- end
240
- else
241
- ""
242
- end
243
- end
244
-
245
234
  def self.safe_params(data)
246
235
  return {} unless data[:params]
247
236
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeadBro
4
- VERSION = "0.2.22"
4
+ VERSION = "0.2.24"
5
5
  end
data/lib/dead_bro.rb CHANGED
@@ -128,6 +128,14 @@ module DeadBro
128
128
  "development"
129
129
  end
130
130
 
131
+ # OS hostname for identifying which machine handled a request, job, or metric.
132
+ def self.safe_hostname
133
+ require "socket"
134
+ Socket.gethostname
135
+ rescue
136
+ "unknown"
137
+ end
138
+
131
139
  def self.process_kind
132
140
  @process_kind ||= begin
133
141
  fingerprint = "#{$PROGRAM_NAME} #{process_command_line}".downcase
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+
5
+ module DeadBro
6
+ module Generators
7
+ class InstallGenerator < ::Rails::Generators::Base
8
+ source_root File.expand_path("templates", __dir__)
9
+
10
+ desc "Creates a DeadBro initializer in config/initializers/dead_bro.rb"
11
+
12
+ def create_initializer
13
+ template "dead_bro.rb", "config/initializers/dead_bro.rb"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ DeadBro.configure do |cfg|
4
+ cfg.api_key = ENV["DEAD_BRO_API_KEY"]
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dead_bro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.22
4
+ version: 0.2.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emanuel Comsa
@@ -56,6 +56,8 @@ files:
56
56
  - lib/dead_bro/subscriber.rb
57
57
  - lib/dead_bro/version.rb
58
58
  - lib/dead_bro/view_rendering_subscriber.rb
59
+ - lib/generators/dead_bro/install/install_generator.rb
60
+ - lib/generators/dead_bro/install/templates/dead_bro.rb
59
61
  homepage: https://www.deadbro.com
60
62
  licenses: []
61
63
  metadata: