Skip to content

Commit fb04184

Browse files
committed
feat(generation): allow awsRegion to be specified by callers in Metadata
Problem: `awsRegion` is a base metric and does not allow for callers to add it to their metadata when using `record()`. However, this field is often known best by the caller and not the telemetry guessing algorithm. Solution: Update type generation to allow this base metric to be specified in metadata.
1 parent a26bd3a commit fb04184

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

telemetry/vscode/src/generate.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ function getTypeOrThrow(types: MetadataType[] = [], name: string) {
122122

123123
const baseName = 'MetricBase'
124124

125+
/**
126+
* Fields set automatically by the telemetry client (thus application code
127+
* normally shouldn't set these because the value will be overridden).
128+
*/
125129
const commonMetadata = [
126130
'awsAccount',
127131
'awsRegion',
@@ -132,7 +136,13 @@ const commonMetadata = [
132136
'requestId',
133137
'requestServiceType',
134138
'result',
135-
]
139+
] as const
140+
141+
/**
142+
* These fields will also be set by the telemetry client, but the caller might
143+
* know better, so they won't be overridden if specified in `.record()` calls.
144+
*/
145+
const optionalMetadata: typeof commonMetadata[number][] = ['awsRegion']
136146

137147
const passive: PropertySignatureStructure = {
138148
isReadonly: true,
@@ -183,7 +193,7 @@ const header = `
183193
`.trimStart()
184194

185195
function getMetricMetadata(metric: Metric) {
186-
return metric.metadata?.filter(m => !commonMetadata.includes(m.type)) ?? []
196+
return metric.metadata?.filter(m => !commonMetadata.includes(m.type as typeof commonMetadata[number])) ?? []
187197
}
188198

189199
function generateMetadataProperty(metadata: MetricMetadataType): PropertySignatureStructure {
@@ -351,7 +361,7 @@ function generateFile(telemetryJson: MetricDefinitionRoot, dest: string) {
351361
isExported: true,
352362
name: 'Metadata',
353363
typeParameters: [`T extends ${baseName}`],
354-
type: `Partial<Omit<T, keyof ${baseName}>>`,
364+
type: `Partial<Omit<T, keyof ${baseName}> | Partial<Pick<${baseName}, ${optionalMetadata.map(v => `'${v}'`).join(' | ')}>>>`,
355365
kind: StructureKind.TypeAlias,
356366
}
357367

telemetry/vscode/test/resources/generatorOutput.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export const definitions: Record<string, MetricDefinition> = {
114114
passive_passive: { unit: 'None', passive: true, requiredMetadata: [] },
115115
}
116116

117-
export type Metadata<T extends MetricBase> = Partial<Omit<T, keyof MetricBase>>
117+
export type Metadata<T extends MetricBase> = Partial<Omit<T, keyof MetricBase> | Partial<Pick<MetricBase, 'awsRegion'>>>
118118

119119
export interface Metric<T extends MetricBase = MetricBase> {
120120
readonly name: string

telemetry/vscode/test/resources/generatorOverrideOutput.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const definitions: Record<string, MetricDefinition> = {
8585
metadata_hasResult: { unit: 'None', passive: false, requiredMetadata: [] },
8686
}
8787

88-
export type Metadata<T extends MetricBase> = Partial<Omit<T, keyof MetricBase>>
88+
export type Metadata<T extends MetricBase> = Partial<Omit<T, keyof MetricBase> | Partial<Pick<MetricBase, 'awsRegion'>>>
8989

9090
export interface Metric<T extends MetricBase = MetricBase> {
9191
readonly name: string

0 commit comments

Comments
 (0)