Skip to content

Commit c9e8b00

Browse files
committed
Add counter metric for WebSocket unavailability
1 parent 36d3c0d commit c9e8b00

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

ui/frontend/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import PageSwitcher from './PageSwitcher';
2424
import playgroundApp from './reducers';
2525
import Router from './Router';
2626
import configureStore from './configureStore';
27+
import openWebSocket from './websocket';
2728

28-
const wsUri = '/websocket';
29-
const socket = new WebSocket(wsUri);
29+
// Might be null;
30+
const socket = openWebSocket(window.location);
3031

3132
const store = configureStore(window);
3233

ui/frontend/websocket.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export default function openWebSocket(currentLocation: Location) {
2+
try {
3+
const wsProtocol = currentLocation.protocol === 'https:' ? 'wss://' : 'ws://';
4+
const wsUri = [wsProtocol, currentLocation.host, '/websocket'].join('');
5+
return new WebSocket(wsUri);
6+
} catch {
7+
// WebSocket URL error or WebSocket is not supported by browser.
8+
// Assume it's the second case since URL error is easy to notice.
9+
(async () => {
10+
try {
11+
await fetch('/nowebsocket', {
12+
method: 'post',
13+
headers: {
14+
'Content-Length': '0',
15+
},
16+
});
17+
} catch (e) {
18+
console.log('Error:', e);
19+
}
20+
})();
21+
return null;
22+
}
23+
}

ui/src/metrics.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use futures::future::BoxFuture;
22
use lazy_static::lazy_static;
33
use prometheus::{
4-
self, register_histogram, register_histogram_vec, register_int_gauge, Histogram, HistogramVec,
5-
IntGauge,
4+
self, register_histogram, register_histogram_vec, register_int_counter, register_int_gauge,
5+
Histogram, HistogramVec, IntCounter, IntGauge,
66
};
77
use regex::Regex;
88
use std::{future::Future, time::Instant};
@@ -28,6 +28,11 @@ lazy_static! {
2828
vec![15.0, 60.0, 300.0, 600.0, 1800.0, 3600.0, 7200.0]
2929
)
3030
.unwrap();
31+
pub(crate) static ref UNAVAILABLE_WS: IntCounter = register_int_counter!(
32+
"websocket_unavailability_count",
33+
"Number of failed WebSocket connections"
34+
)
35+
.unwrap();
3136
}
3237

3338
#[derive(Debug, Copy, Clone, strum::IntoStaticStr)]

ui/src/server_axum.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
gist,
33
metrics::{
44
track_metric_async, track_metric_force_endpoint_async, track_metric_no_request_async,
5-
Endpoint, GenerateLabels, SuccessDetails, DURATION_WS, LIVE_WS,
5+
Endpoint, GenerateLabels, SuccessDetails, DURATION_WS, LIVE_WS, UNAVAILABLE_WS,
66
},
77
sandbox::{self, Channel, Sandbox},
88
CachingSnafu, ClippyRequest, ClippyResponse, CompilationSnafu, CompileRequest, CompileResponse,
@@ -83,6 +83,7 @@ pub(crate) async fn serve(config: Config) {
8383
.route("/meta/gist/:id", get(meta_gist_get))
8484
.route("/metrics", get(metrics))
8585
.route("/websocket", get(websocket))
86+
.route("/nowebsocket", post(nowebsocket))
8687
.layer(Extension(Arc::new(SandboxCache::default())))
8788
.layer(Extension(config.github_token()));
8889

@@ -404,6 +405,10 @@ async fn handle_socket(mut socket: WebSocket) {
404405
DURATION_WS.observe(elapsed.as_secs_f64());
405406
}
406407

408+
async fn nowebsocket() {
409+
UNAVAILABLE_WS.inc();
410+
}
411+
407412
#[derive(Debug)]
408413
struct MetricsAuthorization;
409414

0 commit comments

Comments
 (0)