Class: Salopulse::Client

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/salopulse/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



19
20
21
22
# File 'lib/salopulse/client.rb', line 19

def initialize
  @initialized = false
  @mutex = Mutex.new
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



17
18
19
# File 'lib/salopulse/client.rb', line 17

def buffer
  @buffer
end

#configurationObject (readonly)

Returns the value of attribute configuration.



17
18
19
# File 'lib/salopulse/client.rb', line 17

def configuration
  @configuration
end

#dsnObject (readonly)

Returns the value of attribute dsn.



17
18
19
# File 'lib/salopulse/client.rb', line 17

def dsn
  @dsn
end

#flusherObject (readonly)

Returns the value of attribute flusher.



17
18
19
# File 'lib/salopulse/client.rb', line 17

def flusher
  @flusher
end

#transportObject (readonly)

Returns the value of attribute transport.



17
18
19
# File 'lib/salopulse/client.rb', line 17

def transport
  @transport
end

Instance Method Details

#capture_exception(error, user_context: nil, environment_data: nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/salopulse/client.rb', line 99

def capture_exception(error, user_context: nil, environment_data: nil)
  return if disabled?
  return unless sample?

  ctx = RequestContext.current
  data = {
    "error_class" => error.class.name,
    "message" => error.message.to_s,
    "stack_trace" => Array(error.backtrace).join("\n"),
    "endpoint" => ctx&.dig(:endpoint),
    "http_method" => ctx&.dig(:http_method)
  }
  user = user_context || ctx&.dig(:user)
  data["user_context"] = Sanitizer.scrub_hash(user) if user
  data["environment_data"] = Sanitizer.scrub_hash(environment_data) if environment_data

  enqueue(build_event(type: "error", data: data.compact, ctx: ctx))
end

#capture_message(message, level: :info) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/salopulse/client.rb', line 118

def capture_message(message, level: :info)
  return if disabled?
  return unless sample?

  ctx = RequestContext.current
  data = {
    "error_class" => "Salopulse::Message",
    "message" => message.to_s,
    "stack_trace" => "",
    "endpoint" => ctx&.dig(:endpoint),
    "http_method" => ctx&.dig(:http_method),
    "level" => level.to_s
  }.compact
  enqueue(build_event(type: "error", data: data, ctx: ctx))
end

#capture_performance(endpoint:, http_method:, duration_ms:, status_code:, cpu_usage: nil, memory_usage: nil) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/salopulse/client.rb', line 134

def capture_performance(endpoint:, http_method:, duration_ms:, status_code:, cpu_usage: nil, memory_usage: nil)
  return if disabled?
  return unless sample?

  ctx = RequestContext.current
  data = {
    "endpoint" => endpoint,
    "http_method" => http_method,
    "duration_ms" => duration_ms.to_i,
    "status_code" => status_code.to_i
  }
  data["cpu_usage"] = cpu_usage if cpu_usage
  data["memory_usage"] = memory_usage if memory_usage

  enqueue(build_event(type: "performance", data: data, ctx: ctx))
end

#capture_sql(query:, duration_ms:, rows_returned: nil) ⇒ Object

— Capture API ———————————————————



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
# File 'lib/salopulse/client.rb', line 70

def capture_sql(query:, duration_ms:, rows_returned: nil)
  return if disabled?
  return if RequestContext.suppressed?
  return unless sample?

  ctx = RequestContext.current
  fingerprint = LocalFingerprint.for(query)

  event = build_event(
    type: "sql",
    data: {
      "query" => query,
      "duration_ms" => duration_ms.to_i,
      "endpoint" => ctx&.dig(:endpoint),
      "http_method" => ctx&.dig(:http_method),
      "rows_returned" => rows_returned,
      "n1_detected" => false
    }.compact,
    ctx: ctx,
    extra_envelope: { "database_dialect" => detect_dialect }
  )

  if ctx
    RequestContext.record_sql_event(event, fingerprint)
  else
    enqueue(event)
  end
end

#closeObject



185
186
187
188
189
# File 'lib/salopulse/client.rb', line 185

def close
  return unless @initialized
  @flusher&.stop(timeout: 5)
  @initialized = false
end

#disabled?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/salopulse/client.rb', line 64

def disabled?
  !@initialized || !@configuration&.enabled
end

#flush(timeout: 5) ⇒ Object

— Lifecycle ———————————————————–



180
181
182
183
# File 'lib/salopulse/client.rb', line 180

def flush(timeout: 5)
  return 0 if disabled?
  @flusher.flush_all(timeout: timeout)
end

#flush_request_scope_eventsObject

— Request scope ——————————————————-



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/salopulse/client.rb', line 153

def flush_request_scope_events
  ctx = RequestContext.current
  return unless ctx

  threshold = @configuration.n1_threshold
  counts = ctx[:sql_fingerprint_counts]

  ctx[:sql_events].each do |event, fingerprint|
    if counts[fingerprint] >= threshold
      event[:data]["n1_detected"] = true
    end
    enqueue(event)
  end
end

#init(options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/salopulse/client.rb', line 24

def init(options = {})
  @mutex.synchronize do
    return self if @initialized

    @configuration = Configuration.new
    options.each { |k, v| @configuration.public_send("#{k}=", v) if @configuration.respond_to?("#{k}=") }

    return self unless @configuration.enabled

    @dsn = DSN.new(@configuration.dsn)
    @buffer = Buffer.new(max_size: @configuration.max_buffer_size)
    @transport = Transport.new(
      dsn: @dsn,
      sdk_version: Salopulse::VERSION,
      logger: @configuration.logger
    )
    @flusher = Flusher.new(
      buffer: @buffer,
      transport: @transport,
      interval: @configuration.flush_interval,
      batch_size: @configuration.flush_batch_size,
      logger: @configuration.logger
    )
    @flusher.start

    install_at_exit_hook

    @initialized = true
    self
  end
end

#initialized?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/salopulse/client.rb', line 56

def initialized?
  @initialized
end

#reset!Object

Used by tests to wipe singleton state.



192
193
194
195
196
197
198
199
200
# File 'lib/salopulse/client.rb', line 192

def reset!
  close if @initialized
  @configuration = nil
  @buffer = nil
  @transport = nil
  @flusher = nil
  @dsn = nil
  @initialized = false
end

#set_tag(key, value) ⇒ Object



174
175
176
# File 'lib/salopulse/client.rb', line 174

def set_tag(key, value)
  RequestContext.set_tag(key, value)
end

#set_user(attrs) ⇒ Object

— Context helpers —————————————————–



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

def set_user(attrs)
  RequestContext.set_user(attrs)
end

#uninitialized?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/salopulse/client.rb', line 60

def uninitialized?
  !@initialized
end