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.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/allstak/client.rb', line 6

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 })
  @logs     = Modules::Logs.new(@transport, config, logger)
  @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.



4
5
6
# File 'lib/allstak/client.rb', line 4

def config
  @config
end

#contextsObject (readonly)

Returns the value of attribute contexts.



4
5
6
# File 'lib/allstak/client.rb', line 4

def contexts
  @contexts
end

#cronObject (readonly)

Returns the value of attribute cron.



4
5
6
# File 'lib/allstak/client.rb', line 4

def cron
  @cron
end

#databaseObject (readonly)

Returns the value of attribute database.



4
5
6
# File 'lib/allstak/client.rb', line 4

def database
  @database
end

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/allstak/client.rb', line 4

def errors
  @errors
end

#httpObject (readonly)

Returns the value of attribute http.



4
5
6
# File 'lib/allstak/client.rb', line 4

def http
  @http
end

#loggerObject (readonly)

Returns the value of attribute logger.



4
5
6
# File 'lib/allstak/client.rb', line 4

def logger
  @logger
end

#logsObject (readonly)

Returns the value of attribute logs.



4
5
6
# File 'lib/allstak/client.rb', line 4

def logs
  @logs
end

#session_trackerObject (readonly)

Returns the value of attribute session_tracker.



4
5
6
# File 'lib/allstak/client.rb', line 4

def session_tracker
  @session_tracker
end

#tagsObject (readonly)

Returns the value of attribute tags.



4
5
6
# File 'lib/allstak/client.rb', line 4

def tags
  @tags
end

#tracingObject (readonly)

Returns the value of attribute tracing.



4
5
6
# File 'lib/allstak/client.rb', line 4

def tracing
  @tracing
end

Instance Method Details

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



73
74
75
76
77
# File 'lib/allstak/client.rb', line 73

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



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

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.



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

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

#clear_userObject



63
64
65
# File 'lib/allstak/client.rb', line 63

def clear_user
  @errors.clear_user
end

#current_session_idObject

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



29
30
31
# File 'lib/allstak/client.rb', line 29

def current_session_id
  @session_tracker&.current_session_id
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.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/allstak/client.rb', line 43

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



108
109
110
111
112
113
# File 'lib/allstak/client.rb', line 108

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`.



103
104
105
106
# File 'lib/allstak/client.rb', line 103

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`.



90
91
92
93
# File 'lib/allstak/client.rb', line 90

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

#set_tags(pairs) ⇒ Object

Bulk-set tags.



96
97
98
99
# File 'lib/allstak/client.rb', line 96

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

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



59
60
61
# File 'lib/allstak/client.rb', line 59

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

#shutdownObject



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

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.



35
36
37
# File 'lib/allstak/client.rb', line 35

def start_session
  @session_tracker&.start
end