-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat: telemetry events added #17
Open
gagandeepSf
wants to merge
4
commits into
master
Choose a base branch
from
featTelemetry
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
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
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -13,6 +13,7 @@ import { Logger } from './logger'; | |
import { MarkdownEngine } from './markdownEngine'; | ||
import { getMarkdownExtensionContributions } from './markdownExtensions'; | ||
import { ContentSecurityPolicyArbiter, ExtensionContentSecurityPolicyArbiter, PreviewSecuritySelector } from './security'; | ||
import { telemetryService } from './telemetry'; | ||
|
||
function registerMarkdownLanguageFeatures( | ||
): vscode.Disposable { | ||
|
@@ -48,7 +49,14 @@ function registerMarkdownCommands( | |
return commandManager; | ||
} | ||
|
||
export function activate(context: vscode.ExtensionContext) { | ||
export async function activate(context: vscode.ExtensionContext) { | ||
const extensionHRStart = process.hrtime(); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const { name, aiKey, version } = require(context.asAbsolutePath( | ||
'./package.json' | ||
)); | ||
const machineId = vscode.env ? vscode.env.machineId : 'someValue.machineId'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
await telemetryService.initializeService(context, name, aiKey, version,machineId); | ||
vscode.commands.executeCommand('setContext', 'markdownPreviewFocus', false); | ||
|
||
const contributions = getMarkdownExtensionContributions(context); | ||
|
@@ -69,5 +77,12 @@ export function activate(context: vscode.ExtensionContext) { | |
logger.updateConfiguration(); | ||
previewManager.updateConfiguration(); | ||
})); | ||
telemetryService.sendExtensionActivationEvent(extensionHRStart); | ||
} | ||
|
||
export function deactivate(): void { | ||
// Send metric data. | ||
telemetryService.sendExtensionDeactivationEvent(); | ||
telemetryService.dispose(); | ||
} | ||
|
This file contains 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,10 @@ | ||
|
||
/* | ||
* Copyright (c) 2021, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
import { TelemetryService } from './telemetry'; | ||
|
||
export const telemetryService = TelemetryService.getInstance(); |
This file contains 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,152 @@ | ||
/* | ||
* Copyright (c) 2021, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import * as util from 'util'; | ||
import { ExtensionContext, workspace } from 'vscode'; | ||
import { TelemetryReporter } from './telemetryReporter'; | ||
import { CommandMetric, Measurements, Properties } from './types'; | ||
|
||
export class TelemetryService { | ||
private static instance: TelemetryService; | ||
private context: ExtensionContext | undefined; | ||
private reporter: TelemetryReporter | undefined; | ||
private aiKey = ''; | ||
private version = ''; | ||
private extensionName = 'unknown'; | ||
|
||
public static getInstance(): TelemetryService { | ||
if (!TelemetryService.instance) { | ||
TelemetryService.instance = new TelemetryService(); | ||
} | ||
return TelemetryService.instance; | ||
} | ||
|
||
/** | ||
* Initialize Telemetry Service during extension activation. | ||
* @param context extension context | ||
* @param extensionName extension name | ||
*/ | ||
|
||
public async initializeService( | ||
context: ExtensionContext, | ||
extensionName: string, | ||
aiKey: string, | ||
version: string, | ||
machineId: string | ||
): Promise<void> { | ||
this.context = context; | ||
this.extensionName = extensionName; | ||
this.aiKey = aiKey; | ||
this.version = version; | ||
|
||
const isDevMode = machineId === 'someValue.machineId'; | ||
|
||
// TelemetryReporter is not initialized if user has disabled telemetry setting. | ||
if ( | ||
this.reporter === undefined && | ||
this.isTelemetryExtensionConfigurationEnabled() && | ||
!isDevMode | ||
) { | ||
try { | ||
this.reporter = new TelemetryReporter( | ||
'salesforce-docs-markdown-preview', | ||
this.version, | ||
this.aiKey, | ||
true | ||
); | ||
this.context.subscriptions.push(this.reporter); | ||
} catch (e) { | ||
console.error( | ||
`Error initializing telemetry on Salesforce Docs Markdown extension: ${e.message}` | ||
); | ||
} | ||
} | ||
} | ||
|
||
public getReporter(): TelemetryReporter | undefined { | ||
return this.reporter; | ||
} | ||
|
||
public isTelemetryExtensionConfigurationEnabled(): boolean { | ||
return ( | ||
workspace | ||
.getConfiguration('telemetry') | ||
.get<boolean>('enableTelemetry', true) && | ||
workspace | ||
.getConfiguration('salesforce-docs-markdown-preview') | ||
.get<boolean>('enableTelemetry', true) | ||
); | ||
} | ||
|
||
public sendExtensionActivationEvent(hrstart: [number, number]): void { | ||
const startupTime = this.getEndHRTime(hrstart); | ||
this.reporter?.sendTelemetryEvent( | ||
'activationEvent', | ||
{ | ||
extensionName: this.extensionName, | ||
}, | ||
{ startupTime } | ||
); | ||
} | ||
|
||
public sendExtensionDeactivationEvent(): void { | ||
this.reporter?.sendTelemetryEvent('deactivationEvent', { | ||
extensionName: this.extensionName, | ||
}); | ||
} | ||
|
||
public sendCommandEvent( | ||
commandName?: string, | ||
hrstart?: [number, number], | ||
properties?: Properties, | ||
measurements?: Measurements | ||
): void { | ||
if (commandName) { | ||
const baseProperties: CommandMetric = { | ||
extensionName: this.extensionName, | ||
commandName, | ||
}; | ||
const aggregatedProps = Object.assign(baseProperties, properties); | ||
|
||
let aggregatedMeasurements: Measurements | undefined; | ||
if (hrstart || measurements) { | ||
aggregatedMeasurements = Object.assign({}, measurements); | ||
if (hrstart) { | ||
aggregatedMeasurements.executionTime = this.getEndHRTime(hrstart); | ||
} | ||
} | ||
this.reporter?.sendTelemetryEvent( | ||
'commandExecution', | ||
aggregatedProps, | ||
aggregatedMeasurements | ||
); | ||
} | ||
} | ||
|
||
public sendException(name: string, message: string): void { | ||
this.reporter?.sendExceptionEvent(name, message); | ||
} | ||
|
||
public sendEventData( | ||
eventName: string, | ||
properties?: Properties, | ||
measures?: Measurements | ||
): void { | ||
this.reporter?.sendTelemetryEvent(eventName, properties, measures); | ||
} | ||
|
||
public getEndHRTime(hrstart: [number, number]): number { | ||
const hrend = process.hrtime(hrstart); | ||
return Number(util.format('%d%d', hrend[0], hrend[1] / 1000000)); | ||
} | ||
|
||
public dispose(): void { | ||
if (this.reporter !== undefined) { | ||
this.reporter.dispose().catch((err) => console.log(err)); | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is blank aiKey valid?