question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consider accessing ApplicationInsights instance via import

See original GitHub issue

Currently the recommended way to access to access the ApplicationInsights instance seems to be to set it as a global, if I want to access it from another module, or to pass it to every module that needs it.

I would love it if I could simply do something like this: index.js:

import { ApplicationInsights } from '@microsoft/applicationinsights-web';

var appInsights = new ApplicationInsights({
    config: {
        instrumentationKey: 'YOUR_INSTRUMENTATION_KEY_GOES_HERE'
    }
});
appInsights.loadAppInsights();

other-module.js:

// This imports the appInsights instance that was configured with `loadAppInsights()` in index.js
import appInsights from '@microsoft/applicationinsights-web';

// appInsights.trackX();

Ideally index.js would probably looks something like this, but this would be very breaking, so probably not possible:

import { initAppInsights } from '@microsoft/applicationinsights-web';

initAppInsights({
    config: {
        instrumentationKey: 'YOUR_INSTRUMENTATION_KEY_GOES_HERE'
    }
});

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
bryanteecommented, Oct 3, 2019

This is how I’ve gone about implementing it and seems to be good. Basically moving away from the revealing module pattern used in the demo in favor of a class singleton-ish pattern.

import { ApplicationInsights } from '@microsoft/applicationinsights-web';
import { ReactPlugin } from '@microsoft/applicationinsights-react-js';

class TelemetryService {
  reactPlugin = new ReactPlugin();
  appInsights = null;

  /**
   * Initialize the Application Insights class
   * @param {string} instrumentationKey - Application Insights Instrumentation Key
   * @param {Object} browserHistory - client's browser history, supplied by the withRouter HOC
   * @return {void}
   */
  initialize = (instrumentationKey, browserHistory) => {
    if (!instrumentationKey || !browserHistory) {
      throw new Error('Could not initialize Telemetry Service');
    }

    this.appInsights = new ApplicationInsights({
      config: {
        instrumentationKey,
        maxBatchInterval: 0,
        enableCorsCorrelation: true,
        disableFetchTracking: false,
        extensions: [this.reactPlugin],
        extensionConfig: {
          [this.reactPlugin.identifier]: {
            history: browserHistory,
          },
        },
      },
    });

    this.appInsights.loadAppInsights();
  };
}

const ai = new TelemetryService();

export { ai };

Then any module can get access to the appInsights singleton.

import { ai } from './TelemetryService'

function trackEvent(event) {
  ai.appInsights.trackEvent({ name: event });
}
0reactions
github-actions[bot]commented, Aug 31, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connection strings in Application Insights - Azure Monitor
The Application Insights JavaScript SDK requires the connection string to be passed in during initialization and configuration. It's viewable in ...
Read more >
Using Azure Application Insights to Provide Business ...
With the Application Insights instance created, access it and copy the Instrumentation Key, present in the upper right corner.
Read more >
Given an Applications Insight Instrumentation key, get the ...
Ideally, I would like to embed something so I could implement a page like 'myapp.com/appinsights' and this would give me the correct app ......
Read more >
Application Insights - Product Documentation | ServiceNow
You can access Application Insights by logging in as a user with the admin or sn_app_insights.admin role and navigating to All > Application...
Read more >
6 steps to integrate Application Insights with .Net Core ...
Net Core Web API using Visual Studio as shown below. ... The final step is to create an Application Insights instance and integrate...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found