Module: AllStak

Defined in:
lib/allstak.rb,
lib/allstak/client.rb,
lib/allstak/config.rb,
lib/allstak/version.rb,
lib/allstak/sampling.rb,
lib/allstak/sanitizer.rb,
lib/allstak/propagation.rb,
lib/allstak/modules/cron.rb,
lib/allstak/modules/logs.rb,
lib/allstak/global_handler.rb,
lib/allstak/modules/errors.rb,
lib/allstak/modules/tracing.rb,
lib/allstak/session_tracker.rb,
lib/allstak/modules/database.rb,
lib/allstak/integrations/rack.rb,
lib/allstak/integrations/rails.rb,
lib/allstak/models/user_context.rb,
lib/allstak/integrations/sidekiq.rb,
lib/allstak/modules/http_monitor.rb,
lib/allstak/integrations/net_http.rb,
lib/allstak/transport/event_spool.rb,
lib/allstak/transport/flush_buffer.rb,
lib/allstak/transport/http_transport.rb,
lib/allstak/integrations/active_record.rb

Overview

Official AllStak Ruby SDK.

Quick start:

require "allstak"

AllStak.configure do |c|
  c.api_key      = ENV["ALLSTAK_API_KEY"]
  c.environment  = "production"
  c.release      = "myapp@1.2.3"
  c.service_name = "myapp-api"
end

# Rails: auto-installed via Railtie — no manual wiring needed.
# Plain Rack: add the middleware yourself
use AllStak::Integrations::Rack::Middleware
# Sidekiq: server middleware auto-registered on configure.

# Manual:
AllStak.capture_exception(exc)
AllStak.log.info("hello", metadata: { foo: "bar" })
AllStak.cron.job("daily-report") { generate_report }

Defined Under Namespace

Modules: GitRelease, GlobalHandler, Integrations, Models, Modules, Propagation, Sampling, Sanitizer, Transport Classes: Client, Config, SessionTracker

Constant Summary collapse

VERSION =
"0.2.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject (readonly)

Returns the value of attribute logger.



55
56
57
# File 'lib/allstak.rb', line 55

def logger
  @logger
end

Class Method Details

.capture_error(exception_class, message, **kw) ⇒ Object



146
147
148
# File 'lib/allstak.rb', line 146

def capture_error(exception_class, message, **kw)
  @client&.capture_error(exception_class, message, **kw)
end

.capture_exception(exc, **kw) ⇒ Object



142
143
144
# File 'lib/allstak.rb', line 142

def capture_exception(exc, **kw)
  @client&.capture_exception(exc, **kw)
end

.capture_message(message, level: "info", **kw) ⇒ Object

Cross-SDK parity with JS captureMessage / Python capture_message / Java captureMessage. Emits a string as an error-group entry at the given level. Safe no-op if the SDK is not configured.



153
154
155
# File 'lib/allstak.rb', line 153

def capture_message(message, level: "info", **kw)
  @client&.capture_message(message, level: level, **kw)
end

.capture_unhandled(exc) ⇒ Object

Manually capture an exception as a global, unhandled event (mechanism=at_exit, handled=false) and flush synchronously. Use this at the top of a worker/thread/process boundary where the framework would not otherwise route the exception through the Rack middleware. Safe no-op when the SDK is not configured.



162
163
164
# File 'lib/allstak.rb', line 162

def capture_unhandled(exc)
  AllStak::GlobalHandler.capture_unhandled(exc)
end

.clear_userObject



170
171
172
# File 'lib/allstak.rb', line 170

def clear_user
  @client&.clear_user
end

.clientObject



112
113
114
# File 'lib/allstak.rb', line 112

def client
  @client or raise "AllStak not configured. Call AllStak.configure { |c| ... } first."
end

.configureObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/allstak.rb', line 57

def configure
  @mutex.synchronize do
    @config ||= Config.new
    yield @config if block_given?
    # Resolve release now that explicit user config has been applied:
    # explicit > env (set in #initialize) > local git > SDK version.
    @config.finalize_release!
    @logger = Logger.new($stderr).tap do |l|
      l.level = @config.debug ? Logger::DEBUG : Logger::WARN
      l.progname = "allstak"
    end
    if @config.valid?
      @client = Client.new(@config, @logger)
      register_runtime_release(@config, @logger)
      # Release-health: open one session per process now that config +
      # release are resolved. Idempotent + fully fail-open; self-disables
      # under a unit-test runtime and when enable_auto_session_tracking is
      # false.
      @client.start_session
      # Offline durability: replay telemetry persisted by a previous
      # process/outage through the live transport (respects retry/backoff +
      # circuit breaker). Async + fully fail-open; no-op when disabled.
      @client.drain_offline_queue
      # Auto-wire integrations that are safe to install
      AllStak::Integrations::ActiveRecordIntegration::Subscriber.install!
      AllStak::Integrations::NetHTTP.install!
      # Sidekiq + Rails self-guard: each is a graceful no-op when its host
      # framework is absent, and idempotent when present.
      AllStak::Integrations::Sidekiq.install!
      AllStak::Integrations::Rails.install!
      # Global uncaught-exception capture via at_exit. Idempotent and
      # opt-out via config.install_at_exit_handler.
      if @config.install_at_exit_handler
        # report_on_exception makes Ruby print (and lets us reason about)
        # exceptions that kill non-main threads; keep it on so background
        # thread crashes are at least visible. Best-effort.
        begin
          Thread.report_on_exception = true if Thread.respond_to?(:report_on_exception=)
        rescue StandardError
          # never let observability config break startup
        end
        AllStak::GlobalHandler.install!(@logger)
      end
    else
      @logger.warn("[AllStak] api_key not set — SDK not started")
      @client = nil
    end
    @client
  end
end

.cronObject



206
207
208
# File 'lib/allstak.rb', line 206

def cron
  @client&.cron
end

.databaseObject



202
203
204
# File 'lib/allstak.rb', line 202

def database
  @client&.database
end

.flushObject



210
211
212
# File 'lib/allstak.rb', line 210

def flush
  @client&.flush
end

.httpObject



198
199
200
# File 'lib/allstak.rb', line 198

def http
  @client&.http
end

.initialized?Boolean

Returns:

  • (Boolean)


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

def initialized?
  !@client.nil?
end

.logObject



190
191
192
# File 'lib/allstak.rb', line 190

def log
  @client&.logs
end

.register_runtime_release(config, logger) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/allstak.rb', line 116

def register_runtime_release(config, logger)
  return unless config.auto_register_release
  return if config.release.to_s.empty? || config.api_key.to_s.empty?
  return if ENV["MT_TEST"] ||
            ENV["RACK_ENV"] == "test" ||
            ENV["RAILS_ENV"] == "test" ||
            ENV["RUBYOPT"].to_s.include?("minitest") ||
            $PROGRAM_NAME.to_s.include?("rspec")

  thread = Thread.new do
    begin
      AllStak::Transport::HttpTransport.new(config, logger).post("/ingest/v1/releases", {
        version: config.release,
        environment: config.environment || "production",
        commitSha: config.commit_sha,
        branch: config.branch,
        author: "#{config.sdk_name}/#{config.sdk_version}",
        message: "Registered automatically by AllStak Ruby SDK at runtime"
      })
    rescue StandardError => e
      logger.debug("[AllStak] runtime release registration failed: #{e.class}: #{e.message}") if logger
    end
  end
  thread.abort_on_exception = false
end

.reset!Object

Test helper.



219
220
221
222
223
224
225
# File 'lib/allstak.rb', line 219

def reset!
  @mutex.synchronize do
    @client&.shutdown rescue nil
    @client = nil
    @config = nil
  end
end

.set_context(key, value) ⇒ Object

Attach a custom context entry to every future event. Cross-SDK parity with JS/Python setContext.



186
187
188
# File 'lib/allstak.rb', line 186

def set_context(key, value)
  @client&.set_context(key, value)
end

.set_tag(key, value) ⇒ Object

Attach a tag that sticks to every future event. Cross-SDK parity with JS setTag / Python set_tag.



176
177
178
# File 'lib/allstak.rb', line 176

def set_tag(key, value)
  @client&.set_tag(key, value)
end

.set_tags(pairs) ⇒ Object



180
181
182
# File 'lib/allstak.rb', line 180

def set_tags(pairs)
  @client&.set_tags(pairs)
end

.set_user(**kw) ⇒ Object



166
167
168
# File 'lib/allstak.rb', line 166

def set_user(**kw)
  @client&.set_user(**kw)
end

.shutdownObject



214
215
216
# File 'lib/allstak.rb', line 214

def shutdown
  @client&.shutdown
end

.tracingObject



194
195
196
# File 'lib/allstak.rb', line 194

def tracing
  @client&.tracing
end