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

[AI Light][Task]14130466: Instrumentation key API is being deprecated - need to add support #1956

Merged
merged 6 commits into from
Dec 16, 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
2 changes: 2 additions & 0 deletions AISKULight/Tests/Unit/src/aiskuliteunittests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { AISKULightSizeCheck } from "./AISKULightSize.Tests";
import { ApplicationInsightsConfigTests } from "./config.tests";

export function runTests() {
new AISKULightSizeCheck().registerTests();
new ApplicationInsightsConfigTests().registerTests();
}
183 changes: 183 additions & 0 deletions AISKULight/Tests/Unit/src/config.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import { AITestClass, Assert } from "@microsoft/ai-test-framework";
import { newId } from "@microsoft/applicationinsights-core-js";
import { ApplicationInsights} from "../../../src/index";

export class ApplicationInsightsConfigTests extends AITestClass {
private readonly _instrumentationKey = "b7170927-2d1c-44f1-acec-59f4e1751c11";
private readonly _endpoint = "endpoint"
private readonly _connectionString = `InstrumentationKey=${this._instrumentationKey};ingestionendpoint=${this._endpoint}`;
private readonly _iKey = "testKey";
private _sessionPrefix: string = newId();
static registerTests: any;

constructor(testName?: string) {
super(testName || "ApplicationInsightsAISKULightTests");
}

protected _getTestConfig(sessionPrefix: string, ikey?: boolean, cs?: boolean) {
return {
instrumentationKey: ikey? this._iKey : undefined,
connectionString: cs? this._connectionString : undefined,
namePrefix: sessionPrefix
};
}

public testInitialize() {
super.testInitialize();
}

public testCleanup() {
super.testCleanup();
}

public testFinishedCleanup(): void {
console.log("* testCleanup(" + (AITestClass.currentTestInfo ? AITestClass.currentTestInfo.name : "<null>") + ")");
}

public registerTests() {
this.addConfigTests();
this.addApiTests();
}

private addConfigTests(): void {
this.testCase({
name: "ConfigTests: ApplicationInsights config should set default endpoint",
test: () => {
let expectedConnectionString = `InstrumentationKey=${this._instrumentationKey}`
let _config = {
connectionString: expectedConnectionString,
namePrefix:this._sessionPrefix
};
Assert.ok(_config)
let ai = new ApplicationInsights(_config);
this.onDone(() =>{
ai.unload(false);
});
Assert.ok(ai, "ApplicationInsights light Instance is initialized");

let config = ai.config;
let expectedIkey = this._instrumentationKey;
let expectedEndpointUrl = "https://dc.services.visualstudio.com/v2/track";
let expectedLoggingLevel = 10000;
Assert.ok(config, "ApplicationInsights Light config exists");
Assert.equal(expectedConnectionString, config.connectionString, "connection string is set");
Assert.equal(expectedIkey, config.instrumentationKey, "ikey is set");
Assert.equal(expectedLoggingLevel, config.diagnosticLogInterval, "diagnosticLogInterval is set to 1000 by default");
Assert.equal(expectedEndpointUrl, config.endpointUrl, "endpoint url is set from connection string");
}
});

this.testCase({
name: "ConfigTests: ApplicationInsights config works correctly with connection string",
test: () => {
let _config = this._getTestConfig(this._sessionPrefix, false, true);
Assert.ok(_config)
let ai = new ApplicationInsights(_config);
this.onDone(() =>{
ai.unload(false);
});
Assert.ok(ai, "ApplicationInsights light Instance is initialized");

let config = ai.config;
let expectedIkey = this._instrumentationKey;
let expectedConnectionString = this._connectionString;
let expectedEndpointUrl = `${this._endpoint}/v2/track`;
let expectedLoggingLevel = 10000;
Assert.ok(config, "ApplicationInsights Light config exists");
Assert.equal(expectedConnectionString, config.connectionString, "connection string is set");
Assert.equal(expectedIkey, config.instrumentationKey, "ikey is set");
Assert.equal(expectedLoggingLevel, config.diagnosticLogInterval, "diagnosticLogInterval is set to 1000 by default");
Assert.equal(expectedEndpointUrl, config.endpointUrl, "endpoint url is set from connection string");
}
});

this.testCase({
name: "ConfigTests: ApplicationInsights config works correctly with connection string and Ikey",
useFakeTimers: true,
test: () => {
let _config = this._getTestConfig(this._sessionPrefix, true, true);
Assert.ok(_config)
let ai = new ApplicationInsights(_config);
this.onDone(() =>{
ai.unload(false);
});
Assert.ok(ai, "ApplicationInsights light Instance is initialized");
Assert.ok(ai);
let config = ai.config;
let expectedIkey = this._instrumentationKey;
let expectedConnectionString = this._connectionString;
let expectedEndpointUrl = `${this._endpoint}/v2/track`;
let expectedLoggingLevel = 10000;
Assert.ok(config, "ApplicationInsights Light config exists");
Assert.equal(expectedConnectionString, config.connectionString, "connection string is set");
Assert.equal(expectedIkey, config.instrumentationKey, "ikey is set from connection string");
Assert.equal(expectedLoggingLevel, config.diagnosticLogInterval, "diagnosticLogInterval is set to 1000 by default");
Assert.equal(expectedEndpointUrl, config.endpointUrl, "endpoint url is set from connection string");
}
});

this.testCase({
name: "ConfigTests: ApplicationInsights config works correctly with ikey",
useFakeTimers: true,
test: () => {
let _config = this._getTestConfig(this._sessionPrefix, true, false);
Assert.ok(_config)
let ai = new ApplicationInsights(_config);
this.onDone(() =>{
ai.unload(false);
});
Assert.ok(ai, "ApplicationInsights light Instance is initialized");
Assert.ok(ai);
let config = ai.config;
let expectedIkey = this._iKey;
let expectedLoggingLevel = 10000;
Assert.ok(config, "ApplicationInsights Light config exists");
Assert.ok(!config.connectionString, "connection string shoud not set");
Assert.equal(expectedIkey, config.instrumentationKey, "ikey is set");
Assert.equal(expectedLoggingLevel, config.diagnosticLogInterval, "diagnosticLogInterval is set to 1000 by default");
Assert.ok(!config.endpointUrl, "endpoint url should not set from ikey");
}
});

this.testCase({
name: "ConfigTests: ApplicationInsights sholuld throw error when no ikey and connection string provided",
useFakeTimers: true,
test: () => {
try {
let _config = this._getTestConfig(this._sessionPrefix, false, false);
Assert.ok(_config)
let ai = new ApplicationInsights(_config);
this.onDone(() =>{
ai.unload(false);
});
Assert.ok(false, "ApplicationInsights light Instance should not be initialized");
Assert.ok(ai);
} catch(e) {
Assert.ok(true, "error should be thrown");
}
}
});
}

public addApiTests(): void {
this.testCase({
name: "DynamicConfigTests: Public Members exist",
test: () => {
let _config = this._getTestConfig(this._sessionPrefix, true, false);
Assert.ok(_config)
let ai = new ApplicationInsights(_config);
this.onDone(() =>{
ai.unload(false);
});
Assert.ok(ai, "ApplicationInsights light Instance is initialized");
let trackMethod = "track";
let flushMethod = "flush";
Assert.ok(ai[trackMethod], `${trackMethod} method exists`);
Assert.equal("function", typeof ai["track"], `${trackMethod} is a function`);
Assert.ok(ai[flushMethod], `${flushMethod} method exists`);
Assert.equal("function", typeof ai[flushMethod], `${flushMethod} is a function`);
}
});
}

}
4 changes: 2 additions & 2 deletions AISKULight/Tests/UnitTests.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="Cache-control" content="no-Cache" />
<title>Tests for Application Insights JavaScript AISKU</title>
<title>Tests for Application Insights JavaScript AISKU Light</title>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-1.23.1.css">
<script src="http://sinonjs.org/releases/sinon-2.3.8.js"></script>
<script src="../../../common/Tests/External/require-2.2.0.js"></script>
Expand Down Expand Up @@ -43,7 +43,7 @@
// Load Channel
modules.add("@microsoft/applicationinsights-channel-js", "./node_modules/@microsoft/applicationinsights-channel-js/browser/applicationinsights-channel-js");

var testModule = modules.add("aiskuliteunittests", "./Unit/dist/aiskuliteunittests.tests.js")
var testModule = modules.add("Tests/Unit/src/aiskuliteunittests", "./Unit/dist/aiskuliteunittests.tests.js")
testModule.run = function (tests) {
console && console.log("Starting tests");
QUnit.start();
Expand Down
30 changes: 14 additions & 16 deletions AISKULight/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import dynamicProto from "@microsoft/dynamicproto-js";
import { Sender } from "@microsoft/applicationinsights-channel-js";
import { IConfig } from "@microsoft/applicationinsights-common";
import { DEFAULT_BREEZE_PATH, IConfig, parseConnectionString } from "@microsoft/applicationinsights-common";
import {
AppInsightsCore, IConfiguration, ILoadedPlugin, IPlugin, ITelemetryItem, ITelemetryPlugin, UnloadHandler, _InternalMessageId,
_eInternalMessageId, isNullOrUndefined, proxyFunctions, throwError
AppInsightsCore, IConfiguration, ILoadedPlugin, IPlugin, ITelemetryItem, ITelemetryPlugin, UnloadHandler, isNullOrUndefined,
proxyFunctions, throwError
} from "@microsoft/applicationinsights-core-js";

/**
Expand All @@ -27,12 +27,20 @@ export class ApplicationInsights {
// initialize the queue and config in case they are undefined
if (
isNullOrUndefined(config) ||
isNullOrUndefined(config.instrumentationKey)
(isNullOrUndefined(config.instrumentationKey) && isNullOrUndefined(config.connectionString))
) {
throwError("Invalid input configuration");
}

dynamicProto(ApplicationInsights, this, (_self) => {

if (config.connectionString) {
const cs = parseConnectionString(config.connectionString);
const ingest = cs.ingestionendpoint;
config.endpointUrl = ingest ? (ingest + DEFAULT_BREEZE_PATH) : config.endpointUrl; // only add /v2/track when from connectionstring
config.instrumentationKey = cs.instrumentationkey || config.instrumentationKey;
}

_self.config = config;

_initialize();
Expand All @@ -44,7 +52,7 @@ export class ApplicationInsights {
_self.config.diagnosticLogInterval && _self.config.diagnosticLogInterval > 0 ? _self.config.diagnosticLogInterval : 10000;
};
_self.getSKUDefaults();

proxyFunctions(_self, core, [
"track",
"flush",
Expand All @@ -58,17 +66,7 @@ export class ApplicationInsights {
]);

function _initialize(): void {
const extensions = [];
const appInsightsChannel: Sender = new Sender();

extensions.push(appInsightsChannel);

// initialize core
core.initialize(_self.config, extensions);

// initialize extensions
appInsightsChannel.initialize(_self.config, core, extensions);

core.initialize(_self.config, [new Sender()]);
core.pollInternalLogs();
Karlie-777 marked this conversation as resolved.
Show resolved Hide resolved
}
});
Expand Down