Skip to content

Commit 62eb901

Browse files
authored
Merge #4303 refactor: simplify validateMetricEvent()
2 parents a80b6c8 + 42804b0 commit 62eb901

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

src/shared/telemetry/telemetryService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class DefaultTelemetryService {
101101
if (this.telemetryEnabled && !isAutomation()) {
102102
const currTime = new globals.clock.Date()
103103
// This is noisy when running tests in vscode.
104-
telemetry.session_end.emit({ value: currTime.getTime() - this.startTime.getTime() })
104+
telemetry.session_end.emit({ value: currTime.getTime() - this.startTime.getTime(), result: 'Succeeded' })
105105

106106
try {
107107
await fsCommon.writeFile(this.persistFilePath, JSON.stringify(this._eventQueue))

src/shared/telemetry/util.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,31 +98,25 @@ export async function getUserAgent(
9898
export function validateMetricEvent(event: MetricDatum, fatal: boolean) {
9999
const failedStr: Result = 'Failed'
100100
const telemetryRunDocsStr =
101-
'Consider using `.run()` instead of `.emit()`, which will set these properties automatically. ' +
101+
' Consider using `.run()` instead of `.emit()`, which will set these properties automatically. ' +
102102
'See https://github.com/aws/aws-toolkit-vscode/blob/master/docs/telemetry.md#guidelines'
103103

104104
if (!isValidationExemptMetric(event.MetricName) && event.Metadata) {
105105
const metadata = mapMetadata([])(event.Metadata)
106+
let msg = 'telemetry: invalid Metric: '
107+
108+
if (metadata.result === undefined) {
109+
msg += `"${event.MetricName}" emitted without the \`result\` property, which is always required.`
110+
} else if (metadata.result === failedStr && metadata.reason === undefined) {
111+
msg += `"${event.MetricName}" emitted with result=Failed but without the \`reason\` property.`
112+
} else {
113+
return // Validation passed.
114+
}
106115

107-
try {
108-
if (metadata.result === undefined) {
109-
throw new Error(
110-
`Metric \`${event.MetricName}\` was emitted without the \`result\` property. ` +
111-
`This property is always required. ${telemetryRunDocsStr}`
112-
)
113-
}
114-
115-
if (metadata.result === failedStr && metadata.reason === undefined) {
116-
throw new Error(
117-
`Metric \`${event.MetricName}\` was emitted without the \`reason\` property. ` +
118-
`This property is always required when \`result\` = 'Failed'. ${telemetryRunDocsStr}`
119-
)
120-
}
121-
} catch (err: any) {
122-
if (fatal) {
123-
throw err
124-
}
125-
getLogger().warn(`Metric Event did not pass validation: ${(err as Error).message}`)
116+
msg += telemetryRunDocsStr
117+
if (fatal) {
118+
throw new Error(msg)
126119
}
120+
getLogger().warn(msg)
127121
}
128122
}

0 commit comments

Comments
 (0)