BrainzLab Ruby SDK
Official Ruby SDK for BrainzLab - the complete observability platform.
- Recall - Structured logging
- Reflex - Error tracking
- Pulse - APM & distributed tracing
Installation
From RubyGems (recommended)
Add to your Gemfile:
gem 'brainzlab'Then run:
bundle installFrom GitHub Packages
Add the GitHub Packages source to your Gemfile:
source "https://rubygems.pkg.github.com/brainz-lab" do
gem 'brainzlab'
endConfigure Bundler with your GitHub token:
bundle config set --global rubygems.pkg.github.com USERNAME:TOKENQuick Start
Configuration
# config/initializers/brainzlab.rb
BrainzLab.configure do |config|
# Authentication (required)
config.secret_key = ENV['BRAINZLAB_SECRET_KEY']
# Environment
config.environment = Rails.env
config.service = 'my-app'
# Enable/disable products
config.recall_enabled = true # Logging
config.reflex_enabled = true # Error tracking
config.pulse_enabled = true # APM
# Auto-provisioning (creates projects automatically)
config.recall_auto_provision = true
config.reflex_auto_provision = true
config.pulse_auto_provision = true
endRecall - Structured Logging
# Log levels
BrainzLab::Recall.debug("Debug message", details: "...")
BrainzLab::Recall.info("User signed up", user_id: user.id)
BrainzLab::Recall.warn("Rate limit approaching", current: 95, limit: 100)
BrainzLab::Recall.error("Payment failed", error: e.message, amount: 99.99)
BrainzLab::Recall.fatal("Database connection lost")
# With context
BrainzLab::Recall.info("Order created",
order_id: order.id,
user_id: user.id,
total: order.total,
items: order.items.count
)Configuration Options
config.recall_min_level = :info # Minimum log level (:debug, :info, :warn, :error, :fatal)
config.recall_buffer_size = 50 # Batch size before flush
config.recall_flush_interval = 5 # Seconds between flushesReflex - Error Tracking
# Capture exceptions
begin
risky_operation
rescue => e
BrainzLab::Reflex.capture(e,
user_id: current_user.id,
order_id: order.id
)
end
# Add breadcrumbs for context
BrainzLab::Reflex.add_breadcrumb("User clicked checkout",
category: "ui.click",
data: { button: "checkout" }
)
# Set user context
BrainzLab::Reflex.set_user(
id: user.id,
email: user.email,
plan: user.plan
)
# Add tags
BrainzLab::Reflex.set_tags(
environment: "production",
region: "us-east-1"
)Configuration Options
config.reflex_excluded_exceptions = ['ActiveRecord::RecordNotFound']
config.reflex_sample_rate = 1.0 # 1.0 = 100%, 0.5 = 50%
config.reflex_before_send = ->(event) {
# Modify or filter events
event[:tags][:custom] = 'value'
event # Return nil to drop the event
}Pulse - APM & Distributed Tracing
Pulse automatically instruments your application to track performance.
Automatic Instrumentation
The SDK automatically instruments:
| Library | Description |
|---|---|
| Rails/Rack | Request tracing with breakdown |
| Active Record | SQL queries with timing |
| Net::HTTP | Outbound HTTP calls |
| Faraday | HTTP client requests |
| HTTParty | HTTP client requests |
| Redis | Redis commands |
| Sidekiq | Background job processing |
| Delayed::Job | Background job processing |
| GraphQL | Query and field resolution |
| Grape | API endpoint tracing |
| MongoDB | Database operations |
| Elasticsearch | Search operations |
| ActionMailer | Email delivery |
Configuration Options
# Enable/disable specific instrumentations
config.instrument_http = true # Net::HTTP, Faraday, HTTParty
config.instrument_active_record = true # SQL queries
config.instrument_redis = true # Redis commands
config.instrument_sidekiq = true # Sidekiq jobs
config.instrument_graphql = true # GraphQL queries
config.instrument_mongodb = true # MongoDB operations
config.instrument_elasticsearch = true # Elasticsearch queries
config.instrument_action_mailer = true # Email delivery
config.instrument_delayed_job = true # Delayed::Job
config.instrument_grape = true # Grape API
# Filtering
config.http_ignore_hosts = ['localhost', '127.0.0.1']
config.redis_ignore_commands = ['ping', 'info']
config.pulse_excluded_paths = ['/health', '/ping', '/up', '/assets']
config.pulse_sample_rate = 1.0 # 1.0 = 100%Distributed Tracing
Pulse supports distributed tracing across services using W3C Trace Context and B3 propagation.
# Extracting trace context from incoming requests (automatic in Rails)
context = BrainzLab::Pulse.extract!(request.headers)
# Injecting trace context into outgoing requests (automatic with instrumentation)
BrainzLab::Pulse.inject!(headers)Custom Spans
BrainzLab::Pulse.trace("process_payment", kind: "payment") do |span|
span[:data] = { amount: 99.99, currency: "USD" }
process_payment(order)
endRails Integration
The SDK automatically integrates with Rails when loaded:
- Request context (request_id, path, method, params)
- Exception reporting to Reflex
- Performance tracing with Pulse
- User context from
current_user
Setting User Context
class ApplicationController < ActionController::Base
before_action :set_brainzlab_context
private
def set_brainzlab_context
if current_user
BrainzLab.set_user(
id: current_user.id,
email: current_user.email,
name: current_user.name
)
end
end
endSidekiq Integration
For Sidekiq, the SDK automatically:
- Traces job execution with queue wait time
- Propagates trace context between web and worker
- Captures job failures to Reflex
# config/initializers/sidekiq.rb
# Instrumentation is automatic, but you can configure:
BrainzLab.configure do |config|
config.instrument_sidekiq = true
endGrape API Integration
For Grape APIs, you can use the middleware:
class API < Grape::API
use BrainzLab::Instrumentation::GrapeInstrumentation::Middleware
# Your API endpoints...
endGraphQL Integration
For GraphQL-Ruby 2.0+, add the tracer:
class MySchema < GraphQL::Schema
trace_with BrainzLab::Instrumentation::GraphQLInstrumentation::Tracer
# Your schema...
endEnvironment Variables
| Variable | Description |
|---|---|
BRAINZLAB_SECRET_KEY |
API key for authentication |
BRAINZLAB_ENVIRONMENT |
Environment name (default: auto-detect) |
BRAINZLAB_SERVICE |
Service name |
BRAINZLAB_APP_NAME |
App name for auto-provisioning |
BRAINZLAB_DEBUG |
Enable debug logging (true/false) |
RECALL_URL |
Custom Recall endpoint |
REFLEX_URL |
Custom Reflex endpoint |
PULSE_URL |
Custom Pulse endpoint |
Scrubbing Sensitive Data
The SDK automatically scrubs sensitive fields:
config.scrub_fields = [:password, :password_confirmation, :token, :api_key, :secret]Debug Mode
Enable debug mode to see SDK activity:
config.debug = true
# Or set BRAINZLAB_DEBUG=trueSelf-Hosted
For self-hosted BrainzLab installations:
BrainzLab.configure do |config|
config.recall_url = 'https://recall.your-domain.com'
config.reflex_url = 'https://reflex.your-domain.com'
config.pulse_url = 'https://pulse.your-domain.com'
endDocumentation
Full documentation: docs.brainzlab.ai
Related
- Recall - Logging service
- Reflex - Error tracking service
- Pulse - APM service
- Stack - Self-hosted deployment
License
Ossassy License - see LICENSE for details.