I’m trying to send messages to Segment inside Forge functions to track events. I’m finding they’re tracked maybe 1 fifth of the time they should. Each kind of event has been tracked at least once, but there are clearly gaps where events should be tracked but aren’t. I’ve set
1 so Segment shouldn’t be batching messages.
My suspicion is that Forge is blocking some of our API calls back to Segment as we’re using the
analytics-node package which uses the
tty module. I get the warning below in the console when I call the Segment API from a Forge function. I’m not sure if this is blocking calls to Segment occasionally or there’s some kind of throttling mechanism inside Forge functions.
Thanks in advance.
Hi Rhys, I see you’ve already considered the flush count so it’s likely not a case of events being buffered then never sent because the Lambda has gone… Is there some kind of debug logging you can turn on within that package? Failing that I suppose you could just add logs around your code that calls the analytics methods, maybe, then compare the log count to the analytics count.
In addition to @jhazelwood’s comments above I’ve had a quick look at analytics-node and it looks like they don’t
await the promise when sending analytics to segment. This would be problematic for any serverless environment, not just Forge, and it seems other people have run up against this. Maybe that enclosed thread has some tips that will help you get some more deterministic behaviour, for context the Forge runtime will not wait for untracked
Promise(s) meaning that if your function handler finishes before the metrics have been sent they will be lost.
Let me know if you have any further questions, hope this helps.
Thanks @SamSmyth and @jhazelwood . It turned out I needed to await the promise when sending analytics to segment to get these events to track consistently. Thanks for looking into this.