Skip to content
Draft
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
430 changes: 359 additions & 71 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/capabilities/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ js-sys = "0.3"
http = "1"
bytes = "1.4"
anyhow = "1"
libdd-capabilities = { git = "https://github.com/DataDog/libdatadog.git", tag = "v43.0.0" }
libdd-capabilities = { git = "https://github.com/DataDog/libdatadog.git", rev = "5d495df91d7a3bff7f4807a36405a8efcb7c735c" }
10 changes: 5 additions & 5 deletions crates/pipeline/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ js-sys = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
libdatadog-nodejs-capabilities = { path = "../capabilities" }
libdd-capabilities = { git = "https://github.com/DataDog/libdatadog.git", tag = "v43.0.0" }
libdd-common = { git = "https://github.com/DataDog/libdatadog.git", tag = "v43.0.0", default-features = false }
libdd-data-pipeline = { git = "https://github.com/DataDog/libdatadog.git", tag = "v43.0.0", default-features = false, features = ["telemetry"] }
libdd-trace-utils = { git = "https://github.com/DataDog/libdatadog.git", tag = "v43.0.0", default-features = false, features = ["change-buffer"] }
libdd-shared-runtime = { git = "https://github.com/DataDog/libdatadog.git", tag = "v43.0.0", default-features = false }
libdd-capabilities = { git = "https://github.com/DataDog/libdatadog.git", rev = "5d495df91d7a3bff7f4807a36405a8efcb7c735c" }
libdd-common = { git = "https://github.com/DataDog/libdatadog.git", rev = "5d495df91d7a3bff7f4807a36405a8efcb7c735c", default-features = false }
libdd-data-pipeline = { git = "https://github.com/DataDog/libdatadog.git", rev = "5d495df91d7a3bff7f4807a36405a8efcb7c735c", default-features = false, features = ["telemetry"] }
libdd-trace-utils = { git = "https://github.com/DataDog/libdatadog.git", rev = "5d495df91d7a3bff7f4807a36405a8efcb7c735c", default-features = false, features = ["change-buffer"] }
libdd-shared-runtime = { git = "https://github.com/DataDog/libdatadog.git", rev = "5d495df91d7a3bff7f4807a36405a8efcb7c735c", default-features = false }
console_error_panic_hook = "0.1"

[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
4 changes: 2 additions & 2 deletions crates/remote_config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ description = "Wasm binding for libdatadog's remote config client"
crate-type = ["cdylib", "rlib"]

[dependencies]
libdd-remote-config = { git = "https://github.com/DataDog/libdatadog.git", tag = "v43.0.0", default-features = false, features = ["client", "agentless"] }
libdd-common = { git = "https://github.com/DataDog/libdatadog.git", tag = "v43.0.0", default-features = false }
libdd-remote-config = { git = "https://github.com/DataDog/libdatadog.git", rev = "5d495df91d7a3bff7f4807a36405a8efcb7c735c", default-features = false, features = ["client", "agentless"] }
libdd-common = { git = "https://github.com/DataDog/libdatadog.git", rev = "5d495df91d7a3bff7f4807a36405a8efcb7c735c", default-features = false }
libdatadog-nodejs-capabilities = { path = "../capabilities" }

wasm-bindgen = "0.2"
Expand Down
20 changes: 20 additions & 0 deletions crates/remote_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,18 @@ fn to_change_record(change: Change<Arc<RawFile<Vec<u8>>>, Vec<u8>>) -> ChangeRec
/// take it -- they would find it already borrowed and have nowhere to put the update.
#[derive(Default)]
struct PendingUpdates {
identity: Option<ClientIdentity>,
product_capabilities: Option<(Vec<RemoteConfigProduct>, Vec<RemoteConfigCapabilities>)>,
extra_services: Option<Vec<String>>,
config_states: Vec<(RemoteConfigPath, ConfigApplyState)>,
}

struct ClientIdentity {
client_id: String,
runtime_id: String,
tags: Vec<String>,
}

/// Everything needed to build the fetcher, kept around because building it is deferred.
struct FetcherConfig {
target: Target,
Expand Down Expand Up @@ -251,6 +258,9 @@ impl RemoteConfigFetcher {
};

let updates = std::mem::take(&mut *pending.borrow_mut());
if let Some(identity) = updates.identity {
fetcher.set_identity(identity.client_id, identity.runtime_id, identity.tags);
}
if let Some((products, capabilities)) = updates.product_capabilities {
fetcher.set_product_capabilities(products, capabilities);
}
Expand Down Expand Up @@ -302,6 +312,16 @@ impl RemoteConfigFetcher {
self.pending.borrow_mut().extra_services = Some(services);
}

/// Replaces the client identity reported on the next poll.
#[wasm_bindgen(js_name = "setIdentity")]
pub fn set_identity(&self, client_id: String, runtime_id: String, tags: Vec<String>) {
self.pending.borrow_mut().identity = Some(ClientIdentity {
client_id,
runtime_id,
tags,
});
}

/// Replaces the set of subscribed products and capabilities.
///
/// Names this build does not know are skipped and returned, so that a tracer whose own list has
Expand Down
1 change: 1 addition & 0 deletions packages/libdatadog/test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const remoteConfigFetcher = new RemoteConfigFetcher({
})

remoteConfigFetcher.setExtraServices([])
remoteConfigFetcher.setIdentity('client-id-2', 'runtime-id-2', [])
remoteConfigFetcher.setProductCapabilities([], [])
setStorage(runInStorage)

Expand Down
1 change: 1 addition & 0 deletions packages/libdatadog/wasm/remote-config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class RemoteConfigFetcher {
fetchChanges(): Promise<RemoteConfigChange[]>
setConfigState(path: string, applyState: number, applyError?: string): void
setExtraServices(services: string[]): void
setIdentity(clientId: string, runtimeId: string, tags: string[]): void
setProductCapabilities(products: string[], capabilities: string[]): string[]
}

Expand Down
52 changes: 49 additions & 3 deletions test/remote-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const assert = require('node:assert')
const { createHash } = require('node:crypto')
const { execFileSync } = require('node:child_process')
const { once } = require('node:events')
const { createServer } = require('node:http')
const { test } = require('node:test')

Expand Down Expand Up @@ -75,10 +76,11 @@ async function withAgent (run) {
const chunks = []
req
.on('data', chunk => chunks.push(chunk))
.on('end', () => {
.on('end', async () => {
requests.push(JSON.parse(Buffer.concat(chunks).toString('utf8')))
const response = await (responses.shift() ?? '{}')
res.writeHead(200, { 'content-type': 'application/json' })
res.end(responses.shift() ?? '{}')
res.end(Array.isArray(response) ? response[0].data : response)
})
})

Expand All @@ -89,7 +91,7 @@ async function withAgent (run) {
}))

try {
await run({ fetcher, requests, responses })
await run({ fetcher, requests, responses, server })
} finally {
await new Promise(resolve => server.close(resolve))
}
Expand All @@ -116,6 +118,50 @@ test('reports the client identity, products and capabilities', async () => {
})
})

test('updates identity without resetting remote config state', async () => {
await withAgent(async ({ fetcher, requests, responses, server }) => {
fetcher.setProductCapabilities(['ASM_FEATURES'], ['ASM_ACTIVATION'])
const responseEvents = new EventTarget()
responses.push(once(responseEvents, 'response'))

const requestStarted = once(server, 'request')
const firstPoll = fetcher.fetchChanges()
await requestStarted
try {
fetcher.setIdentity(
'intermediate-client-id',
'intermediate-runtime-id',
['runtime-id:intermediate-runtime-id'],
)
fetcher.setIdentity(
'client-id-2',
'runtime-id-2',
['runtime-id:runtime-id-2', '_dd.rc.client_id:client-id-2'],
)
} finally {
responseEvents.dispatchEvent(new MessageEvent('response', {
data: agentResponse([{ path: CONFIG_PATH, file: { asm: { enabled: true } }, version: 1 }], 1),
}))
}
const [change] = await firstPoll
fetcher.setConfigState(change.path, APPLY_STATE_ACKNOWLEDGED, '')

responses.push(agentResponse([{ path: CONFIG_PATH, file: { asm: { enabled: true } }, version: 1 }], 2))
assert.deepStrictEqual(await fetcher.fetchChanges(), [])

assert.strictEqual(requests[0].client.id, 'client-id-1')
const { client } = requests[1]
assert.strictEqual(client.id, 'client-id-2')
assert.strictEqual(client.client_tracer.runtime_id, 'runtime-id-2')
assert.deepStrictEqual(client.client_tracer.tags, [
'runtime-id:runtime-id-2',
'_dd.rc.client_id:client-id-2',
])
assert.strictEqual(Buffer.from(client.state.backend_client_state).toString(), 'backend-state-1')
assert.strictEqual(client.state.config_states[0].apply_state, APPLY_STATE_ACKNOWLEDGED)
})
})

test('diffs successive polls into add, update and remove changes', async () => {
await withAgent(async ({ fetcher, responses }) => {
fetcher.setProductCapabilities(['ASM_FEATURES'], [])
Expand Down
Loading