Class: Tasker::Telemetry::EventMapping

Inherits:
Tasker::Types::BaseConfig show all
Defined in:
lib/tasker/telemetry/event_mapping.rb

Overview

EventMapping represents a single event→telemetry routing configuration

This class defines how a specific event should be routed to telemetry backends. It uses dry-struct for immutability and type safety, following the established pattern from Tasker::Types configuration classes.

Examples:

Basic event mapping

mapping = EventMapping.new(
  event_name: 'task.completed',
  backends: [:trace, :metrics],
  enabled: true
)

Metrics-only mapping for operational data

mapping = EventMapping.new(
  event_name: 'observability.task.enqueue',
  backends: [:metrics],
  sampling_rate: 1.0
)

Trace-only mapping for debugging

mapping = EventMapping.new(
  event_name: 'step.before_handle',
  backends: [:trace],
  sampling_rate: 0.1  # Sample 10% for performance
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEventMapping

Override initialize to ensure backends array is frozen



47
48
49
50
51
# File 'lib/tasker/telemetry/event_mapping.rb', line 47

def initialize(*)
  super
  backends.freeze
  # Don't freeze here - BaseConfig already freezes the object
end

Instance Attribute Details

#backendsArray<Symbol> (readonly)

Returns List of backend types (:trace, :metrics, :logs).

Returns:

  • (Array<Symbol>)

    List of backend types (:trace, :metrics, :logs)



44
# File 'lib/tasker/telemetry/event_mapping.rb', line 44

attribute :backends, Tasker::Types::Array.of(Tasker::Types::Symbol).default([:trace].freeze)

#enabledBoolean (readonly)

Returns True if this mapping should be processed.

Returns:

  • (Boolean)

    True if this mapping should be processed



57
# File 'lib/tasker/telemetry/event_mapping.rb', line 57

attribute :enabled, Tasker::Types::Bool.default(true)

#event_nameString (readonly)

Returns Event name in dot notation (e.g., 'task.completed').

Returns:

  • (String)

    Event name in dot notation (e.g., 'task.completed')



38
# File 'lib/tasker/telemetry/event_mapping.rb', line 38

attribute :event_name, Tasker::Types::String

#metadataHash (readonly)

Returns Additional configuration data.

Returns:

  • (Hash)

    Additional configuration data



75
# File 'lib/tasker/telemetry/event_mapping.rb', line 75

attribute :metadata, Tasker::Types::Hash.default({}.freeze)

#prioritySymbol (readonly)

Returns Priority level (:low, :normal, :high, :critical).

Returns:

  • (Symbol)

    Priority level (:low, :normal, :high, :critical)



69
# File 'lib/tasker/telemetry/event_mapping.rb', line 69

attribute :priority, Tasker::Types::Symbol.default(:normal)

#sampling_rateFloat (readonly)

Returns Sampling rate (1.0 = 100%, 0.1 = 10%).

Returns:

  • (Float)

    Sampling rate (1.0 = 100%, 0.1 = 10%)



63
# File 'lib/tasker/telemetry/event_mapping.rb', line 63

attribute :sampling_rate, Tasker::Types::Float.default(1.0)

Instance Method Details

#active?Boolean

Check if this mapping is active (enabled and should be sampled)

Returns:

  • (Boolean)

    True if this mapping should be processed



114
115
116
# File 'lib/tasker/telemetry/event_mapping.rb', line 114

def active?
  enabled && should_sample?
end

#descriptionString

Get a description of this mapping for debugging

Returns:

  • (String)

    Human-readable description



121
122
123
# File 'lib/tasker/telemetry/event_mapping.rb', line 121

def description
  "#{event_name}#{backends.join(', ')} (#{(sampling_rate * 100).round(1)}% sampled)"
end

#routes_to_logs?Boolean

Check if this mapping routes to logs

Returns:

  • (Boolean)

    True if backends includes :logs



94
95
96
# File 'lib/tasker/telemetry/event_mapping.rb', line 94

def routes_to_logs?
  backends.include?(:logs)
end

#routes_to_metrics?Boolean

Check if this mapping routes to metrics

Returns:

  • (Boolean)

    True if backends includes :metrics



87
88
89
# File 'lib/tasker/telemetry/event_mapping.rb', line 87

def routes_to_metrics?
  backends.include?(:metrics)
end

#routes_to_traces?Boolean

Check if this mapping routes to traces

Returns:

  • (Boolean)

    True if backends includes :trace



80
81
82
# File 'lib/tasker/telemetry/event_mapping.rb', line 80

def routes_to_traces?
  backends.include?(:trace)
end

#should_sample?Boolean

Check if this event should be sampled

Uses a simple random sampling approach. For production, you might want to implement more sophisticated sampling strategies.

Returns:

  • (Boolean)

    True if this event should be processed



104
105
106
107
108
109
# File 'lib/tasker/telemetry/event_mapping.rb', line 104

def should_sample?
  return true if sampling_rate >= 1.0
  return false if sampling_rate <= 0.0

  Random.rand <= sampling_rate
end