Class: Datadog::OpenFeature::Activation

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

Overview

Owns eager Remote Configuration and lazy agentless delivery activation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings, agent_settings, remote, logger:, telemetry:) ⇒ Activation

Returns a new instance of Activation.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/datadog/open_feature/activation.rb', line 15

def initialize(settings, agent_settings, remote, logger:, telemetry:)
  @settings = settings
  @agent_settings = agent_settings
  @remote = remote
  @logger = logger
  @telemetry = telemetry
  @component = nil
  @configuration_source = nil
  @delivery_source = nil
  @remote_receivers = nil
  @failure = nil
  @providers = {}.compare_by_identity
  @activated = false
  @delivery_started = false
  @shutdown = false
  @mutex = Mutex.new
end

Instance Attribute Details

#component ⇒ Object (readonly)

Returns the value of attribute component.



13
14
15
# File 'lib/datadog/open_feature/activation.rb', line 13

def component
  @component
end

#failure ⇒ Object (readonly)

Returns the value of attribute failure.



13
14
15
# File 'lib/datadog/open_feature/activation.rb', line 13

def failure
  @failure
end

Instance Method Details

#activate(provider) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/datadog/open_feature/activation.rb', line 33

def activate(provider)
  @mutex.synchronize do
    return if @shutdown
    return unless open_feature_available?

    @providers[provider] = true
    return @component if @activated && @delivery_started
    return if @activated

    resolution = Configuration::Source.resolve(@settings, logger: @logger)
    activate_delivery(resolution)
  end
end

#activated? ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/datadog/open_feature/activation.rb', line 67

def activated?
  @mutex.synchronize { @activated }
end

#after_fork ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/datadog/open_feature/activation.rb', line 75

def after_fork
  inherited_providers, component = @mutex.synchronize do
    @shutdown ? [[], nil] : [@providers.keys, @component]
  end
  inherited_providers.each { |provider| provider.send(:after_fork, component) }

  configuration_source = @mutex.synchronize do
    if @shutdown || !@delivery_started
      nil
    else
      (@delivery_source == Configuration::Source::REMOTE_CONFIG) ? @remote : @configuration_source
    end
  end

  configuration_source&.start
  nil
rescue => e
  # Feature Flags is optional and must never interrupt other post-fork handlers.
  report_failure(e, "Feature Flags delivery failed to restart after fork")
end

#deactivate(provider) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/datadog/open_feature/activation.rb', line 96

def deactivate(provider)
  configuration_source, component = @mutex.synchronize do
    return if @shutdown
    return unless @providers.delete(provider)
    return unless @providers.empty?
    if @delivery_source == Configuration::Source::REMOTE_CONFIG && @delivery_started
      # Remote Configuration is process-scoped and may already hold configuration needed by the next provider.
      @logger.debug { "Feature Flags: provider detached; process-scoped Remote Configuration remains active" }
      [nil, nil]
    else
      previous_source = @configuration_source
      previous_component = @component
      @component = nil
      @configuration_source = nil
      @delivery_source = nil
      @activated = false
      @delivery_started = false
      @failure = nil
      [previous_source, previous_component]
    end
  end

  configuration_source&.stop
  component&.shutdown!
end

#providers ⇒ Object



71
72
73
# File 'lib/datadog/open_feature/activation.rb', line 71

def providers
  @mutex.synchronize { @providers.keys }
end

#shutdown! ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/datadog/open_feature/activation.rb', line 122

def shutdown!
  configuration_source, component = @mutex.synchronize do
    return if @shutdown

    @shutdown = true
    [@configuration_source, @component]
  end

  configuration_source&.stop
  configuration_received = @mutex.synchronize do
    !@providers.empty? && !!component&.configuration_received?
  end
  configuration_changed(Component::CONFIGURATION_LOST) if configuration_received
  component&.shutdown!
end

#start! ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/datadog/open_feature/activation.rb', line 55

def start!
  @mutex.synchronize do
    return if @shutdown || @activated
    return unless open_feature_available?

    resolution = Configuration::Source.resolve(@settings, logger: @logger)
    return unless resolution.enabled? && resolution.source == Configuration::Source::REMOTE_CONFIG

    activate_delivery(resolution)
  end
end

#startup! ⇒ Object



47
48
49
50
51
52
53
# File 'lib/datadog/open_feature/activation.rb', line 47

def startup!
  start!
  OpenFeature.reattach(self)
rescue => e
  # Feature Flags is optional and must never interrupt library startup.
  report_failure(e, "Feature Flags delivery failed to start")
end