Skip to content

Commit 42804b0

Browse files
committed
refactor: simplify validateMetricEvent()
1 parent 5a16e8d commit 42804b0

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

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)