posthog icon indicating copy to clipboard operation
posthog copied to clipboard

Not receiving events from AWS node.js lambda

Open AlexandreSi opened this issue 2 years ago • 5 comments

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.

AlexandreSi avatar Dec 02 '22 13:12 AlexandreSi

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.

benjackwhite avatar Dec 05 '22 16:12 benjackwhite

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).

AlexandreSi avatar Dec 05 '22 17:12 AlexandreSi

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...

benjackwhite avatar Dec 08 '22 16:12 benjackwhite

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?

AlexandreSi avatar Dec 09 '22 08:12 AlexandreSi

I think the issue might be that if a previous request is in-flight then shutdownAsync is not awaiting those.

macobo avatar Jan 10 '23 12:01 macobo

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

neilkakkar avatar Feb 09 '23 13:02 neilkakkar

My testing suggests flushAt might be the problem. If you try without this setting, does it work?

neilkakkar avatar Feb 13 '23 13:02 neilkakkar

Thanks @neilkakkar for your work! I'll be testing it right away

AlexandreSi avatar Feb 16 '23 11:02 AlexandreSi

Please do, let me know if you face issues, the version you're looking for is 2.5.1

neilkakkar avatar Feb 16 '23 12:02 neilkakkar

It seems to work fine! Thanks

AlexandreSi avatar Feb 16 '23 16:02 AlexandreSi