Skip to content

Commit 1e411db

Browse files
leigaoljustinmk3
andauthored
feat(amazonq): sync telemetry client id between extensions (#4698)
Alternative to: #4683 ## Problem The elemetry client id has to to be the same between Amazon Q and AWS Toolkit ## Solution VS Code extensions are in same process. Use `process.env` to pass the telemetry client id. Each extension attempt to set its telemetry client id at time of activation. There can be edge cases when Q activates but toolkit is uninstalled (still has old client id), in this case, the 2nd time vscode window reloads, the amazon Q will sync its client id with AWS toolkit. commits: * ws space * use process.env * update debug log * rerun-test * move setup client id to utils * Update packages/core/src/shared/telemetry/activation.ts * fix conflict --------- Co-authored-by: Justin M. Keyes <[email protected]>
1 parent 6710e8a commit 1e411db

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

packages/core/src/shared/telemetry/activation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { DefaultTelemetryService } from './telemetryService'
1414
import { getLogger } from '../logger'
1515
import { getComputeRegion, getIdeProperties, isCloud9 } from '../extensionUtilities'
1616
import { openSettings, Settings } from '../settings'
17-
import { TelemetryConfig } from './util'
17+
import { TelemetryConfig, setupTelemetryId } from './util'
1818
import { isAutomation, isReleaseVersion } from '../vscode/env'
1919

2020
export const noticeResponseViewSettings = localize('AWS.telemetry.notificationViewSettings', 'Settings')
@@ -52,7 +52,7 @@ export async function activate(extensionContext: vscode.ExtensionContext, awsCon
5252
if (!isCloud9() && !hasUserSeenTelemetryNotice(extensionContext)) {
5353
showTelemetryNotice(extensionContext)
5454
}
55-
55+
await setupTelemetryId(extensionContext)
5656
await globals.telemetry.start()
5757
} catch (e) {
5858
// Only throw in a production build because:

packages/core/src/shared/telemetry/util.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
import * as vscode from 'vscode'
67
import { env, Memento, version } from 'vscode'
78
import { getLogger } from '../logger'
89
import { fromExtensionManifest } from '../settings'
@@ -17,11 +18,14 @@ import { MetricDatum } from './clienttelemetry'
1718
import { isValidationExemptMetric } from './exemptMetrics'
1819
import { isCloud9, isSageMaker } from '../../shared/extensionUtilities'
1920
import { VSCODE_EXTENSION_ID } from '../utilities'
21+
import { isWeb } from '../../common/webUtils'
2022

2123
const legacySettingsTelemetryValueDisable = 'Disable'
2224
const legacySettingsTelemetryValueEnable = 'Enable'
2325

2426
const TelemetryFlag = addTypeName('boolean', convertLegacy)
27+
const telemetryClientIdGlobalStatekey = 'telemetryClientId'
28+
const telemetryClientIdEnvKey = '__TELEMETRY_CLIENT_ID'
2529

2630
export class TelemetryConfig extends fromExtensionManifest('aws', { telemetry: TelemetryFlag }) {
2731
public isEnabled(): boolean {
@@ -53,10 +57,10 @@ export const getClientId = shared(
5357
return '11111111-1111-1111-1111-111111111111'
5458
}
5559
try {
56-
let clientId = globalState.get<string>('telemetryClientId')
60+
let clientId = globalState.get<string>(telemetryClientIdGlobalStatekey)
5761
if (!clientId) {
5862
clientId = randomUUID()
59-
await globalState.update('telemetryClientId', clientId)
63+
await globalState.update(telemetryClientIdGlobalStatekey, clientId)
6064
}
6165
return clientId
6266
} catch (error) {
@@ -158,3 +162,44 @@ export function validateMetricEvent(event: MetricDatum, fatal: boolean) {
158162
getLogger().warn(msg)
159163
}
160164
}
165+
166+
/**
167+
* Setup the telemetry client id at extension activation.
168+
* This function is designed to let AWS Toolkit and Amazon Q share
169+
* the same telemetry client id.
170+
*/
171+
172+
export async function setupTelemetryId(extensionContext: vscode.ExtensionContext) {
173+
try {
174+
if (isWeb()) {
175+
await globals.context.globalState.update(telemetryClientIdGlobalStatekey, vscode.env.machineId)
176+
} else {
177+
const currentClientId = globals.context.globalState.get<string>(telemetryClientIdGlobalStatekey)
178+
const storedClientId = process.env[telemetryClientIdEnvKey]
179+
if (currentClientId && storedClientId) {
180+
if (extensionContext.extension.id === VSCODE_EXTENSION_ID.awstoolkit) {
181+
getLogger().debug(`telemetry: Store telemetry client id to env ${currentClientId}`)
182+
process.env[telemetryClientIdEnvKey] = currentClientId
183+
} else if (extensionContext.extension.id === VSCODE_EXTENSION_ID.amazonq) {
184+
getLogger().debug(`telemetry: Set telemetry client id to ${currentClientId}`)
185+
await globals.context.globalState.update(telemetryClientIdGlobalStatekey, currentClientId)
186+
} else {
187+
getLogger().error(`Unexpected extension id ${extensionContext.extension.id}`)
188+
}
189+
} else if (!currentClientId && storedClientId) {
190+
getLogger().debug(`telemetry: Write telemetry client id to global state ${storedClientId}`)
191+
await globals.context.globalState.update(telemetryClientIdGlobalStatekey, storedClientId)
192+
} else if (currentClientId && !storedClientId) {
193+
getLogger().debug(`telemetry: Write telemetry client id to env ${currentClientId}`)
194+
process.env[telemetryClientIdEnvKey] = currentClientId
195+
} else {
196+
const clientId = randomUUID()
197+
getLogger().debug(`telemetry: Setup telemetry client id ${clientId}`)
198+
await globals.context.globalState.update(telemetryClientIdGlobalStatekey, clientId)
199+
process.env[telemetryClientIdEnvKey] = clientId
200+
}
201+
}
202+
} catch (err) {
203+
getLogger().error(`Erro while setting up telemetry id ${err}`)
204+
}
205+
}

0 commit comments

Comments
 (0)