/
ServerHealth.ts
65 lines (63 loc) · 1.9 KB
/
ServerHealth.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
import fetch from 'cross-fetch'
import { Connection } from './Connection'
import { Tip } from '@cardano-ogmios/schema'
/**
* Captures the health of the server, including metrics and synchronization progress.
*
* This is useful to monitor whether the server is running (or simply, if the underlying
* node is ready to receive requests). `lastTipUpdate` will be null if the server hasn't
* received any tip update from the Cardano node which means that either:
*
* - There's an error with the connection to the node
* - The node is still starting and checking the integrity of its database.
*
* The node checks the database integrity on each restart; in some cases it can take several
* minutes. While doing so, it does not allow any external client to connect.
*
* See also {@link InteractionContext} for an easy way to manage connection establishment with
* a bootstrapping node.
*
* @category Connection
*/
export interface ServerHealth {
currentEra: 'Alonzo' | 'Byron' | 'Mary' | 'Shelley'
lastKnownTip: Tip,
lastTipUpdate: string | null,
metrics: {
runtimeStats?: {
gcCpuTime: number,
cpuTime: number,
maxHeapSize: number,
currentHeapSize: number
},
sessionDurations: {
max: number,
mean: number,
min: number
},
totalConnections: number,
totalMessages: number,
totalUnrouted: number,
activeConnections: number
},
startTime: string,
networkSynchronization: number,
}
/**
* Get the server health. This can be safely polled at regular intervals for monitoring.
*
* @category Connection
*/
export const getServerHealth = async (
options?: {
connection?: Connection
}
): Promise<ServerHealth> => {
const response = await fetch(`${options?.connection?.address.http}/health`)
const responseJson = await response.json()
if (response.ok) {
return responseJson
} else {
throw new Error(response.statusText)
}
}