Skip to content
This repository has been archived by the owner on May 22, 2019. It is now read-only.

Implementation if no instrumentation key provided #35

Open
PaulInglis opened this issue Jan 21, 2019 · 4 comments
Open

Implementation if no instrumentation key provided #35

PaulInglis opened this issue Jan 21, 2019 · 4 comments
Labels

Comments

@PaulInglis
Copy link

Hello,

This is more a question than an issue.

We have (as I'm sure others will) the situation where our product will be deployed to various SDLC environments and for some of these environments (e.g. localhost or load testing) we do not wish App Insights to be recorded....or more correctly, we won't assign an instrumentation key (IK) in the environment variables.

What is the best way to handle this? Simply not call the init function if no IK is found? If so, how does that affect withTracking or calls?
Should we have 2 different component exports based on whether we have an IK existing or not?

Any other guidance around best practice for this would be appreciated.

@pviotti
Copy link
Contributor

pviotti commented Jan 22, 2019

react-appinsights is fairly transparent on the configuration of App Insights: it just builds an internal instance by passing the configuration that you feed to the init method.
App Insights itself throws an error in the constructor if it doesn't find the instrumentation key. So all the subsequent calls to withTracking and trackPageView would fail with undefined has not xyz property error if you initialize react-appinsights without IK, or with IK as empty string IK.
So the suggested practice here is to use a non-empty string as IK for environments in which you don't care sending telemetry back to App Insights.

As a reminder, another best practice about using different environments is to use different AppInsights resources (thus, different instrumentation keys), as explained here.

(I'll keep this open until we document this practice better and write some tests in the upcoming v3, which supports the new AppInsights SDK)

@mikegoatly
Copy link

@pviotti Is there a recommended way to pass the instrumentation key to the client initialization? As you mentioned, it's good practice to have a different key per environment, so it would need to be passed to dynamically provided in some form or another.

@PaulInglis
Copy link
Author

I did it with

const instrumentationKey: string = process.env.REACT_APP_INSTRUMENTATION_KEY || 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx';

And then pass that variable into the initialise function

@pviotti
Copy link
Contributor

pviotti commented Feb 6, 2019

There's no recommended way of storing and retrieving the instrumentation key: it depends on where you decide to store such information. I'd say environment variables are good enough for this. This blog post gives an overview of how to use them in React.

Ref: #microsoft/ApplicationInsights-JS#281

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

No branches or pull requests

3 participants