microsoft / ApplicationInsights-node.js

Microsoft Application Insights SDK for Node.js
MIT License
323 stars 141 forks source link

Runtime dependency to nonexistent package @azure/functions-core #1102

Open MHarutunian opened 1 year ago

MHarutunian commented 1 year ago

Hi,

it seems that there is an error in the latest non-beta version (2.5.0), which has a runtime dependency to @azure/functions-core in AzureFunctionsHook. This is breaking our build process, since the package cannot be resolved:

WARNING in ./node_modules/applicationinsights/out/AutoCollection/AzureFunctionsHook.js 51:40-72
Module not found: Error: Can't resolve '@azure/functions-core' in './node_modules/applicationinsights/out/AutoCollection'
 @ ./node_modules/applicationinsights/out/applicationinsights.js 17:27-73
 @ ./src/main.ts 28:33-63

The package doesn't even exist in the public registry in the first place:

$ npm install '@azure/functions-core'
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/@azure%2ffunctions-core - Not found
npm ERR! 404 
npm ERR! 404  '@azure/functions-core@*' is not in this registry.

I'm not sure if this is a private package or if it doesn't exist at all, but this change is breaking our webpack build and also our Function App deployment - deploying the (broken) webpack build results in Azure runtime is unavailable and 503 responses.

I think this was introduced in #1070 which was released in 2.4.2, since reverting/pinning our version to 2.4.1 fixed the issue.

If there are any additional details I can provide please let me know.

Version

Node v16 with packages (only relevant ones):

@azure/functions@3.5.0
applicationinsights@2.5.0
azure-functions-core-tools@4.0.5030
hectorhdzg commented 1 year ago

@MHarutunian can you share your webpack configuration? this is a package only available in Azure Functions and we added typings file here

MHarutunian commented 1 year ago

Thank you for the quick feedback - I expected that this was some kind of internal package, but searching for it only brought up azure-functions-core-tools. The webpack configuration is pretty basic:

// we use this to resolve path aliases, shouldn't affect the issue at hand I believe
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
module.exports = {
  entry: "./src/main.ts",
  target: "node16",
  mode: "production",
  optimization: {
    minimize: false,
    moduleIds: "named",
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        loader: "ts-loader",
      },
    ],
  },
  resolve: {
    extensions: [".ts", ".js"],
    plugins: [
      new TsconfigPathsPlugin({ 
        extensions: [".ts", ".js"],
      }),
    ],
  },
  output: {
    clean: true,
    library: {
      type: "commonjs-module",
    },
  },
};

What might be interesting is that webpack actually rewrites the line in question to this:

this._functionsCoreModule = __webpack_require__(
  Object(
    (function webpackMissingModule() {
      const e = new Error("Cannot find module '@azure/functions-core'");
      e.code = "MODULE_NOT_FOUND";
      throw e;
    })()
  )
);

Running the Function App locally with func start still works (although the AzureFunctionsHook is probably "disabled" due to the webpack generated error being thrown). Deploying the whole thing to Azure is crashing the entire App though, which I assumed to be because of this require().

MHarutunian commented 1 year ago

Your comment @hectorhdzg gave me an idea, since you mentioned that the package will be available at runtime. Adding

externals: "@azure/functions-core",

to our webpack config removes the warning and adds an actual require to @azure/functions-core instead. Deploying it to Azure also works, even though I cannot exactly tell if it all works as intended based on the telemetry data yet, I would have to look into it tomorrow. Is there any way to quickly figure out if this specific feature is doing what it should? Just so I know what I should be looking for.

It might be worth mentioning @azure/functions-core in the README or so, because it caught us off guard and I spent several hours to figure out what had happened.

For some reason I cannot reproduce the 503 errors though, even without the above changes to the webpack configuration, so I'm not sure if it's related. The error disappeared as soon as I reverted the version update (and I only reverted this package), so I thought it was related for sure, but now I cannot tell really :thinking:

hectorhdzg commented 1 year ago

@MHarutunian if you are using Azure Functions correlation of telemetry should be handled automatically, custom code were needed before to have it working, more info here, if you have multiple Azure Functions calling each other you should be able to visualize the data correlated correctly when using Application Insights App Map in the portal or similar, I'm not sure about 503 errors, sounds like an error deploying the app to Azure Functions, maybe someone in https://github.com/Azure/azure-functions-nodejs-worker can help with that if you continue to see the error.

MHarutunian commented 1 year ago

I just got back to this now, but it seems it's working as expected. We did have the setup code before, but I removed it with the fix for webpack I mentioned above, and the correlation is working automatically and visible in the Application Map in Azure. Thanks for the additional hints!

I still think it would be worth mentioning in the README somewhere, because bundling Function App code to reduce cold start times is not uncommon, and it wasn't obvious to me what the @azure/functions-core warnings were about. Not sure how other bundlers handle this, but with webpack the aforementioned auto-correlation feature will not work without additional configuration. This is what we ended up using successfully:

externals: ["@azure/functions-core"],
KasperAndersson commented 4 months ago

Im using nextjs 14.4.2, I get the same errors - after updating from nextjs 14.1.2. Adding the suggested externals: ["@azure/functions-core"], doesn't seems to solve the issue.

Any ideas? Thanks in advance <3

 ○ Compiling /instrumentation ...
 ⚠ ./node_modules/@azure/monitor-opentelemetry/dist/index.js
Module not found: Can't resolve '@azure/functions-core' in '/[PROJECT_FOLDER]/node_modules/@azure/monitor-opentelemetry/dist'

Import trace for requested module:
./node_modules/@azure/monitor-opentelemetry/dist/index.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js

./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/node/index.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/platform/index.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/node_modules/@opentelemetry/instrumentation/build/src/index.js
./node_modules/@azure/opentelemetry-instrumentation-azure-sdk/dist/index.js
./node_modules/@azure/monitor-opentelemetry/dist/index.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js

./node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
./node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
./node_modules/@opentelemetry/instrumentation/build/src/platform/node/index.js
./node_modules/@opentelemetry/instrumentation/build/src/platform/index.js
./node_modules/@opentelemetry/instrumentation/build/src/index.js
./node_modules/diagnostic-channel-publishers/dist/src/azure-coretracing.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

./node_modules/@opentelemetry/sdk-node/build/src/TracerProviderWithEnvExporter.js
Module not found: Can't resolve '@opentelemetry/exporter-jaeger' in '/[PROJECT_FOLDER]/node_modules/@opentelemetry/sdk-node/build/src'

Import trace for requested module:
./node_modules/@opentelemetry/sdk-node/build/src/TracerProviderWithEnvExporter.js
./node_modules/@opentelemetry/sdk-node/build/src/sdk.js
./node_modules/@opentelemetry/sdk-node/build/src/index.js
./node_modules/@azure/monitor-opentelemetry/dist/index.js
./node_modules/applicationinsights/out/src/main.js
./node_modules/applicationinsights/out/src/index.js
willholmeswastaken commented 4 months ago

I'm seeing a similar error / stack trace to @KasperAndersson's above. Adding the externals value to my webpack config just causes the logs to be blank lines which still keeps the problem there but hides the content of the error messages.