generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 2
chore(feature): knowledge hub Telemetry Setup #68
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
Open
aakankshagupta1994
wants to merge
51
commits into
main
Choose a base branch
from
chore/renovate/telemetry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
51 commits
Select commit
Hold shift + click to select a range
ef57919
chore: telemetry setup
aakankshagupta1994 7f65078
chore: telemetry setup
aakankshagupta1994 fd152cf
chore: telemetry changes for tutorials
aakankshagupta1994 ed457a0
chore: lint changes
aakankshagupta1994 e8cb3e4
chore: removed action-map file
aakankshagupta1994 bf12dfc
chore: updated pipeline.yml
aakankshagupta1994 b84b920
chore: open tutorial broswer-telemetry.ts file
aakankshagupta1994 84a8b53
chore: updated telemetry properties action
aakankshagupta1994 a4915f4
chore: test telemetry changes
aakankshagupta1994 39927bd
chore: removed apihost from package.nls.json
aakankshagupta1994 930a773
Merge remote-tracking branch 'origin/main' into chore/renovate/telemetry
aakankshagupta1994 ff96ded
chore: added logic for recording blogs
aakankshagupta1994 b013be5
chore: lint fixes
aakankshagupta1994 eab2f0c
chore: removed dead code
aakankshagupta1994 d8def10
chore: updated test-cases
aakankshagupta1994 172b57f
chore: added test cases
aakankshagupta1994 98d06ca
Merge remote-tracking branch 'origin/main' into chore/renovate/telemetry
aakankshagupta1994 59384cf
chore: updated review comments
aakankshagupta1994 4d67662
chore: other type error fixes
aakankshagupta1994 2ed54ac
chore: lint fix
aakankshagupta1994 3777116
chore: added test cases
aakankshagupta1994 b0ce881
chore: review comments fix
aakankshagupta1994 4bbaa2c
chore: secret logic from .env
aakankshagupta1994 1bc47dc
chore: lint fix
aakankshagupta1994 2f7e18b
Merge remote-tracking branch 'origin/main' into chore/renovate/telemetry
aakankshagupta1994 014783a
chore: updated test-cases
aakankshagupta1994 341b4d3
chore: sonar big fix
aakankshagupta1994 a1653e6
chore: update dotenv support for telemetry
kranthie-sap ff43be0
Merge remote-tracking branch 'origin/main' into chore/renovate/telemetry
aakankshagupta1994 b284716
Merge branch 'chore/renovate/telemetry' of https://github.com/SAP/kno…
aakankshagupta1994 1726910
chore: updated test-cases
aakankshagupta1994 c63a84c
chore: review comments fix
aakankshagupta1994 20d1322
chore: installed and updated uuid path
aakankshagupta1994 cb17d75
chore: lint fix
aakankshagupta1994 3a2cd31
chore: review comments
aakankshagupta1994 e2af57a
chore: lint fix
aakankshagupta1994 d8e4361
chore: debug texts
aakankshagupta1994 5869b17
chore: debug texts
aakankshagupta1994 1efd06e
chore: removed debug texts
aakankshagupta1994 17ccb55
chore: lint fix
aakankshagupta1994 189ae42
chore: added test cases
aakankshagupta1994 1727a91
chore: added test cases
aakankshagupta1994 98aaa65
feat: refactoring telemetry logic
aakankshagupta1994 7df3f78
Merge remote-tracking branch 'origin/main' into chore/renovate/telemetry
aakankshagupta1994 33c2d07
feat: lint fixes
aakankshagupta1994 cded02d
feat: updated pnpm-lock file
aakankshagupta1994 3c23be5
feat: lint fix
aakankshagupta1994 a088ae2
feat: increased test-coverage
aakankshagupta1994 204b4e0
feat: review comments
aakankshagupta1994 2818afd
Merge remote-tracking branch 'origin/main' into chore/renovate/telemetry
aakankshagupta1994 f373855
feat: lint fix
aakankshagupta1994 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
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
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
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
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
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"knowledgeHub.displayName": "Knowledge Hub extension by SAP", | ||
"knowledgeHub.description": "Knowledge Hub extension by SAP", | ||
"configuration.telemetryEnabled": "If enabled, telemetry events to improve extension quality will be recorded", | ||
"commands.knowledgeHub.category": "SAP", | ||
"commands.knowledgeHub.openKnowledgeHub.title": "Open Knowledge Hub" | ||
} |
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
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
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,23 @@ | ||
import type { LogTelemetryEvent } from '@sap/knowledge-hub-extension-types'; | ||
|
||
import { KHUB_OPEN_BLOGS, KHUB_OPEN_TUTORIAL } from '@sap/knowledge-hub-extension-types'; | ||
import type { TelemetryUIOpenBlogProps, TelemetryUIOpenTutorialProps } from './types'; | ||
|
||
/** | ||
* Map redux action -> telemetry event properties | ||
* Requires respective redux action to be in allowedTelemetryActions in packages/webapp/src/webview/state/middleware.ts | ||
*/ | ||
export const actionMap: { | ||
[action: string]: (action: LogTelemetryEvent) => any; | ||
} = { | ||
[KHUB_OPEN_TUTORIAL]: (action: LogTelemetryEvent): TelemetryUIOpenTutorialProps => ({ | ||
action: 'OPEN_TUTORIAL', | ||
title: action.payload?.title as string, | ||
primaryTag: action.payload?.primaryTag as string | ||
}), | ||
[KHUB_OPEN_BLOGS]: (action: LogTelemetryEvent): TelemetryUIOpenBlogProps => ({ | ||
action: 'OPEN_BLOG', | ||
title: action.payload?.title as string, | ||
primaryTag: action.payload?.primaryTag as 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 @@ | ||
export { initTelemetry, setCommonProperties, trackAction, trackEvent } from './telemetry'; |
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,138 @@ | ||
import { platform, arch, release } from 'os'; | ||
import { env, workspace } from 'vscode'; | ||
import type { Disposable } from 'vscode'; | ||
import { TelemetryClient } from 'applicationinsights'; | ||
import { logString } from '../logger/logger'; | ||
import packageJson from '../../package.json'; | ||
import type { TelemetryEvent, TelemetryReporter } from './types'; | ||
import { config as dotenvConfig } from 'dotenv'; | ||
import { join } from 'path'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { actionMap } from './action-map'; | ||
|
||
// Telemetry reporter client | ||
let reporter: TelemetryReporter | undefined; | ||
|
||
/** | ||
* Initialize telemetry. | ||
* | ||
* @returns - telemetry reporter | ||
*/ | ||
export function initTelemetry(): TelemetryReporter { | ||
if (!reporter) { | ||
dotenvConfig({ path: join(__dirname, '../', `.env`) }); | ||
const instrumentationKey = process.env.KHE_TELEMETRY_INSTRUMENTATION_KEY; | ||
if (!instrumentationKey) { | ||
logString('Instrumentation key missing in .env file'); | ||
} | ||
const enabled = updateTelemetryStatus(); | ||
const client = new TelemetryClient(instrumentationKey); | ||
client.channel.setUseDiskRetryCaching(true); | ||
client.context.tags[client.context.keys.userId] = env.machineId; | ||
client.context.tags[client.context.keys.sessionId] = uuidv4(); | ||
client.context.tags[client.context.keys.cloudRole] = env.appName; | ||
|
||
const disposables: Disposable[] = []; | ||
disposables.push(workspace.onDidChangeConfiguration(() => updateTelemetryStatus())); | ||
reporter = { | ||
client, | ||
enabled, | ||
dispose: () => { | ||
disposables.forEach((d) => d.dispose()); | ||
reporter = undefined; | ||
} | ||
}; | ||
} | ||
return reporter; | ||
} | ||
|
||
/** | ||
* Update the telemetry setting by reading configuration. | ||
* | ||
* @returns - status of telemetry setting, true: enabled; false: disabled | ||
*/ | ||
function updateTelemetryStatus(): boolean { | ||
const enabled = !!workspace.getConfiguration('sap.ux.knowledgeHub').get('telemetryEnabled'); | ||
if (reporter) { | ||
reporter.enabled = enabled; | ||
} | ||
return enabled; | ||
} | ||
|
||
/** | ||
* Set common properties which will be added to every telemetry event. | ||
* If called without properties, all common properties are removed. | ||
* | ||
* @param properties - name/value pair of properties (optional) | ||
* @param properties.ide - development environment VSCODE or SBAS | ||
* @param properties.sbasdevSpace - SBAS devspace | ||
*/ | ||
export function setCommonProperties(properties?: { ide: 'VSCODE' | 'SBAS'; sbasdevSpace: string }) { | ||
if (reporter) { | ||
reporter.commonProperties = properties | ||
? { | ||
'cmn.appstudio': properties.ide === 'SBAS' ? 'true' : 'false', | ||
'cmn.devspace': properties.sbasdevSpace, | ||
'common.os': platform(), | ||
'common.nodeArch': arch(), | ||
'common.platformversion': (release() || '').replace(/^(\d+)(\.\d+)?(\.\d+)?(.*)/, '$1$2$3'), | ||
'common.extname': packageJson.name, | ||
'common.extversion': packageJson.version | ||
aakankshagupta1994 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
: undefined; | ||
} | ||
} | ||
|
||
/** | ||
* Track an even using telemetry. | ||
* | ||
* @param event - telemetry event | ||
*/ | ||
export async function trackEvent(event: TelemetryEvent): Promise<void> { | ||
if (!reporter?.enabled) { | ||
return; | ||
} | ||
try { | ||
const name = `${packageJson.name}/${event.name}`; | ||
const properties = propertyValuesToString({ ...event.properties, ...(reporter.commonProperties ?? {}) }); | ||
reporter.client.trackEvent({ name, properties }); | ||
} catch (error) { | ||
logString(`Error sending telemetry event '${event.name}': ${(error as Error).message}`); | ||
} | ||
} | ||
|
||
/** | ||
* Map specified redux actions to to telemetry events and track them. | ||
* | ||
* @param action - action that occurred | ||
*/ | ||
export async function trackAction(action: any): Promise<void> { | ||
if (!reporter?.enabled) { | ||
return; | ||
} | ||
try { | ||
if (actionMap[action.payload.action.type]) { | ||
const properties = actionMap[action.payload.action.type](action.payload.action); | ||
await trackEvent({ name: 'USER_INTERACTION', properties }); | ||
} | ||
} catch (error) { | ||
logString(`Error sending telemetry action '${action?.payload?.action?.type}': ${(error as Error).message}`); | ||
} | ||
} | ||
|
||
/** | ||
* Ensure all property values are strings. While type TelemetryEventProperties defines the values | ||
* as string | number | any, the call LogTelemetryEvent() throws an exception if a non-string | ||
* property value is passed. | ||
* | ||
* @param properties - key/value map of properties | ||
* @returns - key/value map where all values are strings | ||
*/ | ||
function propertyValuesToString(properties: { [key: string]: any }): { [key: string]: string } { | ||
for (const property in properties) { | ||
if (typeof properties[property] !== 'string') { | ||
properties[property] = properties[property].toString(); | ||
} | ||
} | ||
return properties; | ||
} |
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.