Class: AllStak::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/allstak/client.rb

Overview

The AllStak SDK client. Create once via configure.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, logger) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/allstak/client.rb', line 8

def initialize(config, logger)
  @config = config
  @logger = logger
  @transport = Transport::HttpTransport.new(config, logger)

  # Release-health session tracker: one session per process. Started by
  # AllStak.configure after the release is resolved; ended in #shutdown.
  @session_tracker = SessionTracker.new(config, @transport, logger)

  @errors   = Modules::Errors.new(@transport, config, logger,
                                  session_id_provider: -> { @session_tracker&.current_session_id })
  # Bridge AllStak.log.* into auto breadcrumbs (gated by the errors module
  # on config.enable_auto_breadcrumbs). `auto: true` so it respects the
  # toggle and never duplicates a manually-added breadcrumb.
  errors = @errors
  @logs     = Modules::Logs.new(@transport, config, logger,
                                breadcrumb_sink: lambda do |**kw|
                                  errors.add_breadcrumb(**kw, auto: true)
                                end)
  @http     = Modules::HttpMonitor.new(@transport, config, logger)
  @tracing  = Modules::Tracing.new(@transport, config, logger)
  @database = Modules::Database.new(@transport, config, logger)
  @cron     = Modules::Cron.new(@transport, logger, config)
  @tags     = {}
  @contexts = {}

  at_exit { shutdown rescue nil }
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/allstak/client.rb', line 6

def config
  @config
end

#contextsObject (readonly)

Returns the value of attribute contexts.



6
7
8
# File 'lib/allstak/client.rb', line 6

def contexts
  @contexts
end

#cronObject (readonly)

Returns the value of attribute cron.



6
7
8
# File 'lib/allstak/client.rb', line 6

def cron
  @cron
end

#databaseObject (readonly)

Returns the value of attribute database.



6
7
8
# File 'lib/allstak/client.rb', line 6

def database
  @database
end

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/allstak/client.rb', line 6

def errors
  @errors
end

#httpObject (readonly)

Returns the value of attribute http.



6
7
8
# File 'lib/allstak/client.rb', line 6

def http
  @http
end

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/allstak/client.rb', line 6

def logger
  @logger
end

#logsObject (readonly)

Returns the value of attribute logs.



6
7
8
# File 'lib/allstak/client.rb', line 6

def logs
  @logs
end

#session_trackerObject (readonly)

Returns the value of attribute session_tracker.



6
7
8
# File 'lib/allstak/client.rb', line 6

def session_tracker
  @session_tracker
end

#tagsObject (readonly)

Returns the value of attribute tags.



6
7
8
# File 'lib/allstak/client.rb', line 6

def tags
  @tags
end

#tracingObject (readonly)

Returns the value of attribute tracing.



6
7
8
# File 'lib/allstak/client.rb', line 6

def tracing
  @tracing
end

Instance Method Details

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



82
83
84
85
86
# File 'lib/allstak/client.rb', line 82

def capture_error(exception_class, message, **kw)
  kw = (kw)
  mark_session_for(kw)
  @errors.capture_error(exception_class, message, **kw)
end

#capture_exception(exc, **kw) ⇒ Object



76
77
78
79
80
# File 'lib/allstak/client.rb', line 76

def capture_exception(exc, **kw)
  kw = (kw)
  mark_session_for(kw)
  @errors.capture_exception(exc, **kw)
end

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

Capture a standalone string as an “error group” at the given level. Cross-SDK parity with JS/Python/PHP/Java ‘captureMessage`. Implemented on top of capture_error so the dashboard surfaces it as an “info”/“warning”/“error” level entry in the Errors list.



92
93
94
95
# File 'lib/allstak/client.rb', line 92

def capture_message(message, level: "info", **kw)
  kw = (kw)
  @errors.capture_error("Message", message.to_s, level: level.to_s, **kw)
end

#clear_userObject



72
73
74
# File 'lib/allstak/client.rb', line 72

def clear_user
  @errors.clear_user
end

#current_session_idObject

The active release-health session id (nil before start / after shutdown).



38
39
40
# File 'lib/allstak/client.rb', line 38

def current_session_id
  @session_tracker&.current_session_id
end

#diagnosticsObject

Privacy-safe SDK diagnostics. Contains counters and queue sizes only.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/allstak/client.rb', line 136

def diagnostics
  transport_stats =
    if @transport.respond_to?(:diagnostics)
      @transport.diagnostics
    else
      {}
    end
  modules = [@errors, @logs, @http, @tracing, @database]
  buffer_queue = modules.sum { |m| safe_count(m, :buffer_count) }
  buffer_drops = modules.sum { |m| safe_count(m, :dropped_count) }

  Diagnostics.new(
    events_captured: modules.sum { |m| safe_count(m, :captured_count) },
    events_sent: transport_stats.fetch(:sent, 0),
    events_failed: transport_stats.fetch(:failed, 0),
    events_dropped: transport_stats.fetch(:dropped, 0) + buffer_drops,
    events_persisted: transport_stats.fetch(:persisted, 0),
    events_replayed: transport_stats.fetch(:replayed, 0),
    queue_size: transport_stats.fetch(:queue_size, 0) + buffer_queue,
    retry_attempts: transport_stats.fetch(:retry_attempts, 0),
    rate_limited_count: transport_stats.fetch(:rate_limited, 0),
    compressed_payloads: transport_stats.fetch(:compressed, 0),
    uncompressed_payloads: transport_stats.fetch(:uncompressed, 0),
    compression_bytes_saved: transport_stats.fetch(:compression_bytes_saved, 0),
    sanitizer_redaction_count: AllStak::Sanitizer.redaction_count,
    active_trace_count: safe_count(@tracing, :active_trace_count),
    active_span_count: safe_count(@tracing, :active_span_count),
    breadcrumb_count: safe_count(@errors, :breadcrumb_count),
    session_recovery_count: safe_count(@session_tracker, :recovery_count),
    disabled: @transport.disabled?
  )
end

#drain_offline_queueObject

Replay any telemetry persisted by a previous process/outage. Runs on a daemon thread so init never blocks on the network; fully fail-open. Called by AllStak.configure after the client is built. No-op when the offline queue is disabled/unavailable.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/allstak/client.rb', line 52

def drain_offline_queue
  transport = @transport
  return unless transport.respond_to?(:drain_spool)
  thread = Thread.new do
    begin
      transport.drain_spool
    rescue StandardError => e
      @logger&.debug("[AllStak] offline drain failed: #{e.class}: #{e.message}")
    end
  end
  thread.abort_on_exception = false
  self
rescue StandardError
  self
end

#flushObject



117
118
119
120
121
122
# File 'lib/allstak/client.rb', line 117

def flush
  @logs.flush
  @http.flush
  @tracing.flush
  @database.flush
end

#set_context(key, value) ⇒ Object

Attach a key/value context entry (goes into metadata on every event). Cross-SDK parity with JS/Python ‘setContext`.



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

def set_context(key, value)
  @contexts[key.to_s] = value
  self
end

#set_tag(key, value) ⇒ Object

Attach a key/value tag to every subsequent event sent by the SDK. Cross-SDK parity with JS ‘setTag` and Python `set_tag`.



99
100
101
102
# File 'lib/allstak/client.rb', line 99

def set_tag(key, value)
  @tags[key.to_s] = value.to_s
  self
end

#set_tags(pairs) ⇒ Object

Bulk-set tags.



105
106
107
108
# File 'lib/allstak/client.rb', line 105

def set_tags(pairs)
  pairs.each { |k, v| set_tag(k, v) }
  self
end

#set_user(id: nil, email: nil, ip: nil) ⇒ Object



68
69
70
# File 'lib/allstak/client.rb', line 68

def set_user(id: nil, email: nil, ip: nil)
  @errors.set_user(id: id, email: email, ip: ip)
end

#shutdownObject



124
125
126
127
128
129
130
131
132
133
# File 'lib/allstak/client.rb', line 124

def shutdown
  flush
  @logs.shutdown
  @http.shutdown
  @tracing.shutdown
  @database.shutdown
  # Graceful shutdown: end the release-health session last so its
  # /sessions/end carries the final accumulated status. Best-effort.
  @session_tracker&.end rescue nil
end

#start_sessionObject

Begin the release-health session. Idempotent + fail-open. Called by AllStak.configure once the release has been finalized.



44
45
46
# File 'lib/allstak/client.rb', line 44

def start_session
  @session_tracker&.start
end