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.



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

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.



248
249
250
# File 'lib/logbrew/rails_integration.rb', line 248

def configuration
  @configuration
end

Instance Method Details

#clientObject



274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/logbrew/rails_integration.rb', line 274

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



288
289
290
291
292
293
294
# File 'lib/logbrew/rails_integration.rb', line 288

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

#metadataObject



319
320
321
322
323
324
325
326
327
# File 'lib/logbrew/rails_integration.rb', line 319

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



311
312
313
314
315
316
317
# File 'lib/logbrew/rails_integration.rb', line 311

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

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

#shutdownObject



296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/logbrew/rails_integration.rb', line 296

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