forked from redhat-developer/vscode-yaml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry.ts
More file actions
125 lines (113 loc) · 3.83 KB
/
telemetry.ts
File metadata and controls
125 lines (113 loc) · 3.83 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { TelemetryService } from '@redhat-developer/vscode-redhat-telemetry';
import { CloseAction, ErrorAction, ErrorHandler, Message } from 'vscode-languageclient/node';
import * as vscode from 'vscode';
export class TelemetryErrorHandler implements ErrorHandler {
private restarts: number[] = [];
constructor(
private readonly telemetry: TelemetryService,
private readonly name: string,
private readonly maxRestartCount: number
) {}
error(error: Error, message: Message, count: number): ErrorAction {
this.telemetry.send({ name: 'yaml.lsp.error', properties: { jsonrpc: message.jsonrpc, error: error.message } });
if (count && count <= 3) {
return ErrorAction.Continue;
}
return ErrorAction.Shutdown;
}
closed(): CloseAction {
this.restarts.push(Date.now());
if (this.restarts.length <= this.maxRestartCount) {
return CloseAction.Restart;
} else {
const diff = this.restarts[this.restarts.length - 1] - this.restarts[0];
if (diff <= 3 * 60 * 1000) {
vscode.window.showErrorMessage(
`The ${this.name} server crashed ${
this.maxRestartCount + 1
} times in the last 3 minutes. The server will not be restarted.`
);
return CloseAction.DoNotRestart;
} else {
this.restarts.shift();
return CloseAction.Restart;
}
}
}
}
const errorMassagesToSkip = [{ text: 'Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED', contains: true }];
export class TelemetryOutputChannel implements vscode.OutputChannel {
private errors: string[] | undefined;
private throttleTimeout: NodeJS.Timeout | undefined;
constructor(private readonly delegate: vscode.OutputChannel, private readonly telemetry: TelemetryService) {}
get name(): string {
return this.delegate.name;
}
append(value: string): void {
this.checkError(value);
this.delegate.append(value);
}
appendLine(value: string): void {
this.checkError(value);
this.delegate.appendLine(value);
}
private checkError(value: string): void {
if (value.startsWith('[Error') || value.startsWith(' Message: Request')) {
if (this.isNeedToSkip(value)) {
return;
}
if (!this.errors) {
this.errors = [];
}
if (this.throttleTimeout) {
clearTimeout(this.throttleTimeout);
}
this.errors.push(value);
this.throttleTimeout = setTimeout(() => {
this.telemetry.send({ name: 'yaml.server.error', properties: { error: this.createErrorMessage() } });
}, 500);
}
}
private isNeedToSkip(value: string): boolean {
for (const skip of errorMassagesToSkip) {
if (skip.contains) {
if (value.includes(skip.text)) {
return true;
}
} else {
const starts = value.startsWith(skip.text);
if (starts) {
return true;
}
}
}
return false;
}
private createErrorMessage(): string {
const result = [];
for (const value of this.errors) {
if (value.startsWith('[Error')) {
result.push(value.substr(value.indexOf(']') + 1, value.length).trim());
}
}
return result.join('\n');
}
clear(): void {
this.delegate.clear();
}
show(preserveFocus?: boolean): void;
show(column?: vscode.ViewColumn, preserveFocus?: boolean): void;
show(column?: never, preserveFocus?: boolean): void {
this.delegate.show(column, preserveFocus);
}
hide(): void {
this.delegate.hide();
}
dispose(): void {
this.delegate.dispose();
}
}