From ab635f1a2d1b7d928c857ce92e7daddfc530583f Mon Sep 17 00:00:00 2001 From: Rey Abolofia Date: Mon, 16 Nov 2026 22:46:53 -0800 Subject: [PATCH] feat: route feature flag config through the extension when DD_API_KEY is unset Co-Authored-By: Claude Opus 5.5 --- lib/datadog/lambda.rb | 9 +++++++++ test/datadog/lambda.spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/datadog/lambda.rb b/lib/datadog/lambda.rb index ca71f47..66e1644 100644 --- a/lib/datadog/lambda.rb +++ b/lib/datadog/lambda.rb @@ -39,6 +39,15 @@ def self.configure_apm # Needed to keep trace flushes on a single line $stdout.sync = true + # The extension forwards feature flag config requests with the API key it + # resolved (e.g. from DD_API_KEY_SECRET_ARN). Only use it when DD_API_KEY is + # unset: an extension without that route returns 404, and the tracer does + # not fall back to the default endpoint. + ff_url = 'DD_FEATURE_FLAGS_CONFIGURATION_SOURCE_AGENTLESS_BASE_URL' + if Datadog::Utils.extension_running? && ENV['DD_API_KEY'].to_s.strip.empty? && ENV[ff_url].to_s.strip.empty? + ENV[ff_url] = 'http://127.0.0.1:8126' + end + Datadog.configure do |c| unless Datadog::Utils.extension_running? c.tracing.writer = Datadog::Tracing::SyncWriter.new( diff --git a/test/datadog/lambda.spec.rb b/test/datadog/lambda.spec.rb index 7d52c21..182cf90 100644 --- a/test/datadog/lambda.spec.rb +++ b/test/datadog/lambda.spec.rb @@ -255,6 +255,29 @@ # rubocop:enable Metrics/LineLength end end + + describe '#configure_apm feature flags base URL' do + ff_url = 'DD_FEATURE_FLAGS_CONFIGURATION_SOURCE_AGENTLESS_BASE_URL' + [ + [true, {}, 'http://127.0.0.1:8126'], + [true, { 'DD_API_KEY' => '' }, 'http://127.0.0.1:8126'], + [true, { 'DD_API_KEY' => ' ' }, 'http://127.0.0.1:8126'], + [true, { 'DD_API_KEY_SECRET_ARN' => 'arn' }, 'http://127.0.0.1:8126'], + [true, { 'DD_API_KEY' => 'key' }, nil], + [true, { ff_url => 'http://custom' }, 'http://custom'], + [false, {}, nil] + ].each do |extension, env, expected| + it "extension=#{extension} env=#{env} sets #{expected.inspect}" do + stub_const('ENV', env.dup) + allow(Datadog::Utils).to receive(:extension_running?).and_return(extension) + allow(Datadog).to receive(:configure) + + Datadog::Lambda.configure_apm + + expect(ENV[ff_url]).to eq(expected) + end + end + end end # rubocop:enable Metrics/BlockLength