-
Notifications
You must be signed in to change notification settings - Fork 255
[main] [snippet] Integrate 1DS with AI Snippet Generation #2428
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a70b101
snippet works
siyuniu-ms 0af349b
work for both ai and 1ds
siyuniu-ms a3ab91f
rollup seperately
siyuniu-ms 6865bdc
Update snippet-config.js
siyuniu-ms 4779893
remove type
siyuniu-ms d1a60c0
oneds do not need fields
siyuniu-ms 9559bcb
Update package.json
siyuniu-ms 879d6b5
Update package.json
siyuniu-ms 816ca1f
Update snippet.ts
siyuniu-ms fddadda
Merge branch 'main' into siyu/snippet1DS
siyuniu-ms 23fae28
readable
siyuniu-ms 99c6dbe
Update snippet.ts
siyuniu-ms 158fa7f
Merge branch 'main' into siyu/snippet1DS
siyuniu-ms d088fe6
Merge branch 'main' into siyu/snippet1DS
siyuniu-ms 2ed9b02
seperate two file
siyuniu-ms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| src/aiSnippet.ts | ||
| src/oneDSSnippet.ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // createFile.js | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| // Define the path to the snippet.js file | ||
| const filePath = path.join(__dirname, 'src/snippet.ts'); // Adjust the path if necessary | ||
| const aiFilePath = path.join(__dirname, 'src/aiSnippet.ts'); | ||
| const oneDSFilePath = path.join(__dirname, 'src/oneDSSnippet.ts'); | ||
| // Read the snippet.js file | ||
| fs.readFile(filePath, 'utf8', (err, data) => { | ||
| if (err) { | ||
| console.error('Error reading file:', err); | ||
| process.exit(1); | ||
| } | ||
|
|
||
|
|
||
| let remove = data.replace(/let\s+isOneDS\s*=\s*\w+;/i, ''); | ||
| // Check if the replace did anything | ||
| if (remove === data) { | ||
| // If no change was made, throw an error to break the build | ||
| throw new Error("Failed to remove 'let isOneDS' assignment. Possible content change."); | ||
| } | ||
|
|
||
| // Replace all occurrences of "checkplace" with "true" | ||
| let ai = remove.replace(/isOneDS/g, 'false'); | ||
| ai = "// DO NOT EDIT - Automatically Generated\n" + ai; | ||
|
|
||
| // Write the modified content back to the file (or a new file if you prefer) | ||
| fs.writeFile(aiFilePath, ai, 'utf8', (err) => { | ||
| if (err) { | ||
| console.error('Error writing file:', err); | ||
| process.exit(1); | ||
| } | ||
| console.log('New File created successfully:', aiFilePath); | ||
| }); | ||
|
|
||
| let oneDS = remove.replace(/isOneDS/g, 'true'); | ||
| oneDS = "// DO NOT EDIT - Automatically Generated\n" + oneDS; | ||
| fs.writeFile(oneDSFilePath, oneDS, 'utf8', (err) => { | ||
| if (err) { | ||
| console.error('Error writing file:', err); | ||
| process.exit(1); | ||
| } | ||
| console.log('New File created successfully:', oneDSFilePath); | ||
| }); | ||
| } | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
tools/applicationinsights-web-snippet/rollupOneDSSnippet.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { createUnVersionedConfig } from "../../rollup.base.config"; | ||
|
|
||
| const snippetOutputName = "oneDSSnippet"; | ||
| const snippetOutputPath = "../../build/output/oneDSSnippet"; | ||
|
|
||
| export default createUnVersionedConfig("", | ||
| { | ||
| namespace: "oneDS", | ||
| version: "", | ||
| browser: { | ||
| entryPoint: snippetOutputName, | ||
| outputName: snippetOutputPath, | ||
| inputPath: "build/output", | ||
| formats: [{ format: 'cjs', postfix: '', useStrict: false, topLevel: true }], | ||
| }, | ||
| }, | ||
| [ "applicationinsights-web-snippet" ], | ||
| false | ||
| ); |
104 changes: 104 additions & 0 deletions
104
tools/applicationinsights-web-snippet/src/1dsSupport.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { oneDsEnvelope } from "./1dsType"; | ||
| import { _getTime } from "./support"; | ||
|
|
||
|
|
||
| export function _createOneDsEnvelope(iKey: string, theType: string, _epoch: number, _sequence: number, sv: string): oneDsEnvelope { | ||
| let UInt32Mask = 0x100000000; | ||
| if (_epoch === 0) { | ||
| _epoch = Math.floor((UInt32Mask * Math.random()) | 0) >>> 0; | ||
| } | ||
| let envelope: oneDsEnvelope = { | ||
| data: { | ||
| baseData: { | ||
| ver: 2 | ||
| } | ||
| }, | ||
| ext: { | ||
| app: { sesId: "0000" }, | ||
| intweb: {}, | ||
| sdk: { | ||
| ver: "javascript:" + sv, | ||
| epoch: "" + _epoch, | ||
| seq: _sequence++ | ||
| }, | ||
| utc: { | ||
| popSample: 100 | ||
| }, | ||
| web: { | ||
| userConsent: false | ||
| } | ||
| }, | ||
| time: _getTime(), | ||
| iKey: "o:" + _getTenantId(iKey), | ||
| name: theType, | ||
| ver: "4.0" | ||
| }; | ||
| _addTimeZone(envelope); | ||
| _addUser(envelope); | ||
| return envelope; | ||
| } | ||
|
|
||
| function _getTenantId(apiKey: string): string { | ||
| let result: string = ""; | ||
|
|
||
| if (apiKey) { | ||
| const indexTenantId: number = apiKey.indexOf("-"); | ||
| if (indexTenantId > -1) { | ||
| result = apiKey.substring(0, indexTenantId); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function _addTimeZone(envelope: any): void { | ||
| // Add time zone | ||
| const timeZone: number = new Date().getTimezoneOffset(); | ||
| let minutes: number = timeZone % 60; | ||
| let hours: number = (timeZone - minutes) / 60; | ||
| let timeZonePrefix: string = "+"; | ||
| if (hours > 0) { | ||
| timeZonePrefix = "-"; | ||
| } | ||
| hours = Math.abs(hours); | ||
| minutes = Math.abs(minutes); | ||
|
|
||
| envelope.ext = envelope.ext || {}; | ||
| envelope.ext.loc = { // Add time zone | ||
| tz: timeZonePrefix + (hours < 10 ? "0" + hours : hours.toString()) + ":" + (minutes < 10 ? "0" + minutes : minutes.toString()) | ||
| }; | ||
| } | ||
|
|
||
| function _addUser(envelope: any): void { | ||
| const strUndefined: string = "undefined"; | ||
| // Add user language | ||
| if (typeof navigator !== strUndefined) { | ||
| const nav: Navigator & { userLanguage?: string } = navigator; | ||
| envelope.ext = envelope.ext || {}; | ||
| envelope.ext.user = { | ||
| locale: nav.userLanguage || nav.language | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| let track = "track"; | ||
| let trackPage = "TrackPage"; | ||
| let capturePage = "capturePage"; | ||
|
|
||
|
|
||
| export const oneDsMethods = [ | ||
| track + "Event", | ||
| track + "Exception", | ||
| trackPage + "View", | ||
| trackPage + "ViewPerformance", | ||
| "addTelemetryInitializer", | ||
| track, | ||
| trackPage + "Action", | ||
| track + "ContentUpdate", | ||
| trackPage + "Unload", | ||
| capturePage + "View", | ||
| capturePage + "ViewPerformance", | ||
| capturePage + "Action", | ||
| capturePage + "Unload", | ||
| "captureContentUpdate" | ||
| ] | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export interface oneDsEnvelope { | ||
| data: any; | ||
| ext: any; | ||
| time: string; | ||
| iKey: string; | ||
| name: string; | ||
| ver: string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { IEnvelope } from "@microsoft/applicationinsights-common"; | ||
| import { _getTime } from "./support"; | ||
|
|
||
| let strEmpty = ""; | ||
|
|
||
|
|
||
|
|
||
| export function _createAiEnvelope(iKey: string, theType: string, sv: string, version: number, locn: Location): IEnvelope { | ||
| let tags = {}; | ||
| let type = "Browser"; | ||
| let strAiDevice = "ai.device."; | ||
| let strAiOperationName = "ai.operation.name"; | ||
| let strAiSdkVersion = "ai.internal.sdkVersion"; | ||
| let strToLowerCase = "toLowerCase"; | ||
| tags[strAiDevice + "id"] = type[strToLowerCase](); | ||
| tags[strAiDevice + "type"] = type; | ||
| tags[strAiOperationName] = locn && locn.pathname || "_unknown_"; | ||
| tags[strAiSdkVersion] = "javascript:snippet_" + (sv || version); | ||
|
|
||
| let envelope:IEnvelope = { | ||
| time: _getTime(), | ||
| iKey: iKey, | ||
| name: "Microsoft.ApplicationInsights." + iKey.replace(/-/g, strEmpty) + "." + theType, | ||
| sampleRate: 100, | ||
| tags: tags, | ||
| data: { | ||
| baseData: { | ||
| ver: 2 | ||
| } | ||
| }, | ||
| ver: undefined, | ||
| seq: "1", | ||
| aiDataContract: undefined | ||
| }; | ||
| return envelope; | ||
| } | ||
|
|
||
| let track = "track"; | ||
| let trackPage = "TrackPage"; | ||
| let trackEvent = "TrackEvent"; | ||
|
|
||
| export const aiMethod = [ | ||
| track + "Event", | ||
| track + "Exception", | ||
| trackPage + "View", | ||
| trackPage + "ViewPerformance", | ||
| "addTelemetryInitializer", | ||
| track + "Trace", | ||
| track + "DependencyData", | ||
| track + "Metric", | ||
| "start" + trackPage, | ||
| "stop" + trackPage, | ||
| "start" + trackEvent, | ||
| "stop" + trackEvent, | ||
| "setAuthenticatedUserContext", | ||
| "clearAuthenticatedUserContext", | ||
| "flush" | ||
| ] | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.