You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 regels
3.5 KiB

7 jaren geleden
7 jaren geleden
7 jaren geleden
7 jaren geleden
7 jaren geleden
7 jaren geleden
7 jaren geleden
7 jaren geleden
7 jaren geleden
7 jaren geleden
7 jaren geleden
7 jaren geleden
7 jaren geleden
7 jaren geleden
  1. 'use strict';
  2. import { Disposable, env, version, workspace } from 'vscode';
  3. import { ExtensionKey, IConfig } from './configuration';
  4. import * as os from 'os';
  5. let _reporter: TelemetryReporter;
  6. export class Telemetry extends Disposable {
  7. static configure(key: string) {
  8. const cfg = workspace.getConfiguration().get<IConfig>(ExtensionKey)!;
  9. if (!cfg.advanced.telemetry.enabled || !workspace.getConfiguration('telemetry').get<boolean>('enableTelemetry', true)) return;
  10. _reporter = new TelemetryReporter(key);
  11. }
  12. static setContext(context?: { [key: string]: string }) {
  13. if (_reporter === undefined) return;
  14. _reporter.setContext(context);
  15. }
  16. static trackEvent(name: string, properties?: { [key: string]: string }, measurements?: { [key: string]: number; }) {
  17. if (_reporter === undefined) return;
  18. _reporter.trackEvent(name, properties, measurements);
  19. }
  20. static trackException(ex: Error) {
  21. if (_reporter === undefined) return;
  22. _reporter.trackException(ex);
  23. }
  24. }
  25. export class TelemetryReporter {
  26. private appInsights: ApplicationInsights;
  27. private _client: Client;
  28. private _context: { [key: string]: string };
  29. constructor(key: string) {
  30. const diagChannelState = process.env['APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL'];
  31. (process.env['APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL'] as any) = true;
  32. this.appInsights = require('applicationinsights') as ApplicationInsights;
  33. process.env['APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL'] = diagChannelState;
  34. if (this.appInsights.client) {
  35. this._client = this.appInsights.getClient(key);
  36. // no other way to enable offline mode
  37. this._client.channel.setOfflineMode(true);
  38. }
  39. else {
  40. this._client = this.appInsights.setup(key)
  41. .setAutoCollectRequests(false)
  42. .setAutoCollectPerformance(false)
  43. .setAutoCollectExceptions(false)
  44. .setAutoCollectDependencies(false)
  45. .setAutoCollectConsole(false)
  46. .setAutoDependencyCorrelation(false)
  47. .setOfflineMode(true)
  48. .start()
  49. .client;
  50. }
  51. this.setContext();
  52. this._stripPII(this._client);
  53. }
  54. setContext(context?: { [key: string]: string }) {
  55. if (!this._context) {
  56. this._context = Object.create(null);
  57. // Add vscode properties
  58. this._context['code.language'] = env.language;
  59. this._context['code.version'] = version;
  60. this._context[this._client.context.keys.sessionId] = env.sessionId;
  61. // Add os properties
  62. this._context['os.platform'] = os.platform();
  63. this._context['os.version'] = os.release();
  64. }
  65. if (context) {
  66. Object.assign(this._context, context);
  67. }
  68. Object.assign(this._client.commonProperties, this._context);
  69. }
  70. trackEvent(name: string, properties?: { [key: string]: string }, measurements?: { [key: string]: number; }) {
  71. this._client.trackEvent(name, properties, measurements);
  72. }
  73. trackException(ex: Error) {
  74. this._client.trackException(ex);
  75. }
  76. private _stripPII(client: Client) {
  77. if (client && client.context && client.context.keys && client.context.tags) {
  78. const machineNameKey = client.context.keys.deviceMachineName;
  79. client.context.tags[machineNameKey] = '';
  80. }
  81. }
  82. }