Skip to content

Commit

Permalink
feat: open WebSocket and streams metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Jul 18, 2024
1 parent a34d0d7 commit 546b202
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
41 changes: 33 additions & 8 deletions lib/Prometheus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,40 @@ class Prometheus {
}),
);

const swapStatusCacheCount = new Gauge({
name: `${Prometheus.metric_prefix}swap_status_cache_count`,
help: 'swap status messages cached',
});
setInterval(
() => swapStatusCacheCount.set(this.api.swapInfos.cacheSize),
Prometheus.gaugeUpdateInterval,
this.registerGauge(
this.swapRegistry!,
'swap_status_cache_count',
'number of swap status messages cached',
() => this.api.swapInfos.cacheSize,
);

this.registerGauge(
this.swapRegistry!,
'pending_stream_count',
'pending SSE swap status stream count',
() => this.api.controller.pendingStreamCount,
);

this.registerGauge(
this.swapRegistry!,
'open_websocket_count',
'open WebSockets count',
() => this.api.websocket.openWebSocketCount,
);
this.swapRegistry!.registerMetric(swapStatusCacheCount);
};

private registerGauge = (
registry: Registry,
name: string,
help: string,
cb: () => number,
) => {
const gauge = new Gauge({
help,
name: `${Prometheus.metric_prefix}${name}`,
});
setInterval(() => gauge.set(cb()), Prometheus.gaugeUpdateInterval);
registry.registerMetric(gauge);
};
}

Expand Down
5 changes: 2 additions & 3 deletions lib/api/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import WebSocketHandler from './v2/WebSocketHandler';

class Api {
public readonly swapInfos: SwapInfos;
public readonly controller: Controller;
public readonly websocket: WebSocketHandler;

private app: Application;

private readonly websocket: WebSocketHandler;
private readonly controller: Controller;

constructor(
private readonly logger: Logger,
private readonly config: ApiConfig,
Expand Down
4 changes: 4 additions & 0 deletions lib/api/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class Controller {
});
}

public get pendingStreamCount() {
return this.pendingSwapStreams.size;
}

// Static files
public serveFile = (fileName: string) => {
return (_: Request, res: Response): void => {
Expand Down
4 changes: 4 additions & 0 deletions lib/api/v2/WebSocketHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class WebSocketHandler {
this.listenSwapUpdates();
}

public get openWebSocketCount() {
return this.socketToSwaps.size;
}

public register = (server: http.Server) => {
server.on('upgrade', (request, socket, head) => {
this.ws.handleUpgrade(request, socket, head, (ws) => {
Expand Down

0 comments on commit 546b202

Please sign in to comment.