Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@
/integration-tests/import-variants.spec.js @DataDog/lang-platform-js
/integration-tests/init/ @DataDog/lang-platform-js
/integration-tests/init.spec.js @DataDog/lang-platform-js
/integration-tests/helpers/fake-agent.js @DataDog/lang-platform-js @DataDog/feature-flagging-and-experimentation-sdk
/integration-tests/memory-leak/ @DataDog/lang-platform-js
/integration-tests/mocha-parallel-files-fixtures/ @DataDog/lang-platform-js
/integration-tests/mocha-parallel-files.spec.js @DataDog/lang-platform-js
Expand Down
15 changes: 13 additions & 2 deletions integration-tests/helpers/fake-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = class FakeAgent extends EventEmitter {
port = 0
advertiseDebuggerV2IntakeSupport = true
debuggerV2IntakeStatusCode = 202
evpProxyVersions = [2]
/** @type {Set<import('net').Socket>} */
#sockets = new Set()
/** @type {Record<string, RemoteConfigFile>} */
Expand All @@ -50,6 +51,9 @@ module.exports = class FakeAgent extends EventEmitter {
if (options.debuggerV2IntakeStatusCode !== undefined) {
this.debuggerV2IntakeStatusCode = options.debuggerV2IntakeStatusCode
}
if (options.evpProxyVersions !== undefined) {
this.evpProxyVersions = [...options.evpProxyVersions]
}
}

/**
Expand Down Expand Up @@ -376,7 +380,10 @@ function buildExpressServer (agent) {
app.use(bodyParser.json({ limit: Infinity, type: 'application/json' }))

app.get('/info', (req, res) => {
const endpoints = ['/evp_proxy/v2', '/debugger/v1/input']
const endpoints = [
...agent.evpProxyVersions.map(version => `/evp_proxy/v${version}`),
'/debugger/v1/input',
]
if (agent.advertiseDebuggerV2IntakeSupport) {
endpoints.push('/debugger/v2/input')
}
Expand Down Expand Up @@ -565,10 +572,14 @@ function buildExpressServer (agent) {
})
})

app.post('/evp_proxy/v2/api/v2/exposures', (req, res) => {
app.post([
'/evp_proxy/v2/api/v2/exposures',
'/evp_proxy/v4/api/v2/exposures',
], (req, res) => {
res.status(200).send()
agent.emit('exposures', {
headers: req.headers,
path: req.path,
payload: req.body,
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('OpenFeature Remote Config and Exposure Events Integration', () => {
let agent, proc

beforeEach(async () => {
agent = await new FakeAgent().start()
agent = await new FakeAgent(0, { evpProxyVersions: [2, 4] }).start()
proc = await spawnProc(appFile, {
cwd,
env: {
Expand All @@ -78,7 +78,7 @@ describe('OpenFeature Remote Config and Exposure Events Integration', () => {
let receivedAckUpdate = false

// Listen for exposure events
agent.on('exposures', ({ payload, headers }) => {
agent.on('exposures', ({ payload, headers, path }) => {
assert.ok(Object.hasOwn(payload, 'exposures'), `Available keys: ${inspect(Object.keys(payload))}`)
assertObjectContains(payload, {
context: {
Expand All @@ -94,6 +94,7 @@ describe('OpenFeature Remote Config and Exposure Events Integration', () => {
try {
assert.strictEqual(headers['content-type'], 'application/json')
assert.strictEqual(headers['x-datadog-evp-subdomain'], 'event-platform-intake')
assert.strictEqual(path, '/evp_proxy/v2/api/v2/exposures')

// Verify we got exposure events from flag evaluations
assert.strictEqual(exposureEvents.length, 2)
Expand Down
6 changes: 3 additions & 3 deletions packages/dd-trace/src/openfeature/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { channel } = require('dc-polyfill')
const log = require('../log')
const ExposuresWriter = require('./writers/exposures')
const { setAgentStrategy } = require('./writers/util')
const { setExposureDeliveryStrategy } = require('./writers/util')

const exposureSubmitCh = channel('ffe:exposure:submit')
const flushCh = channel('ffe:writers:flush')
Expand Down Expand Up @@ -45,10 +45,10 @@ function enable (config) {
exposureSubmitCh.subscribe(_handleExposureSubmit)
flushCh.subscribe(_handleFlush)

setAgentStrategy(config, (hasAgent, route) => {
setExposureDeliveryStrategy(config, (enabled, route) => {
if (exposuresWriter !== writer) return

writer.setEnabled(hasAgent, route)
writer.setEnabled(enabled, route)
})
}

Expand Down
101 changes: 93 additions & 8 deletions packages/dd-trace/src/openfeature/writers/util.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
'use strict'

const logger = require('../../log')
const { EVP_PROXY_PATH_V2 } = require('../../evp_proxy/constants')
const {
EVP_EVENT_PLATFORM_SUBDOMAIN,
EVP_PROXY_PATH_V2,
EVP_PROXY_PATH_V4,
EVP_SUBDOMAIN_HEADER_NAME,
} = require('../../evp_proxy/constants')
const { createDirectEVPRoute } = require('../../evp_proxy/direct')
const { discoverEVPProxy } = require('../../evp_proxy/discovery')
const logger = require('../../log')

let missingRouteWarningLogged = false

/**
* Logs the unavailable exposure-delivery warning once.
*
* @returns {void}
*/
function warnExposureDeliveryUnavailable () {
if (missingRouteWarningLogged) return
missingRouteWarningLogged = true
logger.warn(
'Feature Flags exposure delivery is disabled because no compatible local EVP route or direct intake ' +
'credentials are available.'
)
}

/**
* Determines if the agent supports EVP proxy and sets the writer enabled state accordingly
* Preserves Agent exposure delivery for the Remote Configuration source.
*
* @param {import('../../config')} config - Tracer configuration object
* @param {Function} setWriterEnabledValue - Callback to set the writer enabled state
* @returns {void}
*/
function setAgentStrategy (config, setWriterEnabledValue) {
discoverEVPProxy(config.url, {
supportedPaths: [EVP_PROXY_PATH_V2],
}, (err, route) => {
if (err) {
logger.debug('FFE Writer disabled - error getting agent info: %s', err.message)
}, (error, route) => {
if (error) {
logger.debug('FFE Writer disabled - error getting agent info: %s', error.message)
setWriterEnabledValue(false)
return
}
Expand All @@ -29,6 +53,67 @@ function setAgentStrategy (config, setWriterEnabledValue) {
})
}

module.exports = {
setAgentStrategy,
/**
* Selects a local serverless receiver or authenticated direct intake.
*
* Local discovery is optional for delivery. A missing listener, discovery
* error, or incompatible receiver selects direct intake when credentials exist.
*
* @param {import('../../config')} config - Tracer configuration object
* @param {Function} setWriterEnabledValue - Callback to set the writer enabled state
* @returns {void}
*/
function setAgentlessStrategy (config, setWriterEnabledValue) {
const directRoute = createDirectEVPRoute(config, EVP_EVENT_PLATFORM_SUBDOMAIN)

discoverEVPProxy(config.url, {
Comment thread
leoromanovsky marked this conversation as resolved.
supportedPaths: [EVP_PROXY_PATH_V4, EVP_PROXY_PATH_V2],
Comment thread
leoromanovsky marked this conversation as resolved.
Comment thread
leoromanovsky marked this conversation as resolved.
}, (error, localRoute) => {
if (localRoute) {
const route = {
...localRoute,
headers: {
[EVP_SUBDOMAIN_HEADER_NAME]: EVP_EVENT_PLATFORM_SUBDOMAIN,
},
...(directRoute && { fallback: directRoute }),
}
logger.debug('FFE Writer enabled with local EVP route %s', route.basePath)
setWriterEnabledValue(true, route)
return
}

if (directRoute) {
if (error) {
logger.debug('FFE Writer using direct EVP intake after local discovery failed: %s', error.message)
} else {
logger.debug('FFE Writer using direct EVP intake because no compatible local route was advertised')
}
setWriterEnabledValue(true, directRoute)
return
}

if (error) {
logger.debug('FFE Writer disabled - error getting local receiver info: %s', error.message)
}
warnExposureDeliveryUnavailable()
setWriterEnabledValue(false)
})
}

/**
* Applies the exposure-delivery strategy for the configured Feature Flags source.
*
* @param {import('../../config')} config - Tracer configuration object
* @param {Function} setWriterEnabledValue - Callback to set the writer enabled state
* @returns {void}
*/
function setExposureDeliveryStrategy (config, setWriterEnabledValue) {
if (config.featureFlags?.DD_FEATURE_FLAGS_CONFIGURATION_SOURCE === 'agentless') {
setAgentlessStrategy(config, setWriterEnabledValue)
return
}

setAgentStrategy(config, setWriterEnabledValue)
}

module.exports = { setExposureDeliveryStrategy }
24 changes: 12 additions & 12 deletions packages/dd-trace/test/openfeature/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('OpenFeature Module', () => {
let openfeatureModule
let mockWriter
let ExposuresWriterStub
let setAgentStrategyStub
let setExposureDeliveryStrategyStub

beforeEach(() => {
config = {
Expand All @@ -30,11 +30,11 @@ describe('OpenFeature Module', () => {
}

ExposuresWriterStub = sinon.stub().returns(mockWriter)
setAgentStrategyStub = sinon.stub()
setExposureDeliveryStrategyStub = sinon.stub()

openfeatureModule = proxyquire('../../src/openfeature', {
'./writers/exposures': ExposuresWriterStub,
'./writers/util': { setAgentStrategy: setAgentStrategyStub },
'./writers/util': { setExposureDeliveryStrategy: setExposureDeliveryStrategyStub },
})
})

Expand All @@ -52,17 +52,17 @@ describe('OpenFeature Module', () => {
openfeatureModule.enable(config)

sinon.assert.calledOnceWithExactly(ExposuresWriterStub, config)
sinon.assert.calledOnce(setAgentStrategyStub)
sinon.assert.calledOnce(setExposureDeliveryStrategyStub)
})

it('passes the discovered route to the writer', () => {
it('configures the writer with the selected exposure route', () => {
openfeatureModule.enable(config)
const setWriterEnabled = setExposureDeliveryStrategyStub.firstCall.args[1]
const route = {
url: new URL('http://localhost:8126'),
basePath: '/evp_proxy/v2',
url: new URL('http://serverless-init:8126'),
basePath: '/evp_proxy/v4',
}
setAgentStrategyStub.callsArgWith(1, true, route)

openfeatureModule.enable(config)
setWriterEnabled(true, route)

sinon.assert.calledOnceWithExactly(mockWriter.setEnabled, true, route)
})
Expand All @@ -85,10 +85,10 @@ describe('OpenFeature Module', () => {
ExposuresWriterStub.onSecondCall().returns(replacementWriter)

openfeatureModule.enable(config)
const staleCallback = setAgentStrategyStub.firstCall.args[1]
const staleCallback = setExposureDeliveryStrategyStub.firstCall.args[1]
openfeatureModule.disable()
openfeatureModule.enable(config)
const currentCallback = setAgentStrategyStub.secondCall.args[1]
const currentCallback = setExposureDeliveryStrategyStub.secondCall.args[1]

staleCallback(true, staleRoute)
sinon.assert.notCalled(replacementWriter.setEnabled)
Expand Down
Loading
Loading