Repository navigation
feat(openfeature): add shared agentless EVP fallback #6574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 5 commits
9aa4fe9
946173d
ff41edc
a0182be
eb3b187
911180e
040f61a
a3cc55b
bda5c14
9c1ea91
b8c93df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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? | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 validationThe 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. |
||
| unless buffer.push(event) | ||
| @logger.debug { "Feature Flags: exposure not queued: buffer closed" } | ||
| return false | ||
|
|
@@ -74,24 +76,27 @@ def perform(*args) | |
| end | ||
|
|
||
| def graceful_shutdown | ||
| return false unless enabled? || !run_loop? | ||
|
|
||
| buffer.close | ||
| @logger.debug do | ||
| "Feature Flags: exposure worker stopping; queued=#{buffer.length}, " \ | ||
| "upload_in_progress=#{!!in_iteration?}, wait_budget=#{GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS}s" | ||
| end | ||
| stopped = stop(true, GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS) | ||
| log_shutdown_outcome(GRACEFUL_SHUTDOWN_TIMEOUT_SECONDS) | ||
| @logger.debug("Feature Flags: exposure worker stopped") unless running? | ||
| stopped | ||
| 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? | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
| end | ||
|
|
||
| sleep(GRACEFUL_SHUTDOWN_WAIT_INTERVAL_SECONDS) | ||
| break if Core::Utils::Time.get_time - started > wait_time | ||
| end | ||
| protected | ||
|
|
||
| log_shutdown_outcome(wait_time) | ||
| stopped = stop(true) | ||
| @logger.debug("Feature Flags: exposure worker stopped") unless running? | ||
| stopped | ||
| def after_fork | ||
| self.buffer = Buffer.new(@buffer_limit) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
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 validationThe 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 | ||
|
|
@@ -106,11 +111,11 @@ def send_events(events, dropped) | |
| end | ||
|
|
||
| payload = @batch_builder.payload_for(events) | ||
| @logger.debug { "Feature Flags: flushing #{events.size} exposure event(s) to the Datadog Agent" } | ||
| @logger.debug { "Feature Flags: flushing #{events.size} exposure event(s) to Datadog" } | ||
| response = @transport.send_exposures(payload) | ||
|
|
||
| if response&.ok? | ||
| @logger.debug { "Feature Flags: sent #{events.size} exposure event(s) to the Datadog Agent" } | ||
| @logger.debug { "Feature Flags: sent #{events.size} exposure event(s) to Datadog" } | ||
| else | ||
| @logger.debug do | ||
| "Feature Flags: exposure upload response was not OK; dropping #{events.size} exposure event(s) " \ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| # 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. | ||
|
leoromanovsky marked this conversation as resolved.
Resolved
|
||
| class Agentless | ||
| DISCOVERY_TIMEOUT_SECONDS = 1 | ||
| REQUEST_TIMEOUT_SECONDS = 2 | ||
|
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 | ||
| @fork_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:) | ||
| reset_after_fork | ||
| @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 | ||
|
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) } | ||
|
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 | ||
|
|
||
| @fork_mutex.synchronize do | ||
| return if @pid == Process.pid | ||
|
|
||
| @mutex = Mutex.new | ||
| @route = nil | ||
| @recover_at = 0.0 | ||
| @shutdown_deadline = nil | ||
| @pid = Process.pid | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.