posthog
posthog copied to clipboard
Not receiving events from AWS node.js lambda
Bug description
I want to send events from my backend that runs in microservices in node.js AWS Lambda functions. But there is nothing showing up on my posthog client, not in events nor in ingestion warnings.
How to reproduce
Here is what my backend looks like:
-
Client instanciation:
const client = new PostHog( POSTHOG_API_KEY, { flushAt: 1, flushInterval: 0 } ); -
Event capture:
const handler = async (event) => { try { ... client.capture({ distinctId: userId, event: 'subscription-purchase-completed', properties: { planId } }); } catch (error) { ... } finally { await client.shutdownAsync(); // I also tested with: // client.shutdown(); } }
I could maybe provide a serverless project to reproduce it yourselves but it would take some time.
Environment
- [x] PostHog Cloud
- [ ] self-hosted PostHog, version/commit: please provide
Additional context
- My backend already makes some api call to other services so I don't think the posthog api calls are blocked by AWS.
- I used the same api key in a node terminal on my computer with the exact same data and it works.
Overall it looks fine code-wise so I'm wondering if you could add posthog.debug() and check it is outputting something that looks sensible.
Thanks for your answer!
I've updated the client instanciation with:
const client = new PostHog(
POSTHOG_API_KEY,
{ flushAt: 1, flushInterval: 0 }
);
client.debug(true);
And at the moment the capture call is done, here is a log that appears:
2022-12-05T17:09:29.943Z 92985e56-2d7a-468d-a4eb-51a808ac221a INFO PostHog Debug capture {
distinct_id: 'tVUYpNMz1AfsbzJtxUEFGguu4Mn1',
event: 'subscription-purchase-completed',
properties: {
planId: 'gdevelop_silver',
'$lib': 'posthog-node',
'$lib_version': '2.2.2',
'$active_feature_flags': undefined,
'$session_id': undefined
},
type: 'capture',
library: 'posthog-node',
library_version: '2.2.2',
timestamp: '2022-12-05T17:09:29.941Z'
}
That's the only log I can see from the client. It looks like that there is nothing logged around the shutdown call (synchronous).
You should also see some message related to the flushing so this indicates to me that the lambda is somehow exiting too early still.
Could you wrap the shutdown code like so:
const handler = async (event) => {
try {
...
client.capture({
distinctId: userId,
event: 'subscription-purchase-completed',
properties: { planId }
});
} catch (error) {
...
} finally {
console.log("PH: Starting shutdown");
await client.shutdownAsync();
console.log("PH: Shutdown finished");
await new Promise(r => setTimeout(r, 500));
console.log("PH: Delay finished");
}
}
I'm interested in the order of output as I'm not sure why this isn't happening as expected...
Hi, Thanks for your suggestion, it seems to have worked. Here is what happened:
Starting shutdown
Shutdown finished
2022-12-09T08:26:36.681Z f3eb95f5-7ff6-4f37-8d08-742647f2f945 INFO PostHog Debug flush [
{
distinct_id: 'tVUYpNMz1AfsbzJtxUEpPTuu4Mn1',
event: 'subscription-purchase-completed',
properties: {
planId: 'gdevelop_silver',
'$lib': 'posthog-node',
'$lib_version': '2.2.2',
'$active_feature_flags': undefined,
'$session_id': undefined
},
type: 'capture',
library: 'posthog-node',
library_version: '2.2.2',
timestamp: '2022-12-09T08:26:36.629Z'
}
]
Delay finished
(I changed the delay from 500ms as you suggested to 1000ms just to make sure).
I don't understand why awaiting for the shutdown async is not enough though?
I think the issue might be that if a previous request is in-flight then shutdownAsync is not awaiting those.
I'm fixing this next week! The workaround for now is to either do the above, or use the 1.X version of the node library
My testing suggests flushAt might be the problem. If you try without this setting, does it work?
Thanks @neilkakkar for your work! I'll be testing it right away
Please do, let me know if you face issues, the version you're looking for is 2.5.1
It seems to work fine! Thanks