Class: Boxcars::MultiBackend

Inherits:
Object
  • Object
show all
Includes:
ObservabilityBackend
Defined in:
lib/boxcars/observability_backends/multi_backend.rb

Overview

An observability backend that delegates tracking calls to multiple other backends. This allows sending observability data to several destinations simultaneously.

Instance Method Summary collapse

Constructor Details

#initialize(backends) ⇒ MultiBackend

Initializes a new MultiBackend.

Parameters:

Raises:



16
17
18
19
20
21
# File 'lib/boxcars/observability_backends/multi_backend.rb', line 16

def initialize(backends)
  @backends = Array(backends).compact # Ensure it's an array and remove nils
  return if @backends.all? { |b| b.respond_to?(:track) }

  raise ArgumentError, "All backends must implement the `track` method (i.e., include Boxcars::ObservabilityBackend)."
end

Instance Method Details

#track(event:, properties:) ⇒ Object

Tracks an event by calling track on each configured backend. It passes a duplicated properties hash to each backend to prevent unintended modifications if one backend alters the hash. Errors from individual backends are silently ignored to ensure other backends still receive the event.

Parameters:

  • event (String, Symbol)

    The name of the event to track.

  • properties (Hash)

    A hash of properties associated with the event.



31
32
33
34
35
36
37
38
39
40
# File 'lib/boxcars/observability_backends/multi_backend.rb', line 31

def track(event:, properties:)
  @backends.each do |backend_instance|
    # Pass a duplicated properties hash to prevent mutation issues across backends
    backend_instance.track(event:, properties: properties.dup)
  rescue StandardError
    # Silently ignore errors from individual backends.
    # Optionally, log:
    # Boxcars.logger.warn "Boxcars::MultiBackend: Error in backend #{backend_instance.class.name}: #{e.message}"
  end
end