fix(telemetry): send PostHog events through the main HTTP client so they honor the proxy (#35972)

* fix(telemetry): send PostHog events through the main HTTP client so they honor the proxy

* fix(telemetry): omit credentials on PostHog sends
This commit is contained in:
Neil
2026-10-29 07:13:47 -07:00
committed by GitHub
parent ba7b9405d7
commit 5cd7b5d52c
2 changed files with 44 additions and 1 deletions
@@ -0,0 +1,39 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { setMainHttpClient } from '../network/http-client'
import { createPostHogClient } from './posthog-transport'
describe('PostHog transport', () => {
const mainFetch = vi.fn(async () => new Response('{}', { status: 200 }))
const globalFetch = vi.fn(async () => new Response('{}', { status: 200 }))
afterEach(() => {
setMainHttpClient(null)
vi.unstubAllGlobals()
mainFetch.mockClear()
globalFetch.mockClear()
})
const capture = async (consent: Parameters<typeof createPostHogClient>[1]) => {
vi.stubGlobal('fetch', globalFetch)
setMainHttpClient({ fetch: mainFetch, proxySession: () => null })
const client = createPostHogClient('test-key', consent)
client.capture({ distinctId: 'install-1', event: 'app_opened' })
await client.shutdown()
}
// The desktop client sends through the session that carries the user's proxy.
it('sends through the main HTTP client, not global fetch', async () => {
await capture({ effective: 'enabled' })
expect(mainFetch).toHaveBeenCalledWith(
expect.stringContaining('/batch'),
expect.objectContaining({ method: 'POST', credentials: 'omit' })
)
expect(globalFetch).not.toHaveBeenCalled()
})
it('sends nothing when telemetry is disabled', async () => {
await capture({ effective: 'disabled', reason: 'user_opt_out' })
expect(mainFetch).not.toHaveBeenCalled()
expect(globalFetch).not.toHaveBeenCalled()
})
})
+5 -1
View File
@@ -1,6 +1,7 @@
import { arch as osArch, platform as osPlatform, release as osRelease } from 'node:os'
import type * as PostHogSdk from 'posthog-node'
import { getAppEnvironment } from '../../shared/app-environment'
import { getMainHttpClient } from '../network/http-client'
import type { CommonProps, EventName } from '../../shared/telemetry-events'
import type { ConsentState } from './consent'
@@ -84,7 +85,10 @@ export function createPostHogClient(writeKey: string, consent: ConsentState): Po
// Disable SDK GeoIP/IP enrichment to keep the wire inside the declared allow-list.
disableGeoip: true,
// Increase offline buffer from 1000 to 5000; overflow drops oldest events.
maxQueueSize: 5000
maxQueueSize: 5000,
// The SDK's default global fetch skips the desktop session's proxy; resolved per send.
// Omit credentials so the desktop session never attaches its cookies to telemetry.
fetch: (url, options) => getMainHttpClient().fetch(url, { ...options, credentials: 'omit' })
})
if (shouldOptOutSdkAtInit(consent)) {