Class: LogBrew::Rails::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/logbrew/rails_integration.rb

Overview

Owns one lazy automatic-delivery client per operating-system process.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, transport_factory: nil, client_factory: nil, timestamp_provider: nil, process_id_provider: nil, on_error: nil) ⇒ Runtime

Returns a new instance of Runtime.



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/logbrew/rails_integration.rb', line 253

def initialize(
  configuration,
  transport_factory: nil,
  client_factory: nil,
  timestamp_provider: nil,
  process_id_provider: nil,
  on_error: nil
)
  unless configuration.is_a?(Configuration)
    raise SdkError.new("configuration_error", "Rails runtime requires a Rails configuration")
  end

  @configuration = configuration
  @transport_factory = transport_factory || method(:build_transport)
  @client_factory = client_factory || method(:build_client)
  @timestamp_provider = timestamp_provider || -> { Time.now.utc }
  @process_id_provider = process_id_provider || -> { Process.pid }
  @on_error = on_error
  @mutex = Mutex.new
  @state_process_id = @process_id_provider.call
  @client = nil
  @shutdown_response = nil
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



251
252
253
# File 'lib/logbrew/rails_integration.rb', line 251

def configuration
  @configuration
end

Instance Method Details

#clientObject



277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/logbrew/rails_integration.rb', line 277

def client
  return nil unless @configuration.enabled?

  prepare_process_state
  @mutex.synchronize do
    return nil unless @shutdown_response.nil?

    @client ||= create_client
  end
rescue StandardError => error
  report_error("client_initialization", error)
  nil
end

#delivery_healthObject



291
292
293
294
295
296
297
# File 'lib/logbrew/rails_integration.rb', line 291

def delivery_health
  active_client = client
  active_client&.delivery_health
rescue StandardError => error
  report_error("delivery_health", error)
  nil
end

#metadataObject



322
323
324
325
326
327
328
329
330
# File 'lib/logbrew/rails_integration.rb', line 322

def 
  @metadata ||= {
    "service" => @configuration.service_name,
    "environment" => @configuration.app_environment,
    "framework" => "rails",
    "framework.version" => @configuration.rails_version,
    "release" => @configuration.release
  }.compact.freeze
end

#report_error(stage, error) ⇒ Object



314
315
316
317
318
319
320
# File 'lib/logbrew/rails_integration.rb', line 314

def report_error(stage, error)
  return unless @on_error.respond_to?(:call)

  @on_error.call(stage.to_s, error)
rescue StandardError
  nil
end

#shutdownObject



299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/logbrew/rails_integration.rb', line 299

def shutdown
  return nil unless @configuration.enabled?

  prepare_process_state
  @mutex.synchronize do
    return @shutdown_response unless @shutdown_response.nil?
    return nil if @client.nil?

    @shutdown_response = @client.shutdown
  end
rescue StandardError => error
  report_error("shutdown", error)
  nil
end