-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathprometheus-metrics-exporter.ts
165 lines (146 loc) · 7.26 KB
/
prometheus-metrics-exporter.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/
import * as prom from "prom-client";
import { injectable } from "inversify";
import { WorkspaceInstance } from "@gitpod/gitpod-protocol";
import { WorkspaceClusterWoTLS } from "@gitpod/gitpod-protocol/src/workspace-cluster";
import { WorkspaceType } from "@gitpod/ws-manager/lib/core_pb";
@injectable()
export class PrometheusMetricsExporter {
protected readonly workspaceStartupTimeHistogram: prom.Histogram<string>;
protected readonly timeToFirstUserActivityHistogram: prom.Histogram<string>;
protected readonly clusterScore: prom.Gauge<string>;
protected readonly clusterCordoned: prom.Gauge<string>;
protected readonly statusUpdatesTotal: prom.Counter<string>;
protected readonly staleStatusUpdatesTotal: prom.Counter<string>;
protected readonly stalePrebuildEventsTotal: prom.Counter<string>;
protected readonly prebuildsCompletedTotal: prom.Counter<string>;
protected readonly workspaceInstanceUpdateStartedTotal: prom.Counter<string>;
protected readonly workspaceInstanceUpdateCompletedSeconds: prom.Histogram<string>;
protected activeClusterNames = new Set<string>();
constructor() {
this.workspaceStartupTimeHistogram = new prom.Histogram({
name: "workspace_startup_time",
help: "The time until a workspace instance is marked running",
labelNames: ["neededImageBuild", "region"],
buckets: prom.exponentialBuckets(2, 2, 10),
});
this.timeToFirstUserActivityHistogram = new prom.Histogram({
name: "first_user_activity_time",
help: "The time between a workspace is running and first user activity",
labelNames: ["region"],
buckets: prom.exponentialBuckets(2, 2, 10),
});
this.clusterScore = new prom.Gauge({
name: "gitpod_ws_manager_bridge_cluster_score",
help: "Score of the individual registered workspace cluster",
labelNames: ["workspace_cluster"],
});
this.clusterCordoned = new prom.Gauge({
name: "gitpod_ws_manager_bridge_cluster_cordoned",
help: "Cordoned status of the individual registered workspace cluster",
labelNames: ["workspace_cluster"],
});
this.statusUpdatesTotal = new prom.Counter({
name: "gitpod_ws_manager_bridge_status_updates_total",
help: "Total workspace status updates received",
labelNames: ["workspace_cluster", "known_instance"],
});
this.staleStatusUpdatesTotal = new prom.Counter({
name: "gitpod_ws_manager_bridge_stale_status_updates_total",
help: "Total count of stale status updates received by workspace manager bridge",
});
this.stalePrebuildEventsTotal = new prom.Counter({
name: "gitpod_ws_manager_bridge_stale_prebuild_events_total",
help: "Total count of stale prebuild events received by workspace manager bridge",
});
this.workspaceInstanceUpdateStartedTotal = new prom.Counter({
name: "gitpod_ws_manager_bridge_workspace_instance_update_started_total",
help: "Total number of workspace instance updates that started processing",
// we track db_write because we need to be able to distinguish between outcomes which did affect the system negatively - failed to write,
// and outcomes by read-only replicas.
labelNames: ["db_write", "workspace_cluster", "workspace_instance_type"],
});
this.workspaceInstanceUpdateCompletedSeconds = new prom.Histogram({
name: "gitpod_ws_manager_bridge_workspace_instance_update_completed_seconds",
help: "Histogram of completed workspace instance updates, by outcome",
// we track db_write because we need to be able to distinguish between outcomes which did affect the system negatively - failed to write,
// and outcomes by read-only replicas.
labelNames: ["db_write", "workspace_cluster", "workspace_instance_type", "outcome"],
buckets: prom.exponentialBuckets(2, 2, 8),
});
this.prebuildsCompletedTotal = new prom.Counter({
name: "gitpod_prebuilds_completed_total",
help: "Counter of total prebuilds ended.",
labelNames: ["state"],
});
}
observeWorkspaceStartupTime(instance: WorkspaceInstance): void {
const timeToRunningSecs =
(new Date(instance.startedTime!).getTime() - new Date(instance.creationTime).getTime()) / 1000;
this.workspaceStartupTimeHistogram.observe(
{
neededImageBuild: JSON.stringify(instance.status.conditions.neededImageBuild),
region: instance.region,
},
timeToRunningSecs,
);
}
observeFirstUserActivity(instance: WorkspaceInstance, firstUserActivity: string): void {
if (!instance.startedTime) {
return;
}
const timeToFirstUserActivity =
(new Date(firstUserActivity).getTime() - new Date(instance.startedTime!).getTime()) / 1000;
this.timeToFirstUserActivityHistogram.observe(
{
region: instance.region,
},
timeToFirstUserActivity,
);
}
updateClusterMetrics(clusters: WorkspaceClusterWoTLS[]): void {
let newActiveClusterNames = new Set<string>();
clusters.forEach((cluster) => {
this.clusterCordoned.labels(cluster.name).set(cluster.state === "cordoned" ? 1 : 0);
this.clusterScore.labels(cluster.name).set(cluster.score);
newActiveClusterNames.add(cluster.name);
});
const noLongerActiveCluster = Array.from(this.activeClusterNames).filter((c) => !newActiveClusterNames.has(c));
noLongerActiveCluster.forEach((clusterName) => {
this.clusterCordoned.remove(clusterName);
this.clusterScore.remove(clusterName);
});
this.activeClusterNames = newActiveClusterNames;
}
statusUpdateReceived(installation: string, knownInstance: boolean): void {
this.statusUpdatesTotal.labels(installation, knownInstance ? "true" : "false").inc();
}
recordStaleStatusUpdate(): void {
this.staleStatusUpdatesTotal.inc();
}
recordStalePrebuildEvent(): void {
this.stalePrebuildEventsTotal.inc();
}
reportWorkspaceInstanceUpdateStarted(dbWrite: boolean, workspaceCluster: string, type: WorkspaceType): void {
this.workspaceInstanceUpdateStartedTotal.labels(String(dbWrite), workspaceCluster, WorkspaceType[type]).inc();
}
reportWorkspaceInstanceUpdateCompleted(
durationSeconds: number,
dbWrite: boolean,
workspaceCluster: string,
type: WorkspaceType,
error?: Error,
): void {
const outcome = error ? "error" : "success";
this.workspaceInstanceUpdateCompletedSeconds
.labels(String(dbWrite), workspaceCluster, WorkspaceType[type], outcome)
.observe(durationSeconds);
}
increasePrebuildsCompletedCounter(state: string) {
this.prebuildsCompletedTotal.inc({ state });
}
}