-
Notifications
You must be signed in to change notification settings - Fork 135
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
Updates analytics error handling to support checking delivery failures on Context #505
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3139512
add tests to ensure plugin errors do not bubble to analytics API calls
chrisradek 7ebee3b
fix vscode launch configurations
chrisradek b225844
add ContextCancelation retry tests and improve plugin error behavior …
chrisradek 57f2737
add ContextCancelation support to before plugins and prevent bubbling…
chrisradek 118f18e
add Context.failedDelivery() to check if an event failed to be delivered
chrisradek 0c71171
only set failed delivery on context on final attempt
chrisradek 9a3123a
Merge remote-tracking branch 'origin/master' into fix-plugin-before
chrisradek 62e445e
add changeset
chrisradek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
packages/browser/src/core/analytics/__tests__/integration.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
import { PriorityQueue } from '../../../lib/priority-queue' | ||
import { MiddlewareParams } from '../../../plugins/middleware' | ||
import { Context } from '../../context' | ||
import { EventQueue } from '../../queue/event-queue' | ||
import { Analytics } from '../index' | ||
import { | ||
AfterPlugin, | ||
BeforePlugin, | ||
DestinationPlugin, | ||
EnrichmentPlugin, | ||
} from './test-plugins' | ||
|
||
describe('Analytics', () => { | ||
describe('plugin error behavior', () => { | ||
const retriesEnabledScenarios = [true, false] | ||
retriesEnabledScenarios.forEach((retriesEnabled) => { | ||
describe(`with retries ${ | ||
retriesEnabled ? 'enabled' : 'disabled' | ||
}`, () => { | ||
let analytics: Analytics | ||
const attemptCount = retriesEnabled ? 2 : 1 | ||
|
||
beforeEach(() => { | ||
const queue = new EventQueue(new PriorityQueue(attemptCount, [])) | ||
analytics = new Analytics({ writeKey: 'writeKey' }, {}, queue) | ||
}) | ||
|
||
// Indicates plugins should throw | ||
const shouldThrow = true | ||
|
||
it(`"before" plugin errors should not throw (single dispatched event)`, async () => { | ||
const plugin = new BeforePlugin({ shouldThrow }) | ||
const trackSpy = jest.spyOn(plugin, 'track') | ||
|
||
await analytics.register(plugin) | ||
const context = await analytics.track('test') | ||
expect(trackSpy).toHaveBeenCalledTimes(attemptCount) | ||
expect(context).toBeInstanceOf(Context) | ||
expect(context.failedDelivery()).toBeTruthy() | ||
}) | ||
|
||
it(`"before" plugin errors should not throw (multiple dispatched events)`, async () => { | ||
const plugin = new BeforePlugin({ shouldThrow }) | ||
const trackSpy = jest.spyOn(plugin, 'track') | ||
|
||
await analytics.register(plugin) | ||
const contexts = await Promise.all([ | ||
analytics.track('test1'), | ||
analytics.track('test2'), | ||
analytics.track('test3'), | ||
]) | ||
expect(trackSpy).toHaveBeenCalledTimes(3 * attemptCount) | ||
|
||
for (const context of contexts) { | ||
expect(context).toBeInstanceOf(Context) | ||
expect(context.failedDelivery()).toBeTruthy() | ||
} | ||
}) | ||
|
||
it(`"before" plugin errors should not impact callbacks`, async () => { | ||
const plugin = new BeforePlugin({ shouldThrow }) | ||
const trackSpy = jest.spyOn(plugin, 'track') | ||
|
||
await analytics.register(plugin) | ||
|
||
const context = await new Promise<Context>((resolve) => { | ||
void analytics.track('test', resolve) | ||
}) | ||
|
||
expect(trackSpy).toHaveBeenCalledTimes(attemptCount) | ||
expect(context).toBeInstanceOf(Context) | ||
expect(context.failedDelivery()).toBeTruthy() | ||
}) | ||
|
||
const testPlugins = [ | ||
new EnrichmentPlugin({ shouldThrow }), | ||
new DestinationPlugin({ shouldThrow }), | ||
new AfterPlugin({ shouldThrow }), | ||
] | ||
testPlugins.forEach((plugin) => { | ||
it(`"${plugin.type}" plugin errors should not throw (single dispatched event)`, async () => { | ||
const trackSpy = jest.spyOn(plugin, 'track') | ||
|
||
await analytics.register(plugin) | ||
|
||
const context = await analytics.track('test') | ||
expect(trackSpy).toHaveBeenCalledTimes(1) | ||
expect(context).toBeInstanceOf(Context) | ||
expect(context.failedDelivery()).toBeFalsy() | ||
}) | ||
|
||
it(`"${plugin.type}" plugin errors should not throw (multiple dispatched events)`, async () => { | ||
const trackSpy = jest.spyOn(plugin, 'track') | ||
|
||
await analytics.register(plugin) | ||
|
||
const contexts = await Promise.all([ | ||
analytics.track('test1'), | ||
analytics.track('test2'), | ||
analytics.track('test3'), | ||
]) | ||
|
||
expect(trackSpy).toHaveBeenCalledTimes(3) | ||
for (const context of contexts) { | ||
expect(context).toBeInstanceOf(Context) | ||
expect(context.failedDelivery()).toBeFalsy() | ||
} | ||
}) | ||
|
||
it(`"${plugin.type}" plugin errors should not impact callbacks`, async () => { | ||
const trackSpy = jest.spyOn(plugin, 'track') | ||
|
||
await analytics.register(plugin) | ||
|
||
const context = await new Promise<Context>((resolve) => { | ||
void analytics.track('test', resolve) | ||
}) | ||
|
||
expect(trackSpy).toHaveBeenCalledTimes(1) | ||
expect(context).toBeInstanceOf(Context) | ||
expect(context.failedDelivery()).toBeFalsy() | ||
}) | ||
}) | ||
|
||
it('"before" plugin supports cancelation (single dispatched event)', async () => { | ||
const plugin = new BeforePlugin({ shouldCancel: true }) | ||
const trackSpy = jest.spyOn(plugin, 'track') | ||
|
||
await analytics.register(plugin) | ||
|
||
const context = await analytics.track('test') | ||
expect(trackSpy).toHaveBeenCalledTimes(1) | ||
expect(context).toBeInstanceOf(Context) | ||
expect(context.failedDelivery()).toBeTruthy() | ||
}) | ||
|
||
it('"before" plugin supports cancelation (multiple dispatched events)', async () => { | ||
const plugin = new BeforePlugin({ shouldCancel: true }) | ||
const trackSpy = jest.spyOn(plugin, 'track') | ||
|
||
await analytics.register(plugin) | ||
const contexts = await Promise.all([ | ||
analytics.track('test1'), | ||
analytics.track('test2'), | ||
analytics.track('test3'), | ||
]) | ||
expect(trackSpy).toHaveBeenCalledTimes(3) | ||
|
||
for (const context of contexts) { | ||
expect(context).toBeInstanceOf(Context) | ||
expect(context.failedDelivery()).toBeTruthy() | ||
} | ||
}) | ||
|
||
it(`source middleware should not throw when "next" not called`, async () => { | ||
const sourceMiddleware = jest.fn<void, MiddlewareParams[]>(() => {}) | ||
|
||
await analytics.addSourceMiddleware(sourceMiddleware) | ||
|
||
const context = await analytics.track('test') | ||
|
||
expect(sourceMiddleware).toHaveBeenCalledTimes(1) | ||
expect(context).toBeInstanceOf(Context) | ||
expect(context.failedDelivery()).toBeTruthy() | ||
}) | ||
|
||
it(`source middleware errors should not throw`, async () => { | ||
const sourceMiddleware = jest.fn<void, MiddlewareParams[]>(() => { | ||
throw new Error('Source middleware error') | ||
}) | ||
|
||
await analytics.addSourceMiddleware(sourceMiddleware) | ||
|
||
const context = await analytics.track('test') | ||
|
||
expect(sourceMiddleware).toHaveBeenCalledTimes(1 * attemptCount) | ||
expect(context).toBeInstanceOf(Context) | ||
expect(context.failedDelivery()).toBeTruthy() | ||
}) | ||
|
||
it(`source middleware errors should not impact callbacks`, async () => { | ||
const sourceMiddleware = jest.fn<void, MiddlewareParams[]>(() => { | ||
throw new Error('Source middleware error') | ||
}) | ||
|
||
await analytics.addSourceMiddleware(sourceMiddleware) | ||
|
||
const context = await new Promise<Context>((resolve) => { | ||
void analytics.track('test', resolve) | ||
}) | ||
|
||
expect(sourceMiddleware).toHaveBeenCalledTimes(1 * attemptCount) | ||
expect(context).toBeInstanceOf(Context) | ||
expect(context.failedDelivery()).toBeTruthy() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
89 changes: 89 additions & 0 deletions
89
packages/browser/src/core/analytics/__tests__/test-plugins.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { Context, ContextCancelation, Plugin } from '../../../index' | ||
|
||
export interface BasePluginOptions { | ||
shouldThrow?: boolean | ||
shouldCancel?: boolean | ||
} | ||
|
||
class BasePlugin implements Partial<Plugin> { | ||
public version = '1.0.0' | ||
private shouldCancel: boolean | ||
private shouldThrow: boolean | ||
|
||
constructor({ | ||
shouldCancel = false, | ||
shouldThrow = false, | ||
}: BasePluginOptions) { | ||
this.shouldCancel = shouldCancel | ||
this.shouldThrow = shouldThrow | ||
} | ||
|
||
isLoaded() { | ||
return true | ||
} | ||
|
||
load() { | ||
return Promise.resolve() | ||
} | ||
|
||
alias(ctx: Context): Context { | ||
return this.task(ctx) | ||
} | ||
|
||
group(ctx: Context): Context { | ||
return this.task(ctx) | ||
} | ||
|
||
identify(ctx: Context): Context { | ||
return this.task(ctx) | ||
} | ||
|
||
page(ctx: Context): Context { | ||
return this.task(ctx) | ||
} | ||
|
||
screen(ctx: Context): Context { | ||
return this.task(ctx) | ||
} | ||
|
||
track(ctx: Context): Context { | ||
return this.task(ctx) | ||
} | ||
|
||
private task(ctx: Context): Context { | ||
if (this.shouldCancel) { | ||
ctx.cancel( | ||
new ContextCancelation({ | ||
retry: false, | ||
}) | ||
) | ||
} else if (this.shouldThrow) { | ||
throw new Error(`Error thrown in task`) | ||
} | ||
return ctx | ||
} | ||
} | ||
|
||
export class BeforePlugin extends BasePlugin implements Plugin { | ||
public name = 'Test Before Error' | ||
public type = 'before' as const | ||
} | ||
|
||
export class EnrichmentPlugin extends BasePlugin implements Plugin { | ||
public name = 'Test Enrichment Error' | ||
public type = 'enrichment' as const | ||
} | ||
|
||
export class DestinationPlugin extends BasePlugin implements Plugin { | ||
public name = 'Test Destination Error' | ||
public type = 'destination' as const | ||
|
||
public ready() { | ||
return Promise.resolve(true) | ||
} | ||
} | ||
|
||
export class AfterPlugin extends BasePlugin implements Plugin { | ||
public name = 'Test After Error' | ||
public type = 'after' as const | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
amazing 🙌