4
4
*/
5
5
6
6
import * as vscode from 'vscode'
7
- import { env , Memento , version } from 'vscode'
7
+ import { env , version } from 'vscode'
8
8
import * as os from 'os'
9
9
import { getLogger } from '../logger'
10
10
import { fromExtensionManifest , migrateSetting , Settings } from '../settings'
@@ -26,11 +26,9 @@ const legacySettingsTelemetryValueDisable = 'Disable'
26
26
const legacySettingsTelemetryValueEnable = 'Enable'
27
27
28
28
const TelemetryFlag = addTypeName ( 'boolean' , convertLegacy )
29
- const telemetryClientIdGlobalStatekey = 'telemetryClientId'
30
29
const telemetryClientIdEnvKey = '__TELEMETRY_CLIENT_ID'
31
30
32
31
export class TelemetryConfig {
33
- private readonly amazonQSettingMigratedKey = 'amazonq.telemetry.migrated'
34
32
private readonly _toolkitConfig
35
33
private readonly _amazonQConfig
36
34
@@ -60,13 +58,13 @@ export class TelemetryConfig {
60
58
}
61
59
62
60
public async initAmazonQSetting ( ) {
63
- if ( ! isAmazonQ ( ) || globals . context . globalState . get < boolean > ( this . amazonQSettingMigratedKey ) ) {
61
+ if ( ! isAmazonQ ( ) || globals . globalState . tryGet ( 'amazonq.telemetry.migrated' , Boolean , false ) ) {
64
62
return
65
63
}
66
64
// aws.telemetry isn't deprecated, we are just initializing amazonQ.telemetry with its value.
67
65
// This is also why we need to check that we only try this migration once.
68
66
await migrateSetting ( { key : 'aws.telemetry' , type : Boolean } , { key : 'amazonQ.telemetry' } )
69
- await globals . context . globalState . update ( this . amazonQSettingMigratedKey , true )
67
+ await globals . globalState . update ( 'amazonq.telemetry.migrated' , true )
70
68
}
71
69
}
72
70
@@ -89,20 +87,23 @@ export const getClientId = memoize(
89
87
/**
90
88
* @param nonce Dummy parameter to allow tests to defeat memoize().
91
89
*/
92
- ( globalState : Memento , isTelemetryEnabled = new TelemetryConfig ( ) . isEnabled ( ) , isTest ?: false , nonce ?: string ) => {
90
+ (
91
+ globalState : typeof globals . globalState ,
92
+ isTelemetryEnabled = new TelemetryConfig ( ) . isEnabled ( ) ,
93
+ isTest ?: false ,
94
+ nonce ?: string
95
+ ) => {
93
96
if ( isTest ?? isAutomation ( ) ) {
94
97
return 'ffffffff-ffff-ffff-ffff-ffffffffffff'
95
98
}
96
99
if ( ! isTelemetryEnabled ) {
97
100
return '11111111-1111-1111-1111-111111111111'
98
101
}
99
102
try {
100
- let clientId = globalState . get < string > ( telemetryClientIdGlobalStatekey )
103
+ let clientId = globalState . tryGet ( 'telemetryClientId' , String )
101
104
if ( ! clientId ) {
102
105
clientId = randomUUID ( )
103
- globalState . update ( telemetryClientIdGlobalStatekey , clientId ) . then ( undefined , ( e ) => {
104
- getLogger ( ) . error ( 'getClientId: globalState.update failed: %O' , e )
105
- } )
106
+ globalState . tryUpdate ( 'telemetryClientId' , clientId )
106
107
}
107
108
return clientId
108
109
} catch ( e ) {
@@ -122,7 +123,7 @@ export const platformPair = () => `${env.appName.replace(/\s/g, '-')}/${version}
122
123
*/
123
124
export function getUserAgent (
124
125
opt ?: { includePlatform ?: boolean ; includeClientId ?: boolean } ,
125
- globalState = globals . context . globalState
126
+ globalState = globals . globalState
126
127
) : string {
127
128
const pairs = isAmazonQ ( )
128
129
? [ `AmazonQ-For-VSCode/${ extensionVersion } ` ]
@@ -212,9 +213,9 @@ export function validateMetricEvent(event: MetricDatum, fatal: boolean) {
212
213
export async function setupTelemetryId ( extensionContext : vscode . ExtensionContext ) {
213
214
try {
214
215
if ( isWeb ( ) ) {
215
- await globals . context . globalState . update ( telemetryClientIdGlobalStatekey , vscode . env . machineId )
216
+ await globals . globalState . update ( 'telemetryClientId' , vscode . env . machineId )
216
217
} else {
217
- const currentClientId = globals . context . globalState . get < string > ( telemetryClientIdGlobalStatekey )
218
+ const currentClientId = globals . globalState . tryGet ( 'telemetryClientId' , String )
218
219
const storedClientId = process . env [ telemetryClientIdEnvKey ]
219
220
if ( currentClientId && storedClientId ) {
220
221
if ( extensionContext . extension . id === VSCODE_EXTENSION_ID . awstoolkit ) {
@@ -230,13 +231,13 @@ export async function setupTelemetryId(extensionContext: vscode.ExtensionContext
230
231
}
231
232
} else if ( isAmazonQ ( ) ) {
232
233
getLogger ( ) . debug ( `telemetry: Set telemetry client id to ${ storedClientId } ` )
233
- await globals . context . globalState . update ( telemetryClientIdGlobalStatekey , storedClientId )
234
+ await globals . globalState . update ( 'telemetryClientId' , storedClientId )
234
235
} else {
235
236
getLogger ( ) . error ( `Unexpected extension id ${ extensionContext . extension . id } ` )
236
237
}
237
238
} else if ( ! currentClientId && storedClientId ) {
238
239
getLogger ( ) . debug ( `telemetry: Write telemetry client id to global state ${ storedClientId } ` )
239
- await globals . context . globalState . update ( telemetryClientIdGlobalStatekey , storedClientId )
240
+ await globals . globalState . update ( 'telemetryClientId' , storedClientId )
240
241
} else if ( currentClientId && ! storedClientId ) {
241
242
getLogger ( ) . debug ( `telemetry: Write telemetry client id to env ${ currentClientId } ` )
242
243
process . env [ telemetryClientIdEnvKey ] = currentClientId
0 commit comments