-
Notifications
You must be signed in to change notification settings - Fork 297
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
feat(shared): Add opt-out support for SDK telemetry #2099
Changes from all commits
7f00caf
22cfc85
92b16a2
7136d30
0c17fd7
a85cd00
2f72334
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,6 +132,11 @@ export default class IsomorphicClerk { | |
this.options = options; | ||
this.Clerk = Clerk; | ||
this.mode = inBrowser() ? 'browser' : 'server'; | ||
|
||
if (!this.options.sdkMetadata) { | ||
this.options.sdkMetadata = SDK_METADATA; | ||
} | ||
|
||
void this.loadClerkJS(); | ||
} | ||
|
||
|
@@ -168,7 +173,6 @@ export default class IsomorphicClerk { | |
domain: this.domain, | ||
} as any); | ||
|
||
c.sdkMetadata = this.options.sdkMetadata ?? SDK_METADATA; | ||
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. Cleaning up this metadata thing by handling it in |
||
await c.load(this.options); | ||
} else { | ||
// Otherwise use the instantiated Clerk object | ||
|
@@ -195,13 +199,9 @@ export default class IsomorphicClerk { | |
throw new Error('Failed to download latest ClerkJS. Contact [email protected].'); | ||
} | ||
|
||
global.Clerk.sdkMetadata = this.options.sdkMetadata ?? SDK_METADATA; | ||
|
||
await global.Clerk.load(this.options); | ||
} | ||
|
||
global.Clerk.sdkMetadata = this.options.sdkMetadata ?? { name: PACKAGE_NAME, version: PACKAGE_VERSION }; | ||
|
||
if (global.Clerk?.loaded || global.Clerk?.isReady()) { | ||
return this.hydrateClerkJS(global.Clerk); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
*/ | ||
/*/ | ||
!src/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import { TelemetryCollector } from '../telemetry'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
const TEST_PK = 'pk_test_Zm9vLWJhci0xMy5jbGVyay5hY2NvdW50cy5kZXYk'; | ||
|
||
describe('TelemetryCollector', () => { | ||
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. Added some high-level tests for the collector. Most importantly, validating that events aren't sent when
brkalow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
test('does nothing when disabled', async () => { | ||
const fetchSpy = jest.spyOn(global, 'fetch'); | ||
|
||
const collector = new TelemetryCollector({ | ||
disabled: true, | ||
publishableKey: TEST_PK, | ||
}); | ||
|
||
collector.record('TEST_EVENT', {}); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(fetchSpy).not.toHaveBeenCalled(); | ||
|
||
fetchSpy.mockRestore(); | ||
}); | ||
|
||
test('does nothing when CLERK_TELEMETRY_DISABLED is set', async () => { | ||
process.env.CLERK_TELEMETRY_DISABLED = '1'; | ||
|
||
const fetchSpy = jest.spyOn(global, 'fetch'); | ||
|
||
const collector = new TelemetryCollector({ | ||
publishableKey: TEST_PK, | ||
}); | ||
|
||
collector.record('TEST_EVENT', {}); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(fetchSpy).not.toHaveBeenCalled(); | ||
|
||
fetchSpy.mockRestore(); | ||
|
||
process.env.CLERK_TELEMETRY_DISABLED = undefined; | ||
}); | ||
|
||
test('does not send events when debug is enabled, logs them instead', async () => { | ||
const fetchSpy = jest.spyOn(global, 'fetch'); | ||
const consoleSpy = jest.spyOn(global.console, 'log'); | ||
|
||
const collector = new TelemetryCollector({ | ||
debug: true, | ||
publishableKey: TEST_PK, | ||
}); | ||
|
||
collector.record('TEST_EVENT', {}); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(fetchSpy).not.toHaveBeenCalled(); | ||
|
||
expect(consoleSpy.mock.calls).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
{ | ||
"cv": "", | ||
"event": "TEST_EVENT", | ||
"it": "development", | ||
"payload": {}, | ||
"pk": "pk_test_Zm9vLWJhci0xMy5jbGVyay5hY2NvdW50cy5kZXYk", | ||
"sdk": undefined, | ||
"sdkv": undefined, | ||
}, | ||
], | ||
] | ||
`); | ||
|
||
consoleSpy.mockRestore(); | ||
fetchSpy.mockRestore(); | ||
}); | ||
|
||
test('enables debug via environment variable', async () => { | ||
process.env.CLERK_TELEMETRY_DEBUG = '1'; | ||
|
||
const fetchSpy = jest.spyOn(global, 'fetch'); | ||
const consoleSpy = jest.spyOn(global.console, 'log'); | ||
|
||
const collector = new TelemetryCollector({ | ||
publishableKey: TEST_PK, | ||
}); | ||
|
||
collector.record('TEST_EVENT', {}); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(fetchSpy).not.toHaveBeenCalled(); | ||
|
||
expect(consoleSpy.mock.calls).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
{ | ||
"cv": "", | ||
"event": "TEST_EVENT", | ||
"it": "development", | ||
"payload": {}, | ||
"pk": "pk_test_Zm9vLWJhci0xMy5jbGVyay5hY2NvdW50cy5kZXYk", | ||
"sdk": undefined, | ||
"sdkv": undefined, | ||
}, | ||
], | ||
] | ||
`); | ||
|
||
consoleSpy.mockRestore(); | ||
fetchSpy.mockRestore(); | ||
|
||
process.env.CLERK_TELEMETRY_DEBUG = undefined; | ||
}); | ||
|
||
test('sends events after a delay when buffer is not full', async () => { | ||
const fetchSpy = jest.spyOn(global, 'fetch'); | ||
|
||
const collector = new TelemetryCollector({ | ||
publishableKey: TEST_PK, | ||
}); | ||
|
||
collector.record('TEST_EVENT', {}); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(fetchSpy).toHaveBeenCalled(); | ||
|
||
fetchSpy.mockRestore(); | ||
}); | ||
|
||
test('sends events immediately when the buffer limit is reached', async () => { | ||
const fetchSpy = jest.spyOn(global, 'fetch'); | ||
|
||
const collector = new TelemetryCollector({ | ||
maxBufferSize: 2, | ||
publishableKey: TEST_PK, | ||
}); | ||
|
||
collector.record('TEST_EVENT', {}); | ||
collector.record('TEST_EVENT', {}); | ||
|
||
expect(fetchSpy).toHaveBeenCalled(); | ||
|
||
fetchSpy.mockRestore(); | ||
}); | ||
}); |
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.
Moved the
TelemetryCollector
instantiation intoClerk.load()
instead of the constructor. It's much easier to pass options this way.