brainzlab 0.1.2 → 0.1.3

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.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +6 -21
  3. data/README.md +16 -2
  4. data/lib/brainzlab/beacon/client.rb +38 -40
  5. data/lib/brainzlab/beacon/provisioner.rb +1 -1
  6. data/lib/brainzlab/beacon.rb +15 -15
  7. data/lib/brainzlab/configuration.rb +92 -90
  8. data/lib/brainzlab/context.rb +2 -3
  9. data/lib/brainzlab/cortex/client.rb +29 -31
  10. data/lib/brainzlab/cortex/provisioner.rb +1 -1
  11. data/lib/brainzlab/cortex.rb +7 -11
  12. data/lib/brainzlab/dendrite/client.rb +42 -44
  13. data/lib/brainzlab/dendrite/provisioner.rb +1 -1
  14. data/lib/brainzlab/dendrite.rb +4 -4
  15. data/lib/brainzlab/devtools/data/collector.rb +22 -22
  16. data/lib/brainzlab/devtools/middleware/asset_server.rb +14 -14
  17. data/lib/brainzlab/devtools/middleware/database_handler.rb +52 -55
  18. data/lib/brainzlab/devtools/middleware/debug_panel.rb +19 -19
  19. data/lib/brainzlab/devtools/middleware/error_page.rb +45 -44
  20. data/lib/brainzlab/devtools/renderers/debug_panel_renderer.rb +39 -35
  21. data/lib/brainzlab/devtools/renderers/error_page_renderer.rb +13 -9
  22. data/lib/brainzlab/devtools.rb +11 -11
  23. data/lib/brainzlab/flux/buffer.rb +3 -3
  24. data/lib/brainzlab/flux/client.rb +14 -16
  25. data/lib/brainzlab/flux/provisioner.rb +13 -13
  26. data/lib/brainzlab/flux.rb +8 -8
  27. data/lib/brainzlab/instrumentation/action_mailer.rb +14 -13
  28. data/lib/brainzlab/instrumentation/active_record.rb +13 -15
  29. data/lib/brainzlab/instrumentation/aws.rb +43 -39
  30. data/lib/brainzlab/instrumentation/dalli.rb +20 -20
  31. data/lib/brainzlab/instrumentation/delayed_job.rb +27 -29
  32. data/lib/brainzlab/instrumentation/elasticsearch.rb +23 -24
  33. data/lib/brainzlab/instrumentation/excon.rb +27 -27
  34. data/lib/brainzlab/instrumentation/faraday.rb +3 -4
  35. data/lib/brainzlab/instrumentation/good_job.rb +28 -28
  36. data/lib/brainzlab/instrumentation/grape.rb +24 -24
  37. data/lib/brainzlab/instrumentation/graphql.rb +24 -23
  38. data/lib/brainzlab/instrumentation/httparty.rb +13 -14
  39. data/lib/brainzlab/instrumentation/mongodb.rb +7 -7
  40. data/lib/brainzlab/instrumentation/net_http.rb +6 -6
  41. data/lib/brainzlab/instrumentation/redis.rb +14 -21
  42. data/lib/brainzlab/instrumentation/resque.rb +23 -24
  43. data/lib/brainzlab/instrumentation/sidekiq.rb +29 -28
  44. data/lib/brainzlab/instrumentation/solid_queue.rb +37 -41
  45. data/lib/brainzlab/instrumentation/stripe.rb +36 -37
  46. data/lib/brainzlab/instrumentation/typhoeus.rb +19 -17
  47. data/lib/brainzlab/instrumentation.rb +20 -20
  48. data/lib/brainzlab/nerve/client.rb +38 -40
  49. data/lib/brainzlab/nerve/provisioner.rb +1 -1
  50. data/lib/brainzlab/nerve.rb +6 -6
  51. data/lib/brainzlab/pulse/client.rb +15 -11
  52. data/lib/brainzlab/pulse/instrumentation.rb +61 -57
  53. data/lib/brainzlab/pulse/propagation.rb +28 -28
  54. data/lib/brainzlab/pulse/provisioner.rb +12 -12
  55. data/lib/brainzlab/pulse/tracer.rb +3 -3
  56. data/lib/brainzlab/pulse.rb +13 -13
  57. data/lib/brainzlab/rails/log_formatter.rb +127 -121
  58. data/lib/brainzlab/rails/log_subscriber.rb +70 -76
  59. data/lib/brainzlab/rails/railtie.rb +66 -89
  60. data/lib/brainzlab/recall/buffer.rb +1 -1
  61. data/lib/brainzlab/recall/client.rb +14 -10
  62. data/lib/brainzlab/recall/logger.rb +16 -18
  63. data/lib/brainzlab/recall/provisioner.rb +16 -16
  64. data/lib/brainzlab/recall.rb +11 -13
  65. data/lib/brainzlab/reflex/breadcrumbs.rb +2 -2
  66. data/lib/brainzlab/reflex/client.rb +14 -10
  67. data/lib/brainzlab/reflex/provisioner.rb +12 -12
  68. data/lib/brainzlab/reflex.rb +29 -29
  69. data/lib/brainzlab/sentinel/client.rb +40 -42
  70. data/lib/brainzlab/sentinel/provisioner.rb +1 -1
  71. data/lib/brainzlab/sentinel.rb +5 -5
  72. data/lib/brainzlab/signal/client.rb +12 -14
  73. data/lib/brainzlab/signal/provisioner.rb +12 -12
  74. data/lib/brainzlab/signal.rb +7 -7
  75. data/lib/brainzlab/synapse/client.rb +42 -44
  76. data/lib/brainzlab/synapse/provisioner.rb +1 -1
  77. data/lib/brainzlab/synapse.rb +6 -6
  78. data/lib/brainzlab/utilities/circuit_breaker.rb +37 -41
  79. data/lib/brainzlab/utilities/health_check.rb +53 -55
  80. data/lib/brainzlab/utilities/log_formatter.rb +38 -40
  81. data/lib/brainzlab/utilities/rate_limiter.rb +5 -5
  82. data/lib/brainzlab/utilities.rb +4 -4
  83. data/lib/brainzlab/vault/cache.rb +1 -1
  84. data/lib/brainzlab/vault/client.rb +39 -41
  85. data/lib/brainzlab/vault/provisioner.rb +1 -1
  86. data/lib/brainzlab/vault.rb +19 -25
  87. data/lib/brainzlab/version.rb +1 -1
  88. data/lib/brainzlab/vision/client.rb +20 -20
  89. data/lib/brainzlab/vision/provisioner.rb +21 -21
  90. data/lib/brainzlab/vision.rb +17 -19
  91. data/lib/brainzlab-sdk.rb +1 -1
  92. data/lib/brainzlab.rb +22 -24
  93. data/lib/generators/brainzlab/install/install_generator.rb +29 -27
  94. metadata +1 -1
@@ -1,15 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "net/http"
4
- require "json"
5
- require "uri"
3
+ require 'net/http'
4
+ require 'json'
5
+ require 'uri'
6
6
 
7
7
  module BrainzLab
8
8
  module Sentinel
9
9
  class Client
10
10
  def initialize(config)
11
11
  @config = config
12
- @base_url = config.sentinel_url || "https://sentinel.brainzlab.ai"
12
+ @base_url = config.sentinel_url || 'https://sentinel.brainzlab.ai'
13
13
  end
14
14
 
15
15
  # List all registered hosts
@@ -17,14 +17,14 @@ module BrainzLab
17
17
  params = { page: page, per_page: per_page }
18
18
  params[:status] = status if status
19
19
 
20
- response = request(:get, "/api/v1/hosts", params: params)
20
+ response = request(:get, '/api/v1/hosts', params: params)
21
21
 
22
22
  return [] unless response.is_a?(Net::HTTPSuccess)
23
23
 
24
24
  data = JSON.parse(response.body, symbolize_names: true)
25
25
  data[:hosts] || []
26
26
  rescue StandardError => e
27
- log_error("list_hosts", e)
27
+ log_error('list_hosts', e)
28
28
  []
29
29
  end
30
30
 
@@ -36,7 +36,7 @@ module BrainzLab
36
36
 
37
37
  JSON.parse(response.body, symbolize_names: true)
38
38
  rescue StandardError => e
39
- log_error("get_host", e)
39
+ log_error('get_host', e)
40
40
  nil
41
41
  end
42
42
 
@@ -44,9 +44,9 @@ module BrainzLab
44
44
  # @param host_id [String] Host ID
45
45
  # @param period [String] Time period (1h, 6h, 24h, 7d, 30d)
46
46
  # @param metrics [Array<String>] Specific metrics to fetch (cpu, memory, disk, network)
47
- def get_metrics(host_id, period: "1h", metrics: nil)
47
+ def get_metrics(host_id, period: '1h', metrics: nil)
48
48
  params = { period: period }
49
- params[:metrics] = metrics.join(",") if metrics
49
+ params[:metrics] = metrics.join(',') if metrics
50
50
 
51
51
  response = request(:get, "/api/v1/hosts/#{host_id}/metrics", params: params)
52
52
 
@@ -54,12 +54,12 @@ module BrainzLab
54
54
 
55
55
  JSON.parse(response.body, symbolize_names: true)
56
56
  rescue StandardError => e
57
- log_error("get_metrics", e)
57
+ log_error('get_metrics', e)
58
58
  nil
59
59
  end
60
60
 
61
61
  # Get top processes for a host
62
- def get_processes(host_id, sort_by: "cpu", limit: 20)
62
+ def get_processes(host_id, sort_by: 'cpu', limit: 20)
63
63
  params = { sort_by: sort_by, limit: limit }
64
64
 
65
65
  response = request(:get, "/api/v1/hosts/#{host_id}/processes", params: params)
@@ -69,7 +69,7 @@ module BrainzLab
69
69
  data = JSON.parse(response.body, symbolize_names: true)
70
70
  data[:processes] || []
71
71
  rescue StandardError => e
72
- log_error("get_processes", e)
72
+ log_error('get_processes', e)
73
73
  []
74
74
  end
75
75
 
@@ -79,14 +79,14 @@ module BrainzLab
79
79
  params[:host_id] = host_id if host_id
80
80
  params[:status] = status if status
81
81
 
82
- response = request(:get, "/api/v1/containers", params: params)
82
+ response = request(:get, '/api/v1/containers', params: params)
83
83
 
84
84
  return [] unless response.is_a?(Net::HTTPSuccess)
85
85
 
86
86
  data = JSON.parse(response.body, symbolize_names: true)
87
87
  data[:containers] || []
88
88
  rescue StandardError => e
89
- log_error("list_containers", e)
89
+ log_error('list_containers', e)
90
90
  []
91
91
  end
92
92
 
@@ -98,12 +98,12 @@ module BrainzLab
98
98
 
99
99
  JSON.parse(response.body, symbolize_names: true)
100
100
  rescue StandardError => e
101
- log_error("get_container", e)
101
+ log_error('get_container', e)
102
102
  nil
103
103
  end
104
104
 
105
105
  # Get container metrics
106
- def get_container_metrics(container_id, period: "1h")
106
+ def get_container_metrics(container_id, period: '1h')
107
107
  params = { period: period }
108
108
 
109
109
  response = request(:get, "/api/v1/containers/#{container_id}/metrics", params: params)
@@ -112,7 +112,7 @@ module BrainzLab
112
112
 
113
113
  JSON.parse(response.body, symbolize_names: true)
114
114
  rescue StandardError => e
115
- log_error("get_container_metrics", e)
115
+ log_error('get_container_metrics', e)
116
116
  nil
117
117
  end
118
118
 
@@ -123,14 +123,14 @@ module BrainzLab
123
123
  params[:status] = status if status
124
124
  params[:severity] = severity if severity
125
125
 
126
- response = request(:get, "/api/v1/alerts", params: params)
126
+ response = request(:get, '/api/v1/alerts', params: params)
127
127
 
128
128
  return [] unless response.is_a?(Net::HTTPSuccess)
129
129
 
130
130
  data = JSON.parse(response.body, symbolize_names: true)
131
131
  data[:alerts] || []
132
132
  rescue StandardError => e
133
- log_error("get_alerts", e)
133
+ log_error('get_alerts', e)
134
134
  []
135
135
  end
136
136
 
@@ -138,7 +138,7 @@ module BrainzLab
138
138
  def report_metrics(host_id:, metrics:, timestamp: nil)
139
139
  response = request(
140
140
  :post,
141
- "/internal/agent/report",
141
+ '/internal/agent/report',
142
142
  body: {
143
143
  host_id: host_id,
144
144
  metrics: metrics,
@@ -149,21 +149,21 @@ module BrainzLab
149
149
 
150
150
  response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPAccepted)
151
151
  rescue StandardError => e
152
- log_error("report_metrics", e)
152
+ log_error('report_metrics', e)
153
153
  false
154
154
  end
155
155
 
156
156
  def provision(project_id:, app_name:)
157
157
  response = request(
158
158
  :post,
159
- "/api/v1/projects/provision",
159
+ '/api/v1/projects/provision',
160
160
  body: { project_id: project_id, app_name: app_name },
161
161
  use_service_key: true
162
162
  )
163
163
 
164
164
  response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPCreated)
165
165
  rescue StandardError => e
166
- log_error("provision", e)
166
+ log_error('provision', e)
167
167
  false
168
168
  end
169
169
 
@@ -172,36 +172,34 @@ module BrainzLab
172
172
  def request(method, path, headers: {}, body: nil, params: nil, use_service_key: false, use_agent_key: false)
173
173
  uri = URI.parse("#{@base_url}#{path}")
174
174
 
175
- if params
176
- uri.query = URI.encode_www_form(params)
177
- end
175
+ uri.query = URI.encode_www_form(params) if params
178
176
 
179
177
  http = Net::HTTP.new(uri.host, uri.port)
180
- http.use_ssl = uri.scheme == "https"
178
+ http.use_ssl = uri.scheme == 'https'
181
179
  http.open_timeout = 10
182
180
  http.read_timeout = 30
183
181
 
184
182
  request = case method
185
- when :get
186
- Net::HTTP::Get.new(uri)
187
- when :post
188
- Net::HTTP::Post.new(uri)
189
- when :put
190
- Net::HTTP::Put.new(uri)
191
- when :delete
192
- Net::HTTP::Delete.new(uri)
193
- end
194
-
195
- request["Content-Type"] = "application/json"
196
- request["Accept"] = "application/json"
183
+ when :get
184
+ Net::HTTP::Get.new(uri)
185
+ when :post
186
+ Net::HTTP::Post.new(uri)
187
+ when :put
188
+ Net::HTTP::Put.new(uri)
189
+ when :delete
190
+ Net::HTTP::Delete.new(uri)
191
+ end
192
+
193
+ request['Content-Type'] = 'application/json'
194
+ request['Accept'] = 'application/json'
197
195
 
198
196
  if use_service_key
199
- request["X-Service-Key"] = @config.sentinel_master_key || @config.secret_key
197
+ request['X-Service-Key'] = @config.sentinel_master_key || @config.secret_key
200
198
  elsif use_agent_key
201
- request["X-Agent-Key"] = @config.sentinel_agent_key || @config.sentinel_api_key || @config.secret_key
199
+ request['X-Agent-Key'] = @config.sentinel_agent_key || @config.sentinel_api_key || @config.secret_key
202
200
  else
203
201
  auth_key = @config.sentinel_api_key || @config.secret_key
204
- request["Authorization"] = "Bearer #{auth_key}" if auth_key
202
+ request['Authorization'] = "Bearer #{auth_key}" if auth_key
205
203
  end
206
204
 
207
205
  headers.each { |k, v| request[k] = v }
@@ -37,7 +37,7 @@ module BrainzLab
37
37
  end
38
38
 
39
39
  def detect_project_id
40
- ENV["BRAINZLAB_PROJECT_ID"]
40
+ ENV.fetch('BRAINZLAB_PROJECT_ID', nil)
41
41
  end
42
42
  end
43
43
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "sentinel/client"
4
- require_relative "sentinel/provisioner"
3
+ require_relative 'sentinel/client'
4
+ require_relative 'sentinel/provisioner'
5
5
 
6
6
  module BrainzLab
7
7
  module Sentinel
@@ -45,7 +45,7 @@ module BrainzLab
45
45
  # @example
46
46
  # metrics = BrainzLab::Sentinel.metrics(host_id, period: "24h", metrics: ["cpu", "memory"])
47
47
  #
48
- def metrics(host_id, period: "1h", metrics: nil)
48
+ def metrics(host_id, period: '1h', metrics: nil)
49
49
  return nil unless enabled?
50
50
 
51
51
  ensure_provisioned!
@@ -63,7 +63,7 @@ module BrainzLab
63
63
  # @example
64
64
  # procs = BrainzLab::Sentinel.processes(host_id, sort_by: "memory", limit: 10)
65
65
  #
66
- def processes(host_id, sort_by: "cpu", limit: 20)
66
+ def processes(host_id, sort_by: 'cpu', limit: 20)
67
67
  return [] unless enabled?
68
68
 
69
69
  ensure_provisioned!
@@ -105,7 +105,7 @@ module BrainzLab
105
105
  # @param container_id [String] Container ID
106
106
  # @param period [String] Time period
107
107
  # @return [Hash, nil] Container metrics
108
- def container_metrics(container_id, period: "1h")
108
+ def container_metrics(container_id, period: '1h')
109
109
  return nil unless enabled?
110
110
 
111
111
  ensure_provisioned!
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "net/http"
4
- require "json"
5
- require "uri"
3
+ require 'net/http'
4
+ require 'json'
5
+ require 'uri'
6
6
 
7
7
  module BrainzLab
8
8
  module Signal
@@ -12,15 +12,15 @@ module BrainzLab
12
12
  end
13
13
 
14
14
  def send_alert(alert)
15
- post("/api/v1/alerts", alert)
15
+ post('/api/v1/alerts', alert)
16
16
  end
17
17
 
18
18
  def send_notification(notification)
19
- post("/api/v1/notifications", notification)
19
+ post('/api/v1/notifications', notification)
20
20
  end
21
21
 
22
22
  def trigger_rule(payload)
23
- post("/api/v1/rules/trigger", payload)
23
+ post('/api/v1/rules/trigger', payload)
24
24
  end
25
25
 
26
26
  private
@@ -28,24 +28,22 @@ module BrainzLab
28
28
  def post(path, body)
29
29
  uri = URI.parse("#{base_url}#{path}")
30
30
  http = Net::HTTP.new(uri.host, uri.port)
31
- http.use_ssl = uri.scheme == "https"
31
+ http.use_ssl = uri.scheme == 'https'
32
32
  http.open_timeout = 5
33
33
  http.read_timeout = 10
34
34
 
35
35
  request = Net::HTTP::Post.new(uri.path)
36
- request["Content-Type"] = "application/json"
37
- request["Authorization"] = "Bearer #{api_key}"
38
- request["User-Agent"] = "brainzlab-sdk/#{BrainzLab::VERSION}"
36
+ request['Content-Type'] = 'application/json'
37
+ request['Authorization'] = "Bearer #{api_key}"
38
+ request['User-Agent'] = "brainzlab-sdk/#{BrainzLab::VERSION}"
39
39
  request.body = body.to_json
40
40
 
41
41
  response = http.request(request)
42
42
 
43
- unless response.is_a?(Net::HTTPSuccess)
44
- BrainzLab.debug_log("[Signal] Request failed: #{response.code} - #{response.body}")
45
- end
43
+ BrainzLab.debug_log("[Signal] Request failed: #{response.code} - #{response.body}") unless response.is_a?(Net::HTTPSuccess)
46
44
 
47
45
  response
48
- rescue => e
46
+ rescue StandardError => e
49
47
  BrainzLab.debug_log("[Signal] Request error: #{e.message}")
50
48
  nil
51
49
  end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "net/http"
4
- require "json"
5
- require "uri"
3
+ require 'net/http'
4
+ require 'json'
5
+ require 'uri'
6
6
 
7
7
  module BrainzLab
8
8
  module Signal
@@ -16,7 +16,7 @@ module BrainzLab
16
16
  return unless @config.signal_url && !@config.signal_url.to_s.empty?
17
17
  return unless @config.secret_key && !@config.secret_key.to_s.empty?
18
18
 
19
- BrainzLab.debug_log("[Signal] Auto-provisioning project...")
19
+ BrainzLab.debug_log('[Signal] Auto-provisioning project...')
20
20
  provision_project
21
21
  end
22
22
 
@@ -25,16 +25,16 @@ module BrainzLab
25
25
  def provision_project
26
26
  uri = URI.parse("#{@config.signal_url}/api/v1/projects/provision")
27
27
  http = Net::HTTP.new(uri.host, uri.port)
28
- http.use_ssl = uri.scheme == "https"
28
+ http.use_ssl = uri.scheme == 'https'
29
29
  http.open_timeout = 10
30
30
  http.read_timeout = 30
31
31
 
32
32
  request = Net::HTTP::Post.new(uri.path)
33
- request["Content-Type"] = "application/json"
34
- request["Authorization"] = "Bearer #{@config.secret_key}"
35
- request["User-Agent"] = "brainzlab-sdk/#{BrainzLab::VERSION}"
33
+ request['Content-Type'] = 'application/json'
34
+ request['Authorization'] = "Bearer #{@config.secret_key}"
35
+ request['User-Agent'] = "brainzlab-sdk/#{BrainzLab::VERSION}"
36
36
  request.body = {
37
- name: @config.service || "default",
37
+ name: @config.service || 'default',
38
38
  environment: @config.environment
39
39
  }.to_json
40
40
 
@@ -42,12 +42,12 @@ module BrainzLab
42
42
 
43
43
  if response.is_a?(Net::HTTPSuccess)
44
44
  data = JSON.parse(response.body)
45
- @config.signal_api_key = data["api_key"]
46
- BrainzLab.debug_log("[Signal] Project provisioned successfully")
45
+ @config.signal_api_key = data['api_key']
46
+ BrainzLab.debug_log('[Signal] Project provisioned successfully')
47
47
  else
48
48
  BrainzLab.debug_log("[Signal] Provisioning failed: #{response.code} - #{response.body}")
49
49
  end
50
- rescue => e
50
+ rescue StandardError => e
51
51
  BrainzLab.debug_log("[Signal] Provisioning error: #{e.message}")
52
52
  end
53
53
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "signal/client"
4
- require_relative "signal/provisioner"
3
+ require_relative 'signal/client'
4
+ require_relative 'signal/provisioner'
5
5
 
6
6
  module BrainzLab
7
7
  module Signal
@@ -21,7 +21,7 @@ module BrainzLab
21
21
  return unless BrainzLab.configuration.signal_valid?
22
22
 
23
23
  payload = {
24
- type: "alert",
24
+ type: 'alert',
25
25
  name: name,
26
26
  message: message,
27
27
  severity: severity.to_s,
@@ -50,7 +50,7 @@ module BrainzLab
50
50
 
51
51
  channels = Array(channel)
52
52
  payload = {
53
- type: "notification",
53
+ type: 'notification',
54
54
  channels: channels,
55
55
  message: message,
56
56
  title: title,
@@ -73,7 +73,7 @@ module BrainzLab
73
73
  return unless BrainzLab.configuration.signal_valid?
74
74
 
75
75
  payload = {
76
- type: "trigger",
76
+ type: 'trigger',
77
77
  rule: rule_name,
78
78
  context: context,
79
79
  timestamp: Time.now.utc.iso8601(3),
@@ -87,8 +87,8 @@ module BrainzLab
87
87
  # Send a test alert to verify configuration
88
88
  def test!
89
89
  alert(
90
- "test_alert",
91
- "This is a test alert from BrainzLab Signal SDK",
90
+ 'test_alert',
91
+ 'This is a test alert from BrainzLab Signal SDK',
92
92
  severity: :info,
93
93
  data: { test: true, sdk_version: BrainzLab::VERSION }
94
94
  )