Skip to content

Commit

Permalink
feat(host-manager): add some metrics (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
zccz14 authored Dec 17, 2024
1 parent 40a1e08 commit ee7c1e7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
10 changes: 10 additions & 0 deletions common/changes/@yuants/host-manager/2024-12-17-01-14.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@yuants/host-manager",
"comment": "add some metrics",
"type": "minor"
}
],
"packageName": "@yuants/host-manager"
}
23 changes: 22 additions & 1 deletion libraries/host-manager/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { formatTime, UUID } from '@yuants/data-model';
import { ITerminalInfo, Terminal } from '@yuants/protocol';
import { ITerminalInfo, PromRegistry, Terminal } from '@yuants/protocol';
import { createServer } from 'http';
import {
bindCallback,
Expand Down Expand Up @@ -61,6 +61,16 @@ export interface IHost {
dispose: () => void;
}

const MetricsHostManagerMessageSize = PromRegistry.create('histogram', 'host_manager_message_size');
const MetricsHostManagerConnectionEstablishedCounter = PromRegistry.create(
'counter',
'host_manager_connection_established',
);
const MetricsHostManagerConnectionErrorCounter = PromRegistry.create(
'counter',
'host_manager_connection_error',
);

/**
* @public
*/
Expand Down Expand Up @@ -226,6 +236,7 @@ export const createNodeJSHostManager = (config: IHostManagerConfig): IHostManger
host_id = config.mapHostUrlToHostId(url.toString());
}
} catch (err) {
MetricsHostManagerConnectionErrorCounter.inc();
console.info(formatTime(Date.now()), 'Auth Failed', url.toString(), `${err}`);
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
Expand All @@ -237,6 +248,11 @@ export const createNodeJSHostManager = (config: IHostManagerConfig): IHostManger

wss.handleUpgrade(request, socket, head, (ws) => {
console.info(formatTime(Date.now()), 'Host', host_id, 'terminal connected', terminal_id);
MetricsHostManagerConnectionEstablishedCounter.inc({
host_id,
terminal_id,
has_header: has_header ? 'true' : 'false',
});
const oldTerminal = host.mapTerminalIdToSocket[terminal_id];
if (oldTerminal) {
console.info(formatTime(Date.now()), 'Host', host_id, 'terminal replaced', terminal_id);
Expand All @@ -247,6 +263,11 @@ export const createNodeJSHostManager = (config: IHostManagerConfig): IHostManger
// Forward Terminal Messages
(fromEvent(ws, 'message') as Observable<WebSocket.MessageEvent>).subscribe((origin) => {
const raw_message = origin.data.toString();
MetricsHostManagerMessageSize.observe(raw_message.length, {
host_id,
has_header: has_header ? 'true' : 'false',
source_terminal_id: terminal_id,
});
if (has_header) {
const idx = raw_message.indexOf('\n');
const raw_headers = raw_message.slice(0, idx + 1);
Expand Down

0 comments on commit ee7c1e7

Please sign in to comment.