PostHog / posthog-node

Official PostHog Node library
MIT License
21 stars 19 forks source link

Issue with dependency #85

Open phkundi opened 2 months ago

phkundi commented 2 months ago

Hi there. I recently posted about an issue I had on Stackoverflow and was advised to share it here as well. I am not 100% confident that Posthog was the source of the problem I had, but after updating my dependencies and removing Posthog, I was able to resolve my issue. As I'm not interested in reintroducing the error, I haven't verified that this issue is coming from Posthog but I figured it might be helpful to post this anyways.

Here's the info from my SO post:

I have a critial issue on my NextJS app where all of the requests to my API routes time out in production. It used to work perfectly up until recently but now my app is unusable (except for the static landing page). It's working perfectly in my local environment.

I'm using NextJS 14.0.1 with App Router; MongoDB + Mongoose

Here are the timed logs from a simple /api/lead endpoint (create a lead in DB)

When fetching this through postman, it still takes > 10 seconds for me to receive the response. Default timeout on Netlify / Vercel happens after 10 seconds.

Eventually, my console showed some useful information with the following error:

Compiling /api/auth/[...nextauth] ...
 ⚠ ./node_modules/.pnpm/debug@4.3.4_supports-color@9.4.0/node_modules/debug/src/node.js
Module not found: ESM packages (supports-color) need to be imported. Use 'import' to reference the package instead. https://nextjs.org/docs/messages/import-esm-externals

Import trace for requested module:
./node_modules/.pnpm/debug@4.3.4_supports-color@9.4.0/node_modules/debug/src/node.js
./node_modules/.pnpm/debug@4.3.4_supports-color@9.4.0/node_modules/debug/src/index.js
./node_modules/.pnpm/follow-redirects@1.15.6_debug@4.3.4/node_modules/follow-redirects/debug.js
./node_modules/.pnpm/follow-redirects@1.15.6_debug@4.3.4/node_modules/follow-redirects/index.js
./node_modules/.pnpm/axios@1.6.8/node_modules/axios/dist/node/axios.cjs
./node_modules/.pnpm/posthog-node@4.0.0/node_modules/posthog-node/lib/index.esm.js
./libs/posthog.js
./libs/next-auth.js
./app/api/auth/[...nextauth]/route.js

Like I said, I upgraded all of my dependencies and removed Posthog, and requests came through normally again. Not sure why this issue was only present in my production environment.

aronedwards commented 1 month ago

Also seeing this issue with Posthog, suddenly began occurring but seems limited to serverless calls (using vercel)

Another issue that is causing additional big issues is the fact that featureFlagsRequestTimeoutMs does not seem to work. running a setInterval I can see the function keeps running

let posthogClient: PostHog | null = null;

function PostHogClient() {
  if (!posthogClient && process.env.POSTHOG_API_KEY) {
    posthogClient = new PostHog(process.env.POSTHOG_API_KEY, {
      host: "https://eu.posthog.com",
      flushAt: 1,
      flushInterval: 0,
      requestTimeout: 3000,
      featureFlagsRequestTimeoutMs: 3000,
    });
  }
  return posthogClient;
}
...
const x = setInterval(() => {
      console.warn("SEC call");
    }, 1000);
const phClient = PostHogClient();
    if (!phClient) {
      return false;
    }
 const result = await phClient.isFeatureEnabled(featureID, id, {
      groups: {
        company: tenantKey,
      },
    });
    clearInterval(x);