Module: BrainzLab

Defined in:
lib/brainzlab.rb,
lib/brainzlab/flux.rb,
lib/brainzlab/debug.rb,
lib/brainzlab/nerve.rb,
lib/brainzlab/pulse.rb,
lib/brainzlab/vault.rb,
lib/brainzlab/beacon.rb,
lib/brainzlab/cortex.rb,
lib/brainzlab/errors.rb,
lib/brainzlab/recall.rb,
lib/brainzlab/reflex.rb,
lib/brainzlab/signal.rb,
lib/brainzlab/vision.rb,
lib/brainzlab/context.rb,
lib/brainzlab/synapse.rb,
lib/brainzlab/testing.rb,
lib/brainzlab/version.rb,
lib/brainzlab/dendrite.rb,
lib/brainzlab/devtools.rb,
lib/brainzlab/sentinel.rb,
lib/brainzlab/utilities.rb,
lib/brainzlab/development.rb,
lib/brainzlab/flux/buffer.rb,
lib/brainzlab/flux/client.rb,
lib/brainzlab/vault/cache.rb,
lib/brainzlab/cortex/cache.rb,
lib/brainzlab/nerve/client.rb,
lib/brainzlab/pulse/client.rb,
lib/brainzlab/pulse/tracer.rb,
lib/brainzlab/vault/client.rb,
lib/brainzlab/beacon/client.rb,
lib/brainzlab/configuration.rb,
lib/brainzlab/cortex/client.rb,
lib/brainzlab/rails/railtie.rb,
lib/brainzlab/recall/buffer.rb,
lib/brainzlab/recall/client.rb,
lib/brainzlab/recall/logger.rb,
lib/brainzlab/reflex/client.rb,
lib/brainzlab/signal/client.rb,
lib/brainzlab/vision/client.rb,
lib/brainzlab/synapse/client.rb,
lib/brainzlab/dendrite/client.rb,
lib/brainzlab/instrumentation.rb,
lib/brainzlab/sentinel/client.rb,
lib/brainzlab/testing/helpers.rb,
lib/brainzlab/flux/provisioner.rb,
lib/brainzlab/testing/matchers.rb,
lib/brainzlab/development/store.rb,
lib/brainzlab/nerve/provisioner.rb,
lib/brainzlab/pulse/propagation.rb,
lib/brainzlab/pulse/provisioner.rb,
lib/brainzlab/vault/provisioner.rb,
lib/brainzlab/beacon/provisioner.rb,
lib/brainzlab/cortex/provisioner.rb,
lib/brainzlab/development/logger.rb,
lib/brainzlab/recall/provisioner.rb,
lib/brainzlab/reflex/breadcrumbs.rb,
lib/brainzlab/reflex/provisioner.rb,
lib/brainzlab/signal/provisioner.rb,
lib/brainzlab/vision/provisioner.rb,
lib/brainzlab/instrumentation/aws.rb,
lib/brainzlab/rails/log_formatter.rb,
lib/brainzlab/synapse/provisioner.rb,
lib/brainzlab/testing/event_store.rb,
lib/brainzlab/dendrite/provisioner.rb,
lib/brainzlab/rails/log_subscriber.rb,
lib/brainzlab/sentinel/provisioner.rb,
lib/brainzlab/instrumentation/dalli.rb,
lib/brainzlab/instrumentation/excon.rb,
lib/brainzlab/instrumentation/grape.rb,
lib/brainzlab/instrumentation/redis.rb,
lib/brainzlab/pulse/instrumentation.rb,
lib/brainzlab/instrumentation/resque.rb,
lib/brainzlab/instrumentation/stripe.rb,
lib/brainzlab/utilities/health_check.rb,
lib/brainzlab/utilities/rate_limiter.rb,
lib/brainzlab/devtools/data/collector.rb,
lib/brainzlab/instrumentation/faraday.rb,
lib/brainzlab/instrumentation/graphql.rb,
lib/brainzlab/instrumentation/mongodb.rb,
lib/brainzlab/instrumentation/sidekiq.rb,
lib/brainzlab/utilities/log_formatter.rb,
lib/brainzlab/instrumentation/good_job.rb,
lib/brainzlab/instrumentation/httparty.rb,
lib/brainzlab/instrumentation/net_http.rb,
lib/brainzlab/instrumentation/railties.rb,
lib/brainzlab/instrumentation/typhoeus.rb,
lib/brainzlab/utilities/circuit_breaker.rb,
lib/brainzlab/instrumentation/active_job.rb,
lib/brainzlab/instrumentation/action_view.rb,
lib/brainzlab/instrumentation/delayed_job.rb,
lib/brainzlab/instrumentation/solid_queue.rb,
lib/brainzlab/instrumentation/action_cable.rb,
lib/brainzlab/instrumentation/action_mailer.rb,
lib/brainzlab/instrumentation/active_record.rb,
lib/brainzlab/instrumentation/elasticsearch.rb,
lib/brainzlab/devtools/middleware/error_page.rb,
lib/brainzlab/instrumentation/action_mailbox.rb,
lib/brainzlab/instrumentation/active_storage.rb,
lib/brainzlab/devtools/middleware/debug_panel.rb,
lib/brainzlab/instrumentation/action_dispatch.rb,
lib/brainzlab/devtools/middleware/asset_server.rb,
lib/brainzlab/instrumentation/action_controller.rb,
lib/brainzlab/instrumentation/rails_deprecation.rb,
lib/brainzlab/devtools/middleware/database_handler.rb,
lib/brainzlab/instrumentation/active_support_cache.rb,
lib/brainzlab/devtools/renderers/error_page_renderer.rb,
lib/brainzlab/devtools/renderers/debug_panel_renderer.rb

Defined Under Namespace

Modules: Beacon, Cortex, Debug, Dendrite, DevTools, Development, ErrorHandler, Flux, Instrumentation, Nerve, Pulse, Rails, Recall, Reflex, Sentinel, Signal, Synapse, Testing, Utilities, Vault, Vision Classes: AuthenticationError, Configuration, ConfigurationError, ConnectionError, Context, Error, NotFoundError, RateLimitError, ServerError, ServiceUnavailableError, TimeoutError, ValidationError

Constant Summary collapse

INSTRUMENTING_KEY =

Thread-local re-entrancy guard for instrumentation. When true, SDK operations that would make HTTP calls are skipped to prevent recursive instrumentation from blocking the host app.

:brainzlab_instrumenting
VERSION =
'0.1.21'

Class Method Summary collapse

Class Method Details

.add_breadcrumb(message, category: 'default', level: :info, data: nil) ⇒ Object

Breadcrumb helpers



113
114
115
# File 'lib/brainzlab.rb', line 113

def add_breadcrumb(message, category: 'default', level: :info, data: nil)
  Reflex.add_breadcrumb(message, category: category, level: level, data: data)
end

.clear_breadcrumbs!Object



117
118
119
# File 'lib/brainzlab.rb', line 117

def clear_breadcrumbs!
  Reflex.clear_breadcrumbs!
end

.clear_context!Object



108
109
110
# File 'lib/brainzlab.rb', line 108

def clear_context!
  Context.clear!
end

.clear_development_events!Object

Clear all events stored in development mode



154
155
156
# File 'lib/brainzlab.rb', line 154

def clear_development_events!
  Development.clear!
end

.configurationObject



69
70
71
# File 'lib/brainzlab.rb', line 69

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



64
65
66
67
# File 'lib/brainzlab.rb', line 64

def configure
  yield(configuration) if block_given?
  configuration
end

.debug?Boolean

Check if debug mode is enabled

Returns:

  • (Boolean)


134
135
136
# File 'lib/brainzlab.rb', line 134

def debug?
  configuration.debug?
end

.debug_log(message) ⇒ Object

Debug logging helper



129
130
131
# File 'lib/brainzlab.rb', line 129

def debug_log(message)
  configuration.debug_log(message)
end

.development_events(service: nil, event_type: nil, since: nil, limit: 100) ⇒ Array<Hash>

Query events stored in development mode

Examples:

BrainzLab.development_events                              # All events
BrainzLab.development_events(service: :recall)            # Only Recall logs
BrainzLab.development_events(service: :reflex, limit: 10) # Last 10 errors
BrainzLab.development_events(since: 1.hour.ago)           # Events from last hour

Parameters:

  • service (Symbol, nil) (defaults to: nil)

    filter by service (:recall, :reflex, :pulse, etc.)

  • event_type (String, nil) (defaults to: nil)

    filter by event type (‘log’, ‘error’, ‘trace’, etc.)

  • since (Time, nil) (defaults to: nil)

    filter events after this time

  • limit (Integer) (defaults to: 100)

    max number of events to return (default: 100)

Returns:

  • (Array<Hash>)

    matching events



149
150
151
# File 'lib/brainzlab.rb', line 149

def development_events(service: nil, event_type: nil, since: nil, limit: 100)
  Development.events(service: service, event_type: event_type, since: since, limit: limit)
end

.development_statsHash

Get stats about stored development events

Returns:

  • (Hash)

    counts by service



160
161
162
# File 'lib/brainzlab.rb', line 160

def development_stats
  Development.stats
end

.health_checkHash

Health check - verifies connectivity to all enabled services

Returns:

  • (Hash)

    Status of each service



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/brainzlab.rb', line 166

def health_check
  results = { status: 'ok', services: {} }

  # Check Recall
  if configuration.recall_enabled
    results[:services][:recall] = check_service_health(
      url: configuration.recall_url,
      name: 'Recall'
    )
  end

  # Check Reflex
  if configuration.reflex_enabled
    results[:services][:reflex] = check_service_health(
      url: configuration.reflex_url,
      name: 'Reflex'
    )
  end

  # Check Pulse
  if configuration.pulse_enabled
    results[:services][:pulse] = check_service_health(
      url: configuration.pulse_url,
      name: 'Pulse'
    )
  end

  # Check Flux
  if configuration.flux_enabled
    results[:services][:flux] = check_service_health(
      url: configuration.flux_url,
      name: 'Flux'
    )
  end

  # Check Signal
  if configuration.signal_enabled
    results[:services][:signal] = check_service_health(
      url: configuration.signal_url,
      name: 'Signal'
    )
  end

  # Check Vault
  if configuration.vault_enabled
    results[:services][:vault] = check_service_health(
      url: configuration.vault_url,
      name: 'Vault'
    )
  end

  # Check Vision
  if configuration.vision_enabled
    results[:services][:vision] = check_service_health(
      url: configuration.vision_url,
      name: 'Vision'
    )
  end

  # Check Cortex
  if configuration.cortex_enabled
    results[:services][:cortex] = check_service_health(
      url: configuration.cortex_url,
      name: 'Cortex'
    )
  end

  # Check Beacon
  if configuration.beacon_enabled
    results[:services][:beacon] = check_service_health(
      url: configuration.beacon_url,
      name: 'Beacon'
    )
  end

  # Check Nerve
  if configuration.nerve_enabled
    results[:services][:nerve] = check_service_health(
      url: configuration.nerve_url,
      name: 'Nerve'
    )
  end

  # Check Dendrite
  if configuration.dendrite_enabled
    results[:services][:dendrite] = check_service_health(
      url: configuration.dendrite_url,
      name: 'Dendrite'
    )
  end

  # Check Sentinel
  if configuration.sentinel_enabled
    results[:services][:sentinel] = check_service_health(
      url: configuration.sentinel_url,
      name: 'Sentinel'
    )
  end

  # Check Synapse
  if configuration.synapse_enabled
    results[:services][:synapse] = check_service_health(
      url: configuration.synapse_url,
      name: 'Synapse'
    )
  end

  # Overall status
  has_failure = results[:services].values.any? { |s| s[:status] == 'error' }
  results[:status] = has_failure ? 'degraded' : 'ok'

  results
end

.instrumenting?Boolean

Returns true when inside an instrumentation handler. Used by Recall.log, Pulse.record_metric, etc. to skip HTTP calls that would block the host app during notification callbacks.

Returns:

  • (Boolean)


47
48
49
# File 'lib/brainzlab.rb', line 47

def instrumenting?
  Thread.current[INSTRUMENTING_KEY] == true
end

.logger(broadcast_to: nil) ⇒ BrainzLab::Recall::Logger

Create a logger that can replace Rails.logger

Parameters:

  • broadcast_to (Logger) (defaults to: nil)

    Optional logger to also send logs to (e.g., original Rails.logger)

Returns:



124
125
126
# File 'lib/brainzlab.rb', line 124

def logger(broadcast_to: nil)
  Recall::Logger.new(nil, broadcast_to: broadcast_to)
end

.reset_configuration!Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/brainzlab.rb', line 73

def reset_configuration!
  @configuration = Configuration.new
  Recall.reset!
  Reflex.reset!
  Pulse.reset!
  Flux.reset!
  Signal.reset!
  Vault.reset!
  Vision.reset!
  Cortex.reset!
  Beacon.reset!
  Nerve.reset!
  Dendrite.reset!
  Sentinel.reset!
  Synapse.reset!
  Development.reset!
end

.set_context(**data) ⇒ Object



96
97
98
# File 'lib/brainzlab.rb', line 96

def set_context(**data)
  Context.current.set_context(**data)
end

.set_tags(**data) ⇒ Object



100
101
102
# File 'lib/brainzlab.rb', line 100

def set_tags(**data)
  Context.current.set_tags(**data)
end

.set_user(id: nil, email: nil, name: nil, **extra) ⇒ Object

Context management



92
93
94
# File 'lib/brainzlab.rb', line 92

def set_user(id: nil, email: nil, name: nil, **extra)
  Context.current.set_user(id: id, email: email, name: name, **extra)
end

.with_context(**data) ⇒ Object



104
105
106
# File 'lib/brainzlab.rb', line 104

def with_context(**data, &)
  Context.current.with_context(**data, &)
end

.with_instrumentation_guardObject

Executes a block within the instrumentation guard. Prevents recursive/cascading SDK HTTP calls from instrumentation handlers.



53
54
55
56
57
58
59
60
61
62
# File 'lib/brainzlab.rb', line 53

def with_instrumentation_guard
  return if Thread.current[INSTRUMENTING_KEY]

  Thread.current[INSTRUMENTING_KEY] = true
  begin
    yield
  ensure
    Thread.current[INSTRUMENTING_KEY] = nil
  end
end