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

Only allow initialization of Agent through env variables #960

Merged
merged 5 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions Bootstrap/Default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export function setStatusLogger(statusLogger: StatusLogger) {

/**
* Try to setup and start this app insights instance if attach is enabled.
* @param setupString connection string
* @param aadTokenCredential Optional AAD credential
*/
export function setupAndStart(setupString?: string, aadTokenCredential?: azureCore.TokenCredential): typeof types | null {
export function setupAndStart(aadTokenCredential?: azureCore.TokenCredential): typeof types | null {
// If app already contains SDK, skip agent attach
if (!forceStart && Helpers.sdkAlreadyExists(_logger)) {
_statusLogger.logStatus({
Expand All @@ -55,8 +55,7 @@ export function setupAndStart(setupString?: string, aadTokenCredential?: azureCo
})
return null;
}

if (!setupString) {
if (!defaultConfig.instrumentationKey) {
const message = "Application Insights wanted to be started, but no Connection String was provided";
_logger.logError(message);
_statusLogger.logStatus({
Expand Down Expand Up @@ -97,7 +96,7 @@ export function setupAndStart(setupString?: string, aadTokenCredential?: azureCo
}

// Instrument the SDK
_appInsights.setup(setupString).setSendLiveMetrics(true);
_appInsights.setup().setSendLiveMetrics(true);
_appInsights.defaultClient.setAutoPopulateAzureProperties(true);
_appInsights.defaultClient.addTelemetryProcessor(prefixInternalSdkVersion);
_appInsights.defaultClient.addTelemetryProcessor(copyOverPrefixInternalSdkVersionToHeartBeatMetric);
Expand All @@ -114,7 +113,7 @@ export function setupAndStart(setupString?: string, aadTokenCredential?: azureCo
}

// Agent successfully instrumented the SDK
_logger.logMessage("Application Insights was started with setupString: " + setupString);
_logger.logMessage("Application Insights was started");
_statusLogger.logStatus({
...StatusLogger.DEFAULT_STATUS,
AgentInitializedSuccessfully: true
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ separately from clients created with `new appInsights.TelemetryClient()`.
| aadTokenCredential| Azure Credential instance to be used to authenticate the App. [AAD Identity Credential Classes](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/identity/identity#credential-classes)
| enableAutoWebSnippetInjection(Preview)| Sets the state of automatic web snippet injection (disabled by default). If true, web snippet will be injected into valid node server http response automatically | |

[Config.ts]: https://github.com/microsoft/ApplicationInsights-node.js/blob/develop/Library/Config.ts
[Config.ts]: https://github.com/microsoft/ApplicationInsights-node.js/blob/develop/Library/Config.ts

All these properties except httpAgent, httpsAgent and aadTokenCredential could be configured using configuration file `applicationinsights.json` located under root folder of applicationinsights package installation folder, Ex: `node_modules/applicationinsights`. These configuration values will be applied to all TelemetryClients created in the SDK.

Expand Down
8 changes: 4 additions & 4 deletions Tests/Bootstrap/Default.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ describe("#setupAndStart()", () => {
// Test
const Default = require("../../Bootstrap/Default") as typeof DefaultTypes;
Default.setLogger(new DiagnosticLogger(logger));
const instance1 = Default.setupAndStart("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333");
const instance1 = Default.setupAndStart();
assert.ok(instance1.defaultClient);
const instance2 = Default.setupAndStart("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333");
const instance2 = Default.setupAndStart();
assert.deepEqual(instance1.defaultClient, instance2.defaultClient);
assert.deepEqual(instance1.defaultClient["_telemetryProcessors"].length, 2)
assert.deepEqual(instance2.defaultClient["_telemetryProcessors"].length, 2)
Expand All @@ -76,7 +76,7 @@ describe("#setupAndStart()", () => {
// Test
const Default = require("../../Bootstrap/Default") as typeof DefaultTypes;
Default.setLogger(new DiagnosticLogger(logger));
const instance = Default.setupAndStart("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333");
const instance = Default.setupAndStart();
assert.deepEqual(instance, appInsights);

// Cleanup
Expand All @@ -90,7 +90,7 @@ describe("#setupAndStart()", () => {
assert.equal(logger.errorCount, 0);
});

it("should not setup and start the SDK if no setupString is provided", () => {
it("should not setup and start the SDK if no connectionString is provided", () => {
// Setup
const logger = new LoggerSpy();
const env = <{ [id: string]: string }>{};
Expand Down