Class: Datadog::OpenFeature::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/open_feature/component.rb

Overview

This class is the entry point for the OpenFeature component

Constant Summary collapse

CONFIGURATION_READY =
:ready
CONFIGURATION_TIMEOUT =
:timeout
CONFIGURATION_SHUTDOWN =
:shutdown
CONFIGURATION_CHANGED =
:changed
CONFIGURATION_LOST =
:lost

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings, agent_settings, logger:, telemetry:, on_configuration_change: nil) ⇒ Component

Returns a new instance of Component.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/datadog/open_feature/component.rb', line 60

def initialize(settings, agent_settings, logger:, telemetry:, on_configuration_change: nil)
  transport = Transport::HTTP.build(agent_settings: agent_settings, logger: logger)
  @worker = Exposures::Worker.new(settings: settings, transport: transport, telemetry: telemetry, logger: logger)

  @engine = EvaluationEngine.new(telemetry: telemetry, logger: logger)

  @telemetry = telemetry
  @logger = logger
  @settings = settings
  @agent_settings = agent_settings
  @on_configuration_change = on_configuration_change
  @exposure_logging_hook_mutex = Mutex.new
  @exposure_logging_hook = create_exposure_logging_hook
  @flag_eval_metrics_hook = create_flag_eval_metrics_hook
  @flag_eval_evp_hook = create_flag_eval_evp_hook
  @span_enrichment_hook = create_span_enrichment_hook
  @debug_logging_hook = nil

  @configuration_mutex = Mutex.new
  @reconfiguration_mutex = Mutex.new
  @configuration_condition = ConditionVariable.new
  @configuration_received = false
  @configuration_shutdown = false

  @engine.warn_raw_data_logging
end

Instance Attribute Details

#engine ⇒ Object (readonly)

Returns the value of attribute engine.



29
30
31
# File 'lib/datadog/open_feature/component.rb', line 29

def engine
  @engine
end

#flag_eval_evp_hook ⇒ Object (readonly)

Returns the value of attribute flag_eval_evp_hook.



29
30
31
# File 'lib/datadog/open_feature/component.rb', line 29

def flag_eval_evp_hook
  @flag_eval_evp_hook
end

#flag_eval_metrics_hook ⇒ Object (readonly)

Returns the value of attribute flag_eval_metrics_hook.



29
30
31
# File 'lib/datadog/open_feature/component.rb', line 29

def flag_eval_metrics_hook
  @flag_eval_metrics_hook
end

#span_enrichment_hook ⇒ Object (readonly)

Returns the value of attribute span_enrichment_hook.



29
30
31
# File 'lib/datadog/open_feature/component.rb', line 29

def span_enrichment_hook
  @span_enrichment_hook
end

Class Method Details

.build(settings, agent_settings, resolution:, on_configuration_change:, logger:, telemetry:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/datadog/open_feature/component.rb', line 31

def self.build(settings, agent_settings, resolution:, on_configuration_change:, logger:, telemetry:)
  return unless resolution.enabled?

  unless ["ruby", "truffleruby"].include?(RUBY_ENGINE)
    message = "OpenFeature could not be enabled as MRI is required, " \
      "but running on #{RUBY_ENGINE.inspect}"

    logger.warn(message)
    return
  end

  if (libdatadog_api_failure = Core::LIBDATADOG_API_FAILURE)
    message = "OpenFeature could not be enabled as `libdatadog` is not loaded: " \
      "#{libdatadog_api_failure.inspect}. For help solving this issue, " \
      "please contact Datadog support at https://docs.datadoghq.com/help/."

    logger.warn(message)
    return
  end

  new(
    settings,
    agent_settings,
    on_configuration_change: on_configuration_change,
    logger: logger,
    telemetry: telemetry,
  )
end

Instance Method Details

#configuration_received? ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/datadog/open_feature/component.rb', line 152

def configuration_received?
  @configuration_mutex.synchronize { @configuration_received }
end

#debug_logging_hook ⇒ Object

Checked per evaluation so the SDK skips hook dispatch unless debug logging is on. Built lazily because Remote Configuration can build this component before the OpenFeature SDK loads.



122
123
124
125
126
127
# File 'lib/datadog/open_feature/component.rb', line 122

def debug_logging_hook
  return unless @logger.debug?

  @engine.warn_raw_data_logging
  @debug_logging_hook ||= create_debug_logging_hook
end

#exposure_logging_hook ⇒ Object

Retried until the OpenFeature SDK loads, since Remote Configuration can build this component first. Built at most once: each hook owns an exposure cache, and two caches would send repeats.



113
114
115
116
117
118
# File 'lib/datadog/open_feature/component.rb', line 113

def exposure_logging_hook
  hook = @exposure_logging_hook
  return hook if hook

  @exposure_logging_hook_mutex.synchronize { @exposure_logging_hook ||= create_exposure_logging_hook }
end

#reconfigure!(configuration) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/datadog/open_feature/component.rb', line 87

def reconfigure!(configuration)
  event = @reconfiguration_mutex.synchronize do
    return if configuration_shutdown?

    @engine.reconfigure!(configuration)

    @configuration_mutex.synchronize do
      return if @configuration_shutdown

      previously_received = @configuration_received
      @configuration_received = !configuration.nil?
      @configuration_condition.broadcast

      if @configuration_received
        previously_received ? CONFIGURATION_CHANGED : CONFIGURATION_READY
      elsif previously_received
        CONFIGURATION_LOST
      end
    end
  end

  @on_configuration_change&.call(event) if event
end

#shutdown! ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/datadog/open_feature/component.rb', line 156

def shutdown!
  @configuration_mutex.synchronize do
    @configuration_shutdown = true
    @configuration_condition.broadcast
  end

  @worker.graceful_shutdown
  @flag_eval_evp_writer&.stop
  # Symmetric teardown: drop any accumulated span-enrichment state and
  # subscriptions (Ruby CLAUDE.md mandates closing resources).
  @span_enrichment_hook&.shutdown
end

#wait_for_configuration(&cancelled) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/datadog/open_feature/component.rb', line 129

def wait_for_configuration(&cancelled)
  timeout_ms = @settings.feature_flags.initialization_timeout_ms
  @logger.debug { "Feature Flags: waiting up to #{timeout_ms}ms for configuration" }
  deadline = Core::Utils::Time.get_time + timeout_ms / 1000.0

  @configuration_mutex.synchronize do
    loop do
      return CONFIGURATION_SHUTDOWN if @configuration_shutdown || cancelled&.call
      return CONFIGURATION_READY if @configuration_received

      remaining = deadline - Core::Utils::Time.get_time
      return CONFIGURATION_TIMEOUT unless remaining.positive?

      @configuration_condition.wait(@configuration_mutex, remaining)
    end
  end
end

#wake_configuration_waiters ⇒ Object



147
148
149
150
# File 'lib/datadog/open_feature/component.rb', line 147

def wake_configuration_waiters
  @configuration_mutex.synchronize { @configuration_condition.broadcast }
  nil
end