Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aplicationinsights package incompatibilities with ESM #1354

Open
1 of 3 tasks
timofei-iatsenko opened this issue Jul 2, 2024 · 3 comments
Open
1 of 3 tasks

aplicationinsights package incompatibilities with ESM #1354

timofei-iatsenko opened this issue Jul 2, 2024 · 3 comments

Comments

@timofei-iatsenko
Copy link

  • Package Name: applicationinsights
  • Package Version: 3.1.0
  • Operating system: Doesn't matter
  • nodejs
    • version: 20
  • browser
    • name/version:
  • typescript
    • version:

Describe the bug
applicationinsights doesn't work properly in ESM environment

This issue is possibly a duplicate of #1205 but with more details and workaround.

To Reproduce
Steps to reproduce the behavior:

  1. Set "type": "module", in package.json
  2. Add
    import { setup, defaultClient } from 'applicationinsights';
    setup().start();
    
    console.log(defaultClient); 
  3. Run the app and check the console output, defaultClient would be undefined.

Expected behavior
defaultClient should be fulfilled

Additional context
This happened because of the differences how native ESM and transpiled to CJS modules works.

If you look to the entry point of the 'applicationinsights' package you will see:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

exports.dispose =
  exports.TelemetryClient =
    exports.DistributedTracingModes =
      exports.Contracts = exports.defaultClient =
        exports.Configuration = exports.wrapWithCorrelationContext =
          exports.startOperation = exports.getCorrelationContext = exports.start = exports.setup = void 0;


var applicationinsights_1 = require("./src/shim/applicationinsights");
Object.defineProperty(exports, "setup", { enumerable: true, get: function () { return applicationinsights_1.setup; } });
Object.defineProperty(exports, "defaultClient", { enumerable: true, get: function () { return applicationinsights_1.defaultClient; } });
// ... other exports

When this package would be requested in CJS context, those getters would be invoked only when user's code will actually access them, but in ESM context, ESM loader enumerate all properties which are exported from the package and save result internally, so getter for defaultClient is executed only once and before this client ever initialized.

There is no simple way to fix this, other than don't use late-binding at all. So instead of defaultClient expose a function getDefaultClient()

To workaround the issue, i created a cjs file which is called from my ESM modules with the following content:

// telemetry.cjs
const applicationinsights = require('applicationinsights');
applicationinsights.setup().start();

module.exports = {
  telemetryClient: applicationinsights.defaultClient
}
@timofei-iatsenko timofei-iatsenko changed the title pplicationinsights package incompatibilities with ESM aplicationinsights package incompatibilities with ESM Jul 2, 2024
@zhiyuanliang-ms
Copy link
Contributor

zhiyuanliang-ms commented Aug 30, 2024

@thekip

I also found that applicationinsights v3 doesn't work well with ESM.

For your issue, I found the following code works for me, the defaultClient will not be undefined.

import applicationInsights from "applicationinsights";

applicationInsights.setup(connectionString)
  .setSendLiveMetrics(true)
  .start();

console.log(applicationInsights.defaultClient === undefined) // defaultClient won't be undefined
applicationInsights.defaultClient.trackEvent({name: "TestEvent"}); // this line works but it will give me some warning/error log

But I will get the following log which is caused by the trackEvent call:

Accessing resource attributes before async attributes settled []

I can get the custom event in the application insights, but this log seems a bug to me and it's quite annoying.

I am using version 3.2.2.

@JacksonWeber
Copy link
Contributor

@zhiyuanliang-ms Unfortunately that log is something that OpenTelemetry throws when we initialize before letting the resource detector's attempt to determine if the SDK is running in a VM. I'll look into how to resolve that log, however it should have no impact on your ability to receive telemetry.

@JacksonWeber
Copy link
Contributor

@thekip Solution to this issue presented by the OTel community is in the discussion on #1375 just FYI in case you run into issues with automatically collecting telemetry from HTTP, DBs, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants