mirror of
https://github.com/stablyai/orca.git
synced 2026-11-02 18:17:36 +00:00
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:
@@ -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()
|
||||
})
|
||||
})
|
||||
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user