-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathindex.ts
126 lines (115 loc) · 3.26 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// eslint-disable-next-line import/default
import TelemetryReporter from '@vscode/extension-telemetry'
import {
APPLICATION_INSIGHTS_CONNECTION_STRING,
IEventNamePropertyMapping,
ViewOpenedEventName
} from './constants'
import { getUserId } from './uuid'
import { Logger } from '../common/logger'
const isTestExecution = (): boolean =>
!!process.env.VSC_TEST || process.env.NODE_ENV === 'test'
const isDebugSession = (): boolean => !!process.env.VSC_DEBUG
let telemetryReporter: TelemetryReporter | undefined
export const getTelemetryReporter = (): TelemetryReporter => {
if (!isTestExecution() && !isDebugSession() && telemetryReporter) {
return telemetryReporter
}
telemetryReporter = new TelemetryReporter(
APPLICATION_INSIGHTS_CONNECTION_STRING
)
return telemetryReporter
}
type EventName = keyof IEventNamePropertyMapping
const convertProperty = (prop: object | string | number | boolean): string => {
if (typeof prop === 'string') {
return prop
}
if (typeof prop === 'object') {
return JSON.stringify(prop)
}
return prop.toString()
}
const sanitizeProperty = (
eventName: string,
sanitizedProperties: Record<string, string>,
key: string,
value: object | string | number | boolean
) => {
try {
// If there are any errors in serializing one property, ignore that and move on.
// Else nothing will be sent.
sanitizedProperties[key] = convertProperty(value)
} catch (error: unknown) {
Logger.error(
`Failed to serialize ${key} for ${String(eventName)}: ${JSON.stringify(
error
)}`
)
}
}
const sanitizeProperties = (
eventName: EventName,
data: IEventNamePropertyMapping[EventName]
) => {
const sanitizedProperties: Record<string, string> = {}
for (const [key, value] of Object.entries(data || {})) {
if (value === undefined || value === null) {
continue
}
sanitizeProperty(
eventName,
sanitizedProperties,
key,
value as string | number | boolean
)
}
return sanitizedProperties
}
export const sendTelemetryEvent = (
eventName: EventName,
properties: IEventNamePropertyMapping[EventName],
measurements: { [key: string]: number } | undefined
) => {
if (isTestExecution() || isDebugSession()) {
return
}
const reporter = getTelemetryReporter()
const sanitizedProperties = properties
? sanitizeProperties(eventName, properties)
: undefined
reporter.sendTelemetryEvent(
eventName as string,
{ ...sanitizedProperties, user_id: getUserId() },
measurements
)
}
export const sendErrorTelemetryEvent = (
eventName: EventName,
e: Error,
duration: number,
properties = {} as IEventNamePropertyMapping[EventName]
) =>
sendTelemetryEvent(
`errors.${String(eventName)}` as EventName,
{
...properties,
error: e.message
} as unknown as IEventNamePropertyMapping[EventName],
{
duration
}
)
export const sendTelemetryEventAndThrow = (
eventName: EventName,
e: Error,
duration: number,
properties = {} as IEventNamePropertyMapping[EventName]
) => {
sendErrorTelemetryEvent(eventName, e, duration, properties)
throw e
}
export const sendViewOpenedTelemetryEvent = (
eventName: ViewOpenedEventName,
dvcRootCount: number
) => sendTelemetryEvent(eventName, { dvcRootCount }, undefined)