Skip to content
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

✨[RUM-2158] Allow more flexible proxy URL #2502

Merged
merged 2 commits into from
Nov 17, 2023
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
9 changes: 8 additions & 1 deletion packages/core/src/domain/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface InitConfiguration {
storeContextsAcrossPages?: boolean | undefined

// transport options
proxy?: string | undefined
proxy?: string | ProxyFn | undefined
site?: string | undefined

// tag and context options
Expand All @@ -49,6 +49,7 @@ export interface InitConfiguration {
enableExperimentalFeatures?: string[] | undefined
replica?: ReplicaUserConfiguration | undefined
datacenter?: string
// TODO next major: remove this option and replace usages by proxyFn
internalAnalyticsSubdomain?: string

telemetryConfigurationSampleRate?: number
Expand All @@ -58,6 +59,12 @@ export interface InitConfiguration {
// for this option.
type GenericBeforeSendCallback = (event: any, context?: any) => unknown

/**
* path: /api/vX/product
* parameters: xxx=yyy&zzz=aaa
*/
type ProxyFn = (options: { path: string; parameters: string }) => string

interface ReplicaUserConfiguration {
applicationId?: string
clientToken: string
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/domain/configuration/endpointBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ describe('endpointBuilder', () => {
)
).toBeTrue()
})

it('should allow to fully control the proxy url', () => {
const proxyFn = (options: { path: string; parameters: string }) =>
`https://proxy.io/prefix${options.path}/suffix?${options.parameters}`
expect(
createEndpointBuilder({ ...initConfiguration, proxy: proxyFn }, 'rum', []).build('xhr', DEFAULT_PAYLOAD)
).toMatch(
`https://proxy.io/prefix/api/v2/rum/suffix\\?ddsource=(.*)&ddtags=(.*)&dd-api-key=${clientToken}&dd-evp-origin-version=(.*)&dd-evp-origin=browser&dd-request-id=(.*)&batch_time=(.*)`
)
})
})

describe('tags', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/domain/configuration/endpointBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ function createEndpointUrlWithParametersBuilder(
): (parameters: string) => string {
const path = `/api/v2/${trackType}`
const proxy = initConfiguration.proxy
if (proxy) {
if (typeof proxy === 'string') {
const normalizedProxyUrl = normalizeUrl(proxy)
return (parameters) => `${normalizedProxyUrl}?ddforward=${encodeURIComponent(`${path}?${parameters}`)}`
}
if (typeof proxy === 'function') {
return (parameters) => proxy({ path, parameters })
}
const host = buildEndpointHost(initConfiguration)
return (parameters) => `https://${host}${path}?${parameters}`
}
Expand Down