-
Notifications
You must be signed in to change notification settings - Fork 9
/
telemetry.ts
49 lines (44 loc) · 1.86 KB
/
telemetry.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
/*******************************************************************************
* Copyright (c) 2021 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
import { getRedHatService, TelemetryEvent, TelemetryService } from '@redhat-developer/vscode-redhat-telemetry';
import { ExtensionContext } from 'vscode';
import * as os from 'os';
import ipRegex = require('ip-regex');
import emailRegex = require('email-regex');
let telemetryService: TelemetryService;
export function createTrackingEvent(name: string, properties: any = {}): TelemetryEvent {
return {
type: 'track',
name,
properties
}
}
export async function startTelemetry(context: ExtensionContext): Promise<void> {
try {
const redHatService = await getRedHatService(context);
telemetryService = await redHatService.getTelemetryService();
} catch(error) {
// eslint-disable-next-line no-console
console.log(`${error}`);
}
return telemetryService?.sendStartupEvent();
}
export async function sendTelemetry(actionName: string, properties?: any): Promise<void> {
return telemetryService?.send(createTrackingEvent(actionName, properties));
}
export function sanitize(message: string) : string {
message = message.replace(os.homedir(), "$HOME");
message = message.replace(os.tmpdir(), "$TMPDIR")
message = message.replace(os.userInfo().username, "$USER")
message = message.replace(ipRegex(), "$IPADDRESS")
message = message.replace(emailRegex(), "$EMAIL")
return message;
}