-
Notifications
You must be signed in to change notification settings - Fork 190
✨ Add remote configuration support for logs fields #4884
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
Open
mormubis
wants to merge
8
commits into
main
Choose a base branch
from
adlrb/remote-config-logs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b63668f
♻️ Move remote config infrastructure from browser-rum-core to browser…
mormubis f7c8f90
✨ Add remote configuration support for logs fields
mormubis 8640c84
✨ Deduplicate concurrent remote config fetches across SDK bundles
mormubis 61208b1
🐛 address Codex review feedback
mormubis 62c3b52
🐛 report remote-overridden config values in telemetry
mormubis 913742d
🐛 fix background sync display.error leaking into unit tests
mormubis f58a48f
🐛 use mockable to prevent background sync from leaking into unit tests
mormubis cf70e66
🐛 fix lint error in never-resolving Promise
mormubis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/browser-core/src/domain/remoteConfiguration/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export * from './remoteConfigurationCache' | ||
| export type { RumSdkConfig, DynamicOption, SerializedRegex, ContextItem } from './remoteConfiguration.types' | ||
| export * from './remoteConfigurationFetch' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/browser-core/src/domain/remoteConfiguration/remoteConfigurationFetch.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { interceptRequests } from '@datadog/browser-core/test' | ||
| import { fetchRemoteConfiguration } from './remoteConfigurationFetch' | ||
|
|
||
| describe('fetchRemoteConfiguration', () => { | ||
| const options = { site: 'datadoghq.com', remoteConfigurationId: 'test-id' } | ||
| let interceptor: ReturnType<typeof interceptRequests> | ||
|
|
||
| beforeEach(() => { | ||
| interceptor = interceptRequests() | ||
| }) | ||
|
|
||
| it('returns ok:true with the parsed config on success', async () => { | ||
| const config = { rum: { applicationId: 'abc', sessionSampleRate: 50 } } | ||
| interceptor.withFetch(() => | ||
| Promise.resolve({ | ||
| ok: true, | ||
| json: () => Promise.resolve(config), | ||
| }) | ||
| ) | ||
|
|
||
| const result = await fetchRemoteConfiguration(options) | ||
| expect(result).toEqual({ ok: true, value: config }) | ||
| }) | ||
|
|
||
| it('returns ok:false on HTTP error (non-ok response)', async () => { | ||
| interceptor.withFetch(() => Promise.resolve({ ok: false, status: 404 })) | ||
|
|
||
| const result = await fetchRemoteConfiguration(options) | ||
| expect(result.ok).toBeFalse() | ||
| expect((result as { ok: false; error: Error }).error).toBeInstanceOf(Error) | ||
| }) | ||
|
|
||
| it('returns ok:false on network failure (fetch throws)', async () => { | ||
| interceptor.withFetch(() => Promise.reject(new Error('Network error'))) | ||
|
|
||
| const result = await fetchRemoteConfiguration(options) | ||
| expect(result.ok).toBeFalse() | ||
| expect((result as { ok: false; error: Error }).error).toBeInstanceOf(Error) | ||
| }) | ||
|
|
||
| it('returns ok:false when response body is not valid JSON', async () => { | ||
| interceptor.withFetch(() => | ||
| Promise.resolve({ | ||
| ok: true, | ||
| json: () => Promise.reject(new SyntaxError('Unexpected end of JSON input')), | ||
| }) | ||
| ) | ||
|
|
||
| const result = await fetchRemoteConfiguration(options) | ||
| expect(result.ok).toBeFalse() | ||
| expect((result as { ok: false; error: Error }).error).toBeInstanceOf(Error) | ||
| }) | ||
|
|
||
| it('removes the window registry entry after all fetches settle', async () => { | ||
| const config = { rum: { applicationId: 'abc', sessionSampleRate: 50 } } | ||
| interceptor.withFetch(() => Promise.resolve({ ok: true, json: () => Promise.resolve(config) })) | ||
|
|
||
| await fetchRemoteConfiguration(options) | ||
|
|
||
| expect((window as unknown as Record<string, unknown>).__ddRcInflight).toBeUndefined() | ||
| }) | ||
|
|
||
| it('deduplicates concurrent calls for the same endpoint', async () => { | ||
| let fetchCount = 0 | ||
| const config = { rum: { applicationId: 'abc', sessionSampleRate: 50 } } | ||
| interceptor.withFetch(() => { | ||
| fetchCount++ | ||
| return Promise.resolve({ ok: true, json: () => Promise.resolve(config) }) | ||
| }) | ||
|
|
||
| const [result1, result2] = await Promise.all([fetchRemoteConfiguration(options), fetchRemoteConfiguration(options)]) | ||
|
|
||
| expect(fetchCount).toBe(1) | ||
| expect(result1).toEqual({ ok: true, value: config }) | ||
| expect(result2).toEqual({ ok: true, value: config }) | ||
| }) | ||
| }) |
86 changes: 86 additions & 0 deletions
86
packages/browser-core/src/domain/remoteConfiguration/remoteConfigurationFetch.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { buildEndpointUrl } from '@datadog/js-core/transport' | ||
| import { globalObject } from '@datadog/js-core/util' | ||
| import { fetch } from '../../browser/fetch' | ||
| import type { RumSdkConfig } from './remoteConfiguration.types' | ||
|
|
||
| export type RemoteConfiguration = RumSdkConfig | ||
|
|
||
| const REMOTE_CONFIGURATION_VERSION = 'v1' | ||
|
|
||
| export interface RemoteConfigurationEndpointOptions { | ||
| site?: string | undefined | ||
| remoteConfigurationId?: string | undefined | ||
| remoteConfigurationProxy?: string | undefined | ||
| remoteConfiguration?: { id?: string } | undefined | ||
| } | ||
|
|
||
| export type FetchRemoteConfigurationResult = { ok: true; value: RemoteConfiguration } | { ok: false; error: Error } | ||
|
|
||
| // Typed interface for the global inflight fetch registry so deduplication | ||
| // works across separate SDK bundles (e.g. RUM and Logs loaded as separate CDN | ||
| // scripts on the same page) and in service-worker environments where `window` | ||
| // is not available. | ||
| interface GlobalWithInflightFetches { | ||
| __ddRcInflight?: Map<string, Promise<FetchRemoteConfigurationResult>> | ||
| } | ||
|
|
||
| function getInflightFetches(): Map<string, Promise<FetchRemoteConfigurationResult>> { | ||
| const global = globalObject as GlobalWithInflightFetches | ||
| if (!global.__ddRcInflight) { | ||
| global.__ddRcInflight = new Map() | ||
| } | ||
| return global.__ddRcInflight | ||
| } | ||
|
|
||
| export function getRemoteConfigurationId(options: RemoteConfigurationEndpointOptions): string | undefined { | ||
| return options.remoteConfiguration?.id ?? options.remoteConfigurationId | ||
| } | ||
|
|
||
| export function buildEndpoint(options: RemoteConfigurationEndpointOptions): string { | ||
| if (options.remoteConfigurationProxy) { | ||
| return options.remoteConfigurationProxy | ||
| } | ||
| const id = getRemoteConfigurationId(options)! | ||
| return buildEndpointUrl({ | ||
| site: options.site!, | ||
| path: `/${REMOTE_CONFIGURATION_VERSION}/${encodeURIComponent(id)}.json`, | ||
| subdomain: 'sdk-configuration', | ||
| }) | ||
| } | ||
|
|
||
| export function fetchRemoteConfiguration( | ||
| options: RemoteConfigurationEndpointOptions | ||
| ): Promise<FetchRemoteConfigurationResult> { | ||
| const endpoint = buildEndpoint(options) | ||
| const inflightFetches = getInflightFetches() | ||
|
|
||
| if (!inflightFetches.has(endpoint)) { | ||
| const promise = doFetchRemoteConfiguration(endpoint).finally(() => { | ||
| inflightFetches.delete(endpoint) | ||
| if (inflightFetches.size === 0) { | ||
| delete (globalObject as GlobalWithInflightFetches).__ddRcInflight | ||
|
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. 💬 suggestion: Encapsulate this in the |
||
| } | ||
| }) | ||
| inflightFetches.set(endpoint, promise) | ||
| } | ||
|
|
||
| return inflightFetches.get(endpoint)! | ||
| } | ||
|
|
||
| async function doFetchRemoteConfiguration(endpoint: string): Promise<FetchRemoteConfigurationResult> { | ||
| let response: Response | undefined | ||
| try { | ||
| response = await fetch(endpoint) | ||
| } catch { | ||
| response = undefined | ||
| } | ||
| if (!response?.ok) { | ||
| return { ok: false, error: new Error('Error fetching the remote configuration.') } | ||
| } | ||
| try { | ||
| const value: RemoteConfiguration = await response.json() | ||
| return { ok: true, value } | ||
| } catch { | ||
| return { ok: false, error: new Error('Error parsing the remote configuration.') } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generic fetch doesn't validate whether the response has a
rumorlogssection. That's intentional — each SDK wraps it with its own guard. RUM checksrum || profilingbefore proceeding; Logs just skips applying iflogsis absent. What do you think?