[main] [snippet update] add functions to set configs of snippet (#2245)

* Update applicationinsights-web-snippet.ts

* add unit test

* copy types folder also

* add interface

* Update applicationinsights-web-snippet.ts
This commit is contained in:
siyuniu-ms 2024-01-23 15:00:27 -08:00 коммит произвёл GitHub
Родитель ef9ad261d6
Коммит ee6e2f8b70
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
4 изменённых файлов: 53 добавлений и 3 удалений

Просмотреть файл

@ -782,7 +782,12 @@ module.exports = function (grunt) {
{ src: "./tools/applicationinsights-web-snippet/build/output/snippet.js.map", dest: `./tools/applicationinsights-web-snippet/dist-es5/snippet.js.map` },
{ src: "./tools/applicationinsights-web-snippet/build/output/snippet.min.js", dest: `./tools/applicationinsights-web-snippet/dist-es5/snippet.min.js` },
{ src: "./tools/applicationinsights-web-snippet/build/output/snippet.min.js.map", dest: `./tools/applicationinsights-web-snippet/dist-es5/snippet.min.js.map` },
]
{
expand: true,
cwd: "./tools/applicationinsights-web-snippet/build/types/",
src: ["**/*"],
dest: "./tools/applicationinsights-web-snippet/types/"
} ]
},
"web-snippet": {

Просмотреть файл

@ -1,5 +1,5 @@
import { AITestClass } from "@microsoft/ai-test-framework";
import { webSnippetVersion, webSnippet, webSnippetCs } from "../../../dist-es5/applicationinsights-web-snippet";
import { webSnippetVersion, webSnippet, webSnippetCs, getSdkLoaderScript } from "../../../dist-es5/applicationinsights-web-snippet";
export class SnippetTests extends AITestClass {
@ -39,5 +39,31 @@ export class SnippetTests extends AITestClass {
QUnit.assert.equal(-1, theSnippet.indexOf("\"InstrumentationKey=INSTRUMENTATION_KEY\""), "The Snippet contains \"InstrumentationKey=INSTRUMENTATION_KEY\"");
}
});
this.testCase({
name: "Check substituteInstrumentationKey",
test: () => {
let key = "814a172a-xxxx-4950-9023-9cf13bb65696";
let config = {instrumentationKey: key};
let theSnippet = getSdkLoaderScript(config);
console.log(theSnippet);
QUnit.assert.equal(-1, theSnippet.indexOf("InstrumentationKey"), "The Snippet does not contains InstrumentationKey");
QUnit.assert.equal(-1, theSnippet.indexOf("YOUR_CONNECTION_STRING"), "The Snippet should not contain YOUR_CONNECTION_STRING");
QUnit.assert.notEqual(-1, theSnippet.indexOf(key), "key is injected");
}
});
this.testCase({
name: "Check substituteConnectionString",
test: () => {
let key = "InstrumentationKey=814a172a-92fd-4950-9023-9cf13bb65696;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/";
let config = {connectionString: key};
let theSnippet = getSdkLoaderScript(config);
console.log(theSnippet);
QUnit.assert.notEqual(-1, theSnippet.indexOf("InstrumentationKey"), "The Snippet contains InstrumentationKey");
QUnit.assert.equal(-1, theSnippet.indexOf("YOUR_CONNECTION_STRING"), "The Snippet should not contain YOUR_CONNECTION_STRING");
QUnit.assert.notEqual(-1, theSnippet.indexOf(key), "key is injected");
}
});
}
}

Просмотреть файл

@ -1,3 +1,5 @@
import { SdkLoaderConfig } from "./type";
const webSnippet = "##replaceIKeySnippet##";
const webSnippetCs = "##replaceConnStringSnippet##";
@ -10,4 +12,15 @@ function webSnippetVersion() {
return "";
}
export { webSnippet, webSnippetCs, webSnippetVersion }
function getSdkLoaderScript(config: SdkLoaderConfig) {
let snippet: string = webSnippetCs;
if (config && config.connectionString) {
snippet = webSnippetCs.replace("YOUR_CONNECTION_STRING", config.connectionString);
} else if (config && config.instrumentationKey) {
snippet = webSnippet.replace("InstrumentationKey=INSTRUMENTATION_KEY", config.instrumentationKey);
}
return snippet;
}
export { webSnippet, webSnippetCs, webSnippetVersion, getSdkLoaderScript }

Просмотреть файл

@ -1,5 +1,11 @@
import { IConfiguration } from "@microsoft/applicationinsights-web";
// in the future, we can add more fields here
export interface SdkLoaderConfig {
instrumentationKey?: string;
connectionString?: string;
}
export interface ISnippetConfig {
src: string;
name?: string;