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

fix: endless capturing /s/ #1551

Merged
merged 5 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
36 changes: 30 additions & 6 deletions src/__tests__/extensions/replay/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,34 +75,58 @@ describe('config', () => {
{
name: 'https://app.posthog.com/api/feature_flag/',
},
undefined,
],
[
{
name: 'https://app.posthog.com/s/',
name: 'https://app.posthog.com/s/?ip=1&ver=123',
},
undefined,
undefined,
],
[
{
name: 'https://app.posthog.com/e/',
name: 'https://app.posthog.com/e/?ip=1&ver=123',
},
undefined,
undefined,
],
[
{
name: 'https://app.posthog.com/i/v0/e/',
name: 'https://app.posthog.com/i/v0/e/?ip=1&ver=123',
},
undefined,
undefined,
],
[
{
// even an imaginary future world of rust session replay capture
name: 'https://app.posthog.com/i/v0/s/',
name: 'https://app.posthog.com/i/v0/s/?ip=1&ver=123',
},
undefined,
undefined,
],
])('ignores ingestion paths', (capturedRequest, expected) => {
const networkOptions = buildNetworkRequestOptions(defaultConfig(), {})
[
{
// even an imaginary future world of rust session replay capture
pauldambra marked this conversation as resolved.
Show resolved Hide resolved
name: 'https://app.posthog.com/ingest/s/?ip=1&ver=123',
},
undefined,
'/ingest',
],
[
{
// even an imaginary future world of rust session replay capture
pauldambra marked this conversation as resolved.
Show resolved Hide resolved
name: 'https://app.posthog.com/ingest/s/?ip=1&ver=123',
},
undefined,
'https://app.posthog.com/ingest',
],
])('ignores ingestion paths', (capturedRequest, expected, apiHost?: string) => {
const networkOptions = buildNetworkRequestOptions(
{ ...defaultConfig(), api_host: apiHost || 'https://us.posthog.com' },
{}
)
const x = networkOptions.maskRequestFn!(capturedRequest as CapturedNetworkRequest)
expect(x).toEqual(expected)
})
Expand Down
17 changes: 14 additions & 3 deletions src/extensions/replay/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,20 @@ const removeAuthorizationHeader = (data: CapturedNetworkRequest): CapturedNetwor
const POSTHOG_PATHS_TO_IGNORE = ['/s/', '/e/', '/i/']
// want to ignore posthog paths when capturing requests, or we can get trapped in a loop
// because calls to PostHog would be reported using a call to PostHog which would be reported....
const ignorePostHogPaths = (data: CapturedNetworkRequest): CapturedNetworkRequest | undefined => {
const ignorePostHogPaths = (
data: CapturedNetworkRequest,
apiHostConfig: PostHogConfig['api_host']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to filter from ui_host as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so... I think it's just the possible endless loop of capturing our own api calls with our own api calls 🤞

): CapturedNetworkRequest | undefined => {
const url = convertToURL(data.name)
if (url && url.pathname && POSTHOG_PATHS_TO_IGNORE.some((path) => url.pathname.indexOf(path) === 0)) {

// we need to account for api host config as e.g. pathname could be /ingest/s/ and we want to ignore that
let replaceValue = apiHostConfig.indexOf('http') === 0 ? convertToURL(apiHostConfig)?.pathname : apiHostConfig
if (replaceValue === '/') {
replaceValue = ''
}
const pathname = url?.pathname.replace(replaceValue || '', '')

if (url && pathname && POSTHOG_PATHS_TO_IGNORE.some((path) => pathname.indexOf(path) === 0)) {
return undefined
}
return data
Expand Down Expand Up @@ -211,7 +222,7 @@ export const buildNetworkRequestOptions = (
const payloadLimiter = limitPayloadSize(config)

const enforcedCleaningFn: NetworkRecordOptions['maskRequestFn'] = (d: CapturedNetworkRequest) =>
payloadLimiter(ignorePostHogPaths(removeAuthorizationHeader(d)))
payloadLimiter(ignorePostHogPaths(removeAuthorizationHeader(d), instanceConfig.api_host))

const hasDeprecatedMaskFunction = isFunction(instanceConfig.session_recording.maskNetworkRequestFn)

Expand Down
Loading