Adds batching support to AnalyticsNode - #640
Conversation
|
|
|
||
| describe('drained emitted event', () => { | ||
| test('emits a drained event if only one event is dispatched', async () => { | ||
| it('emits a drained event if only one event is dispatched', async () => { |
There was a problem hiding this comment.
I changed test to it just because my IDE thought test() had to return a promise, so I had tons of red squiggles. Might be a transient type merge occurring, but it matches what we use in out other packages and fixed my issue 😁
| }) | ||
|
|
||
| test('http delivery errors are accessed through the emitter', (done) => { | ||
| it.skip('http delivery errors are accessed through the emitter', (done) => { |
There was a problem hiding this comment.
I skipped this for now - it looked like any error in the previous plugin would end up being treated as an http delivery error, so I wanted to confirm if the intent was for this just to apply to errors thrown by fetch and not non-200 status codes. @silesky
There was a problem hiding this comment.
http delivery was meant to be any non ok response along with any thrown errors -- sort of the way axios work, but the issue that maybe you're running into is that a thrown fetch error might not have a Response (e.g., a network error).
So, maybe the "response" keys should be optional, but we also add the request as well. (packages/node/src/app/emitted-errors.ts)
The justification is that even though most emitted errors will be http delivery, we may add other error types like validation in the future, so it's a good future-proof pattern to have, and it also lets you add type-safe specific metadata based on the code (which is the discriminant key). See: packages/node/src/app/emitted-errors.ts
edit: Error emitting doesn't seem to work atm.
- I simplified typing for now (for now -- we already had this
)
This will get called through "dispatchAndEmit"
|
Perf testing |
This PR adds batching support to AnalyticsNode.
Plugin behavior
This plugin supports batching events and sending batches when 1 of 3 conditions is met:
The plugin methods (track et. al.) resolve once the associated event has been sent to Segment or retries have been exhausted. This differs from the batching implementation in AnalyticsBrowser which instead resolves as soon as an event is enqueued. I decided to wait to resolve until the event was done being sent because it significantly simplifies the
EventQueue'sability to keep track of in-flight events, which in turn makes it easier to support graceful shutdowns.Implementation details
The plugin is broken into 2 main components:
ContextBatchPublisherThe
ContextBatchensures that events are batched properly. It won't allow events to be added if they exceed the event count or byte count.The
Publisherdoes the heavy lifting. It is responsible for receiving events, creating batches, and sending those batches to Segment.