Skip to content

Weird mysql error #1475

@thany

Description

@thany

I've just started to implement appinsights in a monorepo, for a Next.js app router project. the appinsights client has to be in a package. All good, so I've created a file called app-insights-server.ts and put this in it:

import * as appInsights from 'applicationinsights';

const connectionString = process.env.APPLICATIONINSIGHTS_CONNECTION_STRING;

// Check if the SDK is already started
// This prevents re-initialization during hot-reloads in development
if (!appInsights.defaultClient && connectionString) {
  appInsights
    .setup(connectionString)
    .setAutoCollectRequests(true)
    .setAutoCollectPerformance(false, false)
    .setAutoCollectExceptions(true)
    .setAutoCollectDependencies(true)
    .setAutoCollectConsole(false, false)
    .setAutoCollectPreAggregatedMetrics(true)
    .setSendLiveMetrics(false)
    .setInternalLogging(false, true)
    .enableWebInstrumentation(false)
    .start();
}
else if (appInsights.defaultClient) {
  // In development, the client may exist but not be started
  // This ensures it starts on the next server-side request
  appInsights.start();
}

// Export the client for manual tracking
export const telemetryClient = appInsights.defaultClient;

Disclaimer: an AI helped me to set it up like this. It looks fine to me.

Then, starting the application, and importing the telemetryClient anywhere at all, without even using it, an error is spewed into the server console:

./node_modules/diagnostic-channel-publishers/dist/src/mysql.pub.js:71:21
Module not found: Can't resolve <dynamic>
  69 |         "_enqueueCallback"
  70 |     ];
> 71 |     var poolClass = require(path.dirname(originalMysqlPath) + "/lib/Pool");
     |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  72 |     poolCallbackFunctions.forEach(function (value) { return patchClassMemberFunction(poolClass, "Pool")(value); });
  73 |     return originalMysql;
  74 | };



Import trace:
  Server Component:
    ./node_modules/diagnostic-channel-publishers/dist/src/mysql.pub.js
    ./node_modules/diagnostic-channel-publishers/dist/src/index.js
    ./node_modules/applicationinsights/out/src/logs/diagnostic-channel/initialization.js
    ./node_modules/applicationinsights/out/src/logs/autoCollectLogs.js
    ./node_modules/applicationinsights/out/src/main.js
    ./node_modules/applicationinsights/out/src/index.js
    ./packages/api/src/app-insights-server.ts
    ./packages/api/src/fetch.ts
    ./packages/api/src/gen/sitecore.ts
    ./packages/api/src/server.ts
    ./apps/myapplication/src/components/Footer/Footer.server.tsx
    ./apps/myapplication/src/components/layout/AppLayout.tsx
    ./apps/myapplication/src/app/[lang]/layout.tsx

https://nextjs.org/docs/messages/module-not-found

Okay? I don't use mysql. Anywhere. Never used it, never installed it. Why is it trying to do anything with it? Is it sort of blindly assuming I use mysql? Because I don't.

The same AI told me the "solution", which is even more gobsmacking to me. In the application, put this in the next.config.ts:

const nextConfig: NextConfig = {
  serverExternalPackages: ['applicationinsights'],
  // ...
};

What? Why? Why does this help? Because it does. What kind of solution even is this? I've not seen this in the documentation, making it feel like a filthy workaround... For a problem that needn't exist in the first place.

Just don't touch packages that aren't there? Seems pretty basic to me.

More to the point - I would like to know why appinsights wants the mysql package. Seriously, why? 🤨 I want to send errors to appinsights (duh), not to mysql.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions