Class: Datadog::OpenFeature::Agentless::ConfigurationSource

Inherits:
Object
  • Object
show all
Includes:
Core::Workers::Polling
Defined in:
lib/datadog/open_feature/agentless/configuration_source.rb

Overview

Polls the agentless endpoint and applies accepted configuration.

Constant Summary collapse

MAX_ATTEMPTS =
3
FIRST_RETRY_MIN_SECONDS =
2.0
FIRST_RETRY_MAX_SECONDS =
10.0
SECOND_RETRY_MIN_SECONDS =
5.0
SECOND_RETRY_MAX_SECONDS =
30.0
RETRY_JITTER =
0.2

Constants included from Core::Workers::Polling

Core::Workers::Polling::DEFAULT_SHUTDOWN_TIMEOUT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Core::Workers::Polling

#enabled=, #enabled?, included

Constructor Details

#initialize(endpoint:, api_key:, poll_interval_seconds:, request_timeout_seconds:, apply:, logger:, transport: nil, random: nil, retry_wait: nil) ⇒ ConfigurationSource

Returns a new instance of ConfigurationSource.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/datadog/open_feature/agentless/configuration_source.rb', line 41

def initialize(
  endpoint:,
  api_key:,
  poll_interval_seconds:,
  request_timeout_seconds:,
  apply:,
  logger:,
  transport: nil,
  random: nil,
  retry_wait: nil
)
  @transport = transport || Transport.new(
    endpoint: endpoint,
    api_key: api_key,
    timeout_seconds: request_timeout_seconds,
  )
  @apply = apply
  @logger = logger
  @poll_interval_seconds = poll_interval_seconds
  @random = random || -> { Kernel.rand }
  @retry_wait = retry_wait || ->(delay) { wait_for_retry(delay) }
  @etag = nil
  @warned = {}
  @failed_polls = 0
  @lifecycle_mutex = Mutex.new
  @started = false
  @stopped = false

  self.enabled = true
  self.fork_policy = Core::Workers::Async::Thread::FORK_POLICY_RESTART
  self.loop_base_interval = poll_interval_seconds
end

Instance Attribute Details

#etag ⇒ Object (readonly)

Returns the value of attribute etag.



22
23
24
# File 'lib/datadog/open_feature/agentless/configuration_source.rb', line 22

def etag
  @etag
end

Class Method Details

.build(settings, endpoint:, apply:, logger: Datadog.logger) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/datadog/open_feature/agentless/configuration_source.rb', line 24

def self.build(settings, endpoint:, apply:, logger: Datadog.logger)
  api_key = settings.api_key.to_s.strip
  if endpoint.managed? && api_key.empty?
    logger.warn("Feature Flags agentless delivery requires DD_API_KEY; agentless delivery is disabled")
    return
  end

  new(
    endpoint: endpoint,
    api_key: api_key.empty? ? nil : api_key,
    poll_interval_seconds: settings.feature_flags.agentless.poll_interval_seconds,
    request_timeout_seconds: settings.feature_flags.agentless.request_timeout_seconds,
    apply: apply,
    logger: logger,
  )
end

Instance Method Details

#perform ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/datadog/open_feature/agentless/configuration_source.rb', line 99

def perform
  poll
# An exception escaping here ends the unsupervised worker thread and freezes configuration.
rescue => error
  warn_once(:unexpected) do
    @logger.error("Feature Flags agentless poll failed with #{error.class}; polling continues")
  end
end

#poll ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/datadog/open_feature/agentless/configuration_source.rb', line 108

def poll
  attempt = 1

  while attempt <= MAX_ATTEMPTS
    return if stopped?

    response = @transport.get(@etag)
    log_poll_attempt(response, attempt)
    unless retryable?(response)
      apply_response(response)
      return
    end

    if attempt == MAX_ATTEMPTS
      warn_failure(response, attempt)
      return
    end

    delay = retry_delay(attempt)
    @logger.debug { "Feature Flags: retrying agentless configuration poll in #{delay.round(1)}s" }
    return if wait_before_retry(delay)

    attempt += 1
  end
end

#start ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/datadog/open_feature/agentless/configuration_source.rb', line 74

def start
  @lifecycle_mutex.synchronize do
    return false if @stopped
    return true if @started && !forked?

    perform
    @started = true
  end

  true
end

#stop(force_stop = true, timeout = Core::Workers::Polling::DEFAULT_SHUTDOWN_TIMEOUT) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/datadog/open_feature/agentless/configuration_source.rb', line 86

def stop(force_stop = true, timeout = Core::Workers::Polling::DEFAULT_SHUTDOWN_TIMEOUT)
  @lifecycle_mutex.synchronize do
    return false if @stopped

    @stopped = true
    self.enabled = false
  end

  super
  @logger.debug { "Feature Flags: stopped agentless configuration polling" }
  true
end