Skip to content

Commit 63d19e9

Browse files
authored
feat(telemetry): add awsRegion in commonMetadata #2941
related: a50b83a Also rename "filters" to "excludeKeys" because "filters" is ambiguous: it can mean include or exclude.
1 parent 57102b7 commit 63d19e9

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

src/shared/telemetry/telemetryClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { DevSettings } from '../settings'
1919
import { ClassToInterfaceType } from '../utilities/tsUtils'
2020

2121
export const ACCOUNT_METADATA_KEY = 'awsAccount'
22+
export const REGION_KEY = 'awsRegion'
2223
export const COMPUTE_REGION_KEY = 'computeRegion'
2324

2425
export enum AccountStatus {

src/shared/telemetry/telemetryLogger.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ export interface MetricQuery {
1414
readonly metricName: string
1515

1616
/**
17-
* Attributes to filter out of the metadata
17+
* Exclude metadata items matching these keys.
1818
*/
19-
readonly filters?: string[]
19+
readonly excludeKeys?: string[]
2020
}
2121

2222
interface Metadata {
@@ -30,11 +30,11 @@ function isValidEntry(datum: MetadataEntry): datum is Required<MetadataEntry> {
3030
/**
3131
* Telemetry currently sends metadata as an array of key/value pairs, but this is unintuitive for JS
3232
*/
33-
const mapMetadata = (filters: string[]) => (metadata: Required<MetricDatum>['Metadata']) => {
33+
const mapMetadata = (excludeKeys: string[]) => (metadata: Required<MetricDatum>['Metadata']) => {
3434
const result: Metadata = {}
3535
return metadata
3636
.filter(isValidEntry)
37-
.filter(a => !filters.includes(a.Key))
37+
.filter(a => !excludeKeys.includes(a.Key))
3838
.reduce((a, b) => ((a[b.Key] = b.Value), a), result)
3939
}
4040

@@ -80,7 +80,7 @@ export class TelemetryLogger {
8080
public query(query: MetricQuery): Metadata[] {
8181
return this.queryFull(query)
8282
.map(m => m.Metadata ?? [])
83-
.map(mapMetadata(query.filters ?? []))
83+
.map(mapMetadata(query.excludeKeys ?? []))
8484
}
8585

8686
/**

src/shared/telemetry/telemetryService.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { AwsContext } from '../awsContext'
1111
import { isReleaseVersion, isAutomation } from '../vscode/env'
1212
import { getLogger } from '../logger'
1313
import { MetricDatum } from './clienttelemetry'
14-
import { DefaultTelemetryClient } from './telemetryClient'
14+
import { DefaultTelemetryClient, REGION_KEY } from './telemetryClient'
1515
import { DefaultTelemetryPublisher } from './telemetryPublisher'
1616
import { TelemetryFeedback } from './telemetryClient'
1717
import { TelemetryPublisher } from './telemetryPublisher'
@@ -241,6 +241,9 @@ export class DefaultTelemetryService {
241241
if (this.computeRegion) {
242242
commonMetadata.push({ Key: COMPUTE_REGION_KEY, Value: this.computeRegion })
243243
}
244+
if (!event?.Metadata?.some((m: any) => m?.Key == REGION_KEY)) {
245+
commonMetadata.push({ Key: REGION_KEY, Value: globals.regionProvider.guessDefaultRegion() })
246+
}
244247

245248
if (event.Metadata) {
246249
event.Metadata.push(...commonMetadata)

src/test/codewhisperer/tracker/codewhispererCodeCoverageTracker.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ describe('codewhispererCodecoverageTracker', function () {
318318
tracker.flush()
319319
const data = globals.telemetry.logger.query({
320320
metricName: 'codewhisperer_codePercentage',
321-
filters: ['awsAccount'],
321+
excludeKeys: ['awsAccount'],
322322
})
323323
assert.strictEqual(data.length, 0)
324324
})

src/test/testUtil.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,11 @@ export function installFakeClock(): FakeTimers.InstalledClock {
136136
* Unlike {@link assertTelemetry}, this function does not do any transformations to
137137
* handle fields being converted into strings. It will also not return `passive` or `value`.
138138
*/
139-
export function getMetrics<K extends MetricName>(name: K, ...filters: string[]): readonly Partial<MetricShapes[K]>[] {
140-
const query = { metricName: name, filters: ['awsAccount', ...filters] }
139+
export function getMetrics<K extends MetricName>(
140+
name: K,
141+
...excludeKeys: string[]
142+
): readonly Partial<MetricShapes[K]>[] {
143+
const query = { metricName: name, excludeKeys: ['awsAccount', 'awsRegion', ...excludeKeys] }
141144

142145
return globals.telemetry.logger.query(query) as unknown as Partial<MetricShapes[K]>[]
143146
}
@@ -149,13 +152,16 @@ export function getMetrics<K extends MetricName>(name: K, ...filters: string[]):
149152
export function assertTelemetry<K extends MetricName>(name: K, expected: MetricShapes[K]): void | never {
150153
const expectedCopy = { ...expected } as { -readonly [P in keyof MetricShapes[K]]: MetricShapes[K][P] }
151154
const passive = expectedCopy?.passive
152-
const query = { metricName: name, filters: ['awsAccount', 'duration'] }
155+
const query = { metricName: name, excludeKeys: ['awsAccount', 'duration'] }
153156
delete expectedCopy['passive']
154157

155158
Object.keys(expectedCopy).forEach(
156159
k => ((expectedCopy as any)[k] = (expectedCopy as Record<string, any>)[k]?.toString())
157160
)
158161

162+
// Telemetry client should add awsRegion to all metrics.
163+
;(expectedCopy as any)['awsRegion'] = globals.regionProvider.guessDefaultRegion()
164+
159165
const metadata = globals.telemetry.logger.query(query)
160166
assert.ok(metadata.length > 0, `Telemetry did not contain any metrics with the name "${name}"`)
161167
assert.deepStrictEqual(metadata[0], expectedCopy)

0 commit comments

Comments
 (0)