Skip to content

Commit 6c8898a

Browse files
authored
refactor(vscode): remove telemetry.foo.record (#859)
## Problem In vscode `telemetry.foo.record` is confusing to partner teams and has a couple of misuses in our codebase ## Solution We're modifying vscodes telemetry generation to only allow records on a span, instead of on `telemetry.foo.record`. This means the following will be available ``` telemetry.foo.run(span => { span.record( {someInformation: 5}) }) ``` but `telemetry.foo.record` will not.
1 parent 6b95f02 commit 6c8898a

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

telemetry/vscode/src/generate.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,6 @@ function generateMetricRecorder(metadataType: TypeAliasDeclarationStructure): In
284284
}
285285
],
286286
methods: [
287-
{
288-
docs: ['Adds data to the metric which is preserved for the remainder of the execution context'],
289-
name: 'record',
290-
returnType: 'void',
291-
parameters: [{
292-
name: 'data',
293-
type: `${metadataType.name}<T>`,
294-
}],
295-
},
296287
{
297288
docs: ['Sends the metric to the telemetry service'],
298289
name: 'emit',
@@ -310,7 +301,7 @@ function generateMetricRecorder(metadataType: TypeAliasDeclarationStructure): In
310301
returnType: 'U',
311302
parameters: [{
312303
name: 'fn',
313-
type: `(span: this) => U`,
304+
type: `(span: Span<T>) => U`,
314305
}],
315306
}
316307
],
@@ -406,10 +397,31 @@ function generateFile(telemetryJson: MetricDefinitionRoot, dest: string) {
406397
kind: StructureKind.TypeAlias,
407398
}
408399

400+
const span: InterfaceDeclarationStructure = {
401+
kind: StructureKind.Interface,
402+
name: 'Span',
403+
isExported: true,
404+
typeParameters: [{ name: 'T' }],
405+
docs: ['Represents a telemetry span for tracking and recording metric data.'],
406+
methods: [
407+
{
408+
name: 'record',
409+
parameters: [
410+
{
411+
name: 'data',
412+
type: 'Partial<T>',
413+
},
414+
],
415+
returnType: 'this',
416+
}
417+
],
418+
}
419+
409420
const definitions = generateDefinitions(telemetryJson.metrics)
410421
const recorder = generateMetricRecorder(metadataType)
411422
file.addVariableStatement(definitions)
412423
file.addTypeAlias(metadataType)
424+
file.addInterface(span)
413425
file.addInterface(recorder)
414426
file.addClass(generateTelemetryHelper(recorder, telemetryJson.metrics))
415427

telemetry/vscode/test/resources/generatorOutput.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,17 @@ export const definitions: Record<string, MetricDefinition> = {
125125

126126
export type Metadata<T extends MetricBase> = Partial<Omit<T, keyof MetricBase> | Partial<Pick<MetricBase, 'awsRegion'>>>
127127

128+
/** Represents a telemetry span for tracking and recording metric data. */
129+
export interface Span<T> {
130+
record(data: Partial<T>): this
131+
}
132+
128133
export interface Metric<T extends MetricBase = MetricBase> {
129134
readonly name: string
130-
/** Adds data to the metric which is preserved for the remainder of the execution context */
131-
record(data: Metadata<T>): void
132135
/** Sends the metric to the telemetry service */
133136
emit(data?: T): void
134137
/** Executes a callback, automatically sending the metric after completion */
135-
run<U>(fn: (span: this) => U): U
138+
run<U>(fn: (span: Span<T>) => U): U
136139
}
137140

138141
export abstract class TelemetryBase {

telemetry/vscode/test/resources/generatorOverrideOutput.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,17 @@ export const definitions: Record<string, MetricDefinition> = {
9696

9797
export type Metadata<T extends MetricBase> = Partial<Omit<T, keyof MetricBase> | Partial<Pick<MetricBase, 'awsRegion'>>>
9898

99+
/** Represents a telemetry span for tracking and recording metric data. */
100+
export interface Span<T> {
101+
record(data: Partial<T>): this
102+
}
103+
99104
export interface Metric<T extends MetricBase = MetricBase> {
100105
readonly name: string
101-
/** Adds data to the metric which is preserved for the remainder of the execution context */
102-
record(data: Metadata<T>): void
103106
/** Sends the metric to the telemetry service */
104107
emit(data?: T): void
105108
/** Executes a callback, automatically sending the metric after completion */
106-
run<U>(fn: (span: this) => U): U
109+
run<U>(fn: (span: Span<T>) => U): U
107110
}
108111

109112
export abstract class TelemetryBase {

0 commit comments

Comments
 (0)