Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -2907,7 +2907,11 @@ Configure Feature Flags with environment variables or inside a `Datadog.configur

`DD_EXPERIMENTAL_FLAGGING_PROVIDER_ENABLED` and `c.open_feature.enabled` are deprecated. When neither stable setting is explicitly configured, the legacy value `true` selects `remote_config` and `false` disables Feature Flags.

The API key is sent only to the managed Datadog endpoint. A custom agentless URL is treated as opaque, and its query string is preserved without automatically adding `dd_env`.
Configuration polling sends the API key only to the managed Datadog endpoint. A custom agentless configuration URL is treated as opaque, and its query string is preserved without automatically adding `dd_env`.

Comment thread
leoromanovsky marked this conversation as resolved.
Resolved
In agentless mode, exposures and flag evaluations share one delivery route. A compatible local Agent or serverless relay is preferred; otherwise events use `https://event-platform-intake.<DD_SITE>` with `DD_API_KEY`. Direct delivery supports Ruby's standard HTTP proxy settings, `https_proxy` / `HTTPS_PROXY`, and `no_proxy` / `NO_PROXY`; redirects are refused. Remote Configuration continues to deliver events only through the Agent.
Comment thread
leoromanovsky marked this conversation as resolved.
Resolved

A local 404/405 or a proven failure before request transmission permits one direct replay. Other transport failures and local 403/429/5xx responses switch only future batches, avoiding duplicate delivery of possibly accepted events. Direct routing remains selected until the Feature Flags component is replaced; when neither route is available, local discovery can recover after a 30-second cooldown.
Comment thread
leoromanovsky marked this conversation as resolved.
Resolved

#### Evaluation telemetry opt-out

Expand Down
16 changes: 13 additions & 3 deletions lib/datadog/open_feature/component.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative "transport"
require_relative "transport/agentless"
require_relative "evaluation_engine"
require_relative "../core/utils/time"
require_relative "configuration/source"
Expand Down Expand Up @@ -49,14 +50,21 @@ def self.build(settings, agent_settings, resolution:, on_configuration_change:,
new(
settings,
agent_settings,
resolution: resolution,
on_configuration_change: on_configuration_change,
logger: logger,
telemetry: telemetry,
)
end

def initialize(settings, agent_settings, logger:, telemetry:, on_configuration_change: nil)
transport = Transport::HTTP.build(agent_settings: agent_settings, logger: logger)
def initialize(settings, agent_settings, logger:, telemetry:, on_configuration_change: nil,
resolution: Configuration::Source.resolve(settings))
if resolution.source == Configuration::Source::AGENTLESS
@event_transport = Transport::Agentless.new(
agent_settings: agent_settings, site: settings.site, api_key: settings.api_key, logger: logger,
)
end
transport = @event_transport || Transport::HTTP.build(agent_settings: agent_settings, logger: logger)
@worker = Exposures::Worker.new(settings: settings, transport: transport, telemetry: telemetry, logger: logger)

reporter = Exposures::Reporter.new(@worker, telemetry: telemetry, logger: logger)
Expand Down Expand Up @@ -134,8 +142,10 @@ def shutdown!
@configuration_condition.broadcast
end

@event_transport&.shutdown!(timeout: 5)
Comment thread
leoromanovsky marked this conversation as resolved.
Resolved
@worker.graceful_shutdown
@flag_eval_evp_writer&.stop
@event_transport&.close
# Symmetric teardown: drop any accumulated span-enrichment state and
# subscriptions (Ruby CLAUDE.md mandates closing resources).
@span_enrichment_hook&.shutdown
Expand All @@ -160,7 +170,7 @@ def create_flag_eval_evp_hook
return unless @settings.open_feature.evaluation_counts_enabled
return unless Hooks::FlagEvalEVPHook.available?

evp_transport = Transport::HTTP.build_flagevaluations(
evp_transport = @event_transport || Transport::HTTP.build_flagevaluations(
agent_settings: @agent_settings,
logger: @logger,
)
Expand Down
27 changes: 14 additions & 13 deletions lib/datadog/open_feature/exposures/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class Worker
include Core::Workers::Queue
include Core::Workers::Polling

GRACEFUL_SHUTDOWN_EXTRA_SECONDS = 5
GRACEFUL_SHUTDOWN_WAIT_INTERVAL_SECONDS = 0.5
GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS = 3

DEFAULT_FLUSH_INTERVAL_SECONDS = 30
DEFAULT_BUFFER_LIMIT = Buffer::DEFAULT_LIMIT
Expand Down Expand Up @@ -54,6 +53,9 @@ def stop(force_stop = false, timeout = Core::Workers::Polling::DEFAULT_SHUTDOWN_
end

def enqueue(event)
return false unless enabled?

start if forked?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Comment from Codex working with @sameerank:

[P2] Wake the restarted poller after the first child enqueue

This starts the restarted poller before the first child event is pushed. The poller can observe the new buffer while it is empty and enter its 30-second interval wait; the later buffer.push(event) does not signal that wait. A deterministic barrier around this ordering left the child event undelivered after 200 ms, and the existing shutdown path can hide the delay by waking the poller.

Please remember whether this enqueue performed the fork restart and, after its push succeeds, signal that restarted poller. Limiting the signal to this first post-fork path preserves ordinary batching behavior.

Executed validation

The baseline regression failed with 1,492 examples and one timeout. The candidate wakeup, together with the shutdown-race fix and a normal post-fork positive control, passed both OpenFeature dependency appraisals: 2,984 aggregate examples and zero failures.

buffer.push(event)
start unless running?

Expand All @@ -70,21 +72,20 @@ def perform(*args)
end

def graceful_shutdown
return false unless enabled? || !run_loop?

buffer.close
stop(true, GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS)
ensure
self.enabled = false
end

started = Core::Utils::Time.get_time
wait_time = loop_base_interval + GRACEFUL_SHUTDOWN_EXTRA_SECONDS

loop do
break if buffer.empty? && !in_iteration?
def work_pending?
!buffer.empty?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: buffer.any?

end

sleep(GRACEFUL_SHUTDOWN_WAIT_INTERVAL_SECONDS)
break if Core::Utils::Time.get_time - started > wait_time
end
protected

stop(true)
def after_fork
self.buffer = Buffer.new(@buffer_limit)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Comment from Codex working with @sameerank:

[P2] Keep teardown terminal across this fork reset

graceful_shutdown closes the buffer, but it does not set enabled = false until the blocking stop returns. If the first enqueue in a child races with teardown in that child, start if forked? can reach this after_fork, replace the closed buffer with an open one, and start another worker. A deterministic fork/thread regression returned true from that enqueue, sent one exposure after shutdown had returned, and left the replacement worker running.

Please record a terminal stop-requested state before closing or waiting, re-check it around the fork restart, and keep any replacement child buffer closed when teardown has begun. If restart creates a thread during the race, stop it after leaving the async lifecycle lock.

Executed validation

The baseline regression failed with 1,491 examples and one failure. A candidate fix with the lifecycle checks above, an ordinary post-fork positive control, and the separate first-delivery regression passed both OpenFeature dependency appraisals: 2,984 aggregate examples and zero failures.

end

private
Expand Down
8 changes: 6 additions & 2 deletions lib/datadog/open_feature/flag_evaluation/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,12 @@ def enqueue(
attrs: attrs,
observe_full_evaluation_data: observe_full_evaluation_data,
}
@queue.push(bounded_event, true)
@stop_mutex.synchronize { @stop_cond.signal }
@stop_mutex.synchronize do
return if @stopped
Comment thread
leoromanovsky marked this conversation as resolved.
Resolved

@queue.push(bounded_event, true)
@stop_cond.signal
end
start_background_thread unless running?
rescue ThreadError
# Report queue backpressure on the next flush.
Expand Down
132 changes: 132 additions & 0 deletions lib/datadog/open_feature/transport/agentless.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# frozen_string_literal: true

require_relative "../../core/encoding"
require_relative "../../core/evp"
require_relative "../../core/utils/time"
require_relative "http_client"

module Datadog
module OpenFeature
module Transport
# Component-owned route selection shared by both event writers.
Comment thread
leoromanovsky marked this conversation as resolved.
Resolved
class Agentless
DISCOVERY_TIMEOUT_SECONDS = 1
REQUEST_TIMEOUT_SECONDS = 2
Comment thread
leoromanovsky marked this conversation as resolved.
Resolved
RECOVERY_INTERVAL_SECONDS = 30
LOCAL_ROUTES = ["/evp_proxy/v4", "/evp_proxy/v2"].freeze

def initialize(agent_settings:, site:, api_key:, logger:)
@client = HTTPClient.new(agent_settings: agent_settings, site: site, api_key: api_key)
@logger = logger
@mutex = Mutex.new
@route = nil
@recover_at = 0.0
@pid = Process.pid
@closed = false
@shutdown_deadline = nil
end

def send_exposures(payload)
send_batch("/api/v2/exposures", payload)
end

def send_flag_evaluations(payload)
send_batch("/api/v2/flagevaluation", payload)
end

def shutdown!(timeout:)
@shutdown_deadline = Core::Utils::Time.get_time + timeout
end

def close
@closed = true
end

private

def send_batch(path, payload)
encoded = Core::Encoding::JSONEncoder.encode(payload)
route = select_route
return Response.new unless route

response = send_once(route, path, encoded)
if route != :direct && (response.internal_error? || [403, 404, 405, 429].include?(response.code) ||
response.code&.between?(500, 599))
fallback = leave_local(route)
if fallback && (response.pre_send? || [404, 405].include?(response.code))
response = send_once(:direct, path, encoded)
end
end
@logger.debug { "OpenFeature EVP: batch dropped after delivery failure" } unless response.ok?
response
# Serialization and delivery failures must never interrupt flag evaluation or reveal event data.
rescue
@logger.debug { "OpenFeature EVP: batch dropped after internal delivery failure" }
Response.new
end

def send_once(route, path, encoded)
direct = route == :direct
@client.request(direct ? path : route.to_s + path,
payload: encoded, direct: direct, timeout: remaining(REQUEST_TIMEOUT_SECONDS))
end

def select_route
reset_after_fork
@mutex.synchronize do
return if @closed || remaining(REQUEST_TIMEOUT_SECONDS) <= 0
return @route if @route || Core::Utils::Time.get_time < @recover_at

info = @client.request("/info", timeout: remaining(DISCOVERY_TIMEOUT_SECONDS)).info
@route = compatible_route(info)
@route = :direct if !@route && @client.direct?
@recover_at = Core::Utils::Time.get_time + RECOVERY_INTERVAL_SECONDS unless @route
Comment thread
leoromanovsky marked this conversation as resolved.
Resolved
@route
end
end

def compatible_route(info)
return unless info

headers = info["evp_proxy_allowed_headers"]
endpoints = info["endpoints"]
return unless headers.is_a?(Array) && endpoints.is_a?(Array)

headers = headers.grep(String).map { |header| header.strip.downcase }
return unless HTTPClient::IDENTITY_HEADERS.keys.all? { |name| headers.include?(name.downcase) }
Comment thread
leoromanovsky marked this conversation as resolved.
Resolved

LOCAL_ROUTES.find { |path| endpoints.include?(path) || endpoints.include?(path + "/") }
end

def leave_local(route)
@mutex.synchronize do
return false if @closed

if @route == route
@route = @client.direct? ? :direct : nil
@recover_at = Core::Utils::Time.get_time + RECOVERY_INTERVAL_SECONDS unless @route
end
@route == :direct
end
end

def remaining(limit)
return 0 if @closed

deadline = @shutdown_deadline
deadline ? [limit, deadline - Core::Utils::Time.get_time].min : limit
end

def reset_after_fork
return if @pid == Process.pid

@mutex = Mutex.new
Comment thread
leoromanovsky marked this conversation as resolved.
Resolved
@route = nil
@recover_at = 0.0
@shutdown_deadline = nil
@pid = Process.pid
end
end
end
end
end
Loading
Loading