ApplicationInsights-node.js: Opentelemetry spans are not showing in "Application map"

Hello,

I’m using the beta of this package, and everything seems to work fine, all spans are reported correctly when running my app locally, but all the spans are being reported as Role Name “Web” and my computers name as “Role instance”.

I tried to overwrite these values (so that they would end up in my real service) by following these instructions:

First of all, the docs seem to be invalid, I dug into the code and the correct way to overwrite these values should be:

appInsights = new ApplicationInsightsClient(config);

  const traceResource = appInsights.getTraceResource() as {
    attributes: Record<string, string>;
  };
  const metricResource = appInsights.getMetricResource() as {
    attributes: Record<string, string>;
  };
  const logResource = appInsights.getLogResource() as {
    attributes: Record<string, string>;
  };

  // ----------------------------------------
  // Setting role name and role instance
  // ----------------------------------------
  traceResource.attributes[SemanticResourceAttributes.SERVICE_NAME] =
    ENV.WEBSITE_SITE_NAME;
  traceResource.attributes[SemanticResourceAttributes.SERVICE_INSTANCE_ID] =
    ENV.WEBSITE_INSTANCE_ID;

  metricResource.attributes[SemanticResourceAttributes.SERVICE_NAME] =
    ENV.WEBSITE_SITE_NAME;
  metricResource.attributes[SemanticResourceAttributes.SERVICE_INSTANCE_ID] =
    ENV.WEBSITE_INSTANCE_ID;

  logResource.attributes[SemanticResourceAttributes.SERVICE_NAME] =
    ENV.WEBSITE_SITE_NAME;
  logResource.attributes[SemanticResourceAttributes.SERVICE_INSTANCE_ID] =
    ENV.WEBSITE_INSTANCE_ID;

But that seems to have no effect on the traces my computer reports to Application Insights, they still have “Web” and my computer’s name as values. Screenshot 2023-02-23 at 13 46 41

However, the next problem comes when I deploy my app, then there are no spans at all, when I dig into details for requests in the application map, what am I doing wrong?

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 16 (3 by maintainers)

Most upvoted comments

I was FINALLY able to solve it! 🎉

Here is a description of what caused the issue, so you can improve documentation for both application-insights and app service.

To support an easy setup (I guess) Azure App Service adds the following env-variable:

NODE_OPTIONS=--require /agents/node/build/src/Loader.js

That Loader.js seems to check if any of the APPLICATIONINSIGHTS_CONNECTION_STRING or APPINSIGHTS_INSTRUMENTATIONKEY is set, if so it will automatically start the non-beta agent. https://github.com/microsoft/ApplicationInsights-node.js/blob/8b322090f52c2b34c7021a8aed5f7237712fc48f/Bootstrap/Default.ts#L59

Not sure of the reason, but that causes the non-beta-agent to have precedence over the beta-agent, and your app will just report to app insights with the non-beta format (and it’s default options).

Solved it by renaming APPLICATIONINSIGHTS_CONNECTION_STRING to TELEMETRY_APPLICATIONINSIGHTS_CONNECTION_STRING and APPINSIGHTS_INSTRUMENTATIONKEY to TELEMETRY_APPINSIGHTS_INSTRUMENTATIONKEY so it tricks Loader.js to skip starting the non-beta applicationinsights-agent.

However, a drawback with that approach seems to be that Azure doesn’t understand the connection between my App Service and Application Insights. But the data is streamed and correctly interpreted anyway Screenshot 2023-02-24 at 13 39 19