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
80 lines (74 loc) · 2.7 KB
/
telemetry.ts
File metadata and controls
80 lines (74 loc) · 2.7 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
/*---------------------------------------------------------------------------------------------
* 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/lib';
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;
}
}
}
}
export class TelemetryOutputChannel implements vscode.OutputChannel {
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')) {
this.telemetry.send({ name: 'yaml.server.error', properties: { error: value } });
}
}
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();
}
}