1
+ import { db , privateDB } from "../databases/databases" ;
2
+ import { Request , Response } from "express" ;
3
+ import os from "os" ;
4
+ import redis from "../utils/redis" ;
5
+ import { Postgres } from "../databases/Postgres" ;
6
+ import { Server } from "http" ;
7
+
8
+ export async function getMetrics ( req : Request , res : Response , server : Server ) : Promise < Response > {
9
+ return res . type ( "text" ) . send ( [
10
+ `# HELP sb_uptime Uptime of this instance` ,
11
+ `# TYPE sb_uptime counter` ,
12
+ `sb_uptime ${ process . uptime ( ) } ` ,
13
+ `# HELP sb_commit The commit hash of the running version` ,
14
+ `# TYPE sb_commit gauge` ,
15
+ `sb_commit ${ ( global as any ) . HEADCOMMIT ?? "unknown" } ` ,
16
+ `# HELP sb_db_version The version of the database` ,
17
+ `# TYPE sb_db_version counter` ,
18
+ `sb_db_version ${ await db . prepare ( "get" , "SELECT key, value FROM config where key = ?" , [ "version" ] ) . then ( e => e . value ) . catch ( ( ) => - 1 ) } ` ,
19
+ `# HELP sb_start_time The time this instance was started` ,
20
+ `# TYPE sb_start_time gauge` ,
21
+ `sb_start_time ${ Date . now ( ) } ` ,
22
+ `# HELP sb_loadavg_5 The 5 minute load average of the system` ,
23
+ `# TYPE sb_loadavg_5 gauge` ,
24
+ `sb_loadavg_5 ${ os . loadavg ( ) [ 0 ] } ` ,
25
+ `# HELP sb_loadavg_15 The 15 minute load average of the system` ,
26
+ `# TYPE sb_loadavg_15 gauge` ,
27
+ `sb_loadavg_15 ${ os . loadavg ( ) [ 1 ] } ` ,
28
+ `# HELP sb_connections The number of connections to this instance` ,
29
+ `# TYPE sb_connections gauge` ,
30
+ `sb_connections ${ await new Promise ( ( resolve ) => server . getConnections ( ( _ , count ) => resolve ( count ) ) as any ) } ` ,
31
+ `# HELP sb_status_requests The number of status requests made to this instance` ,
32
+ `# TYPE sb_status_requests gauge` ,
33
+ `sb_status_requests ${ await redis . increment ( "statusRequest" ) . then ( e => e [ 0 ] ) . catch ( ( ) => - 1 ) } ` ,
34
+ `# HELP sb_postgres_active_requests The number of active requests to the postgres database` ,
35
+ `# TYPE sb_postgres_active_requests gauge` ,
36
+ `sb_postgres_active_requests ${ ( db as Postgres ) ?. getStats ?.( ) ?. activeRequests ?? - 1 } ` ,
37
+ `# HELP sb_postgres_avg_read_time The average read time of the postgres database` ,
38
+ `# TYPE sb_postgres_avg_read_time gauge` ,
39
+ `sb_postgres_avg_read_time ${ ( db as Postgres ) ?. getStats ?.( ) ?. avgReadTime ?? - 1 } ` ,
40
+ `# HELP sb_postgres_avg_write_time The average write time of the postgres database` ,
41
+ `# TYPE sb_postgres_avg_write_time gauge` ,
42
+ `sb_postgres_avg_write_time ${ ( db as Postgres ) ?. getStats ?.( ) ?. avgWriteTime ?? - 1 } ` ,
43
+ `# HELP sb_postgres_avg_failed_time The average failed time of the postgres database` ,
44
+ `# TYPE sb_postgres_avg_failed_time gauge` ,
45
+ `sb_postgres_avg_failed_time ${ ( db as Postgres ) ?. getStats ?.( ) ?. avgFailedTime ?? - 1 } ` ,
46
+ `# HELP sb_postgres_pool_total The total number of connections in the postgres pool` ,
47
+ `# TYPE sb_postgres_pool_total gauge` ,
48
+ `sb_postgres_pool_total ${ ( db as Postgres ) ?. getStats ?.( ) ?. pool ?. total ?? - 1 } ` ,
49
+ `# HELP sb_postgres_pool_idle The number of idle connections in the postgres pool` ,
50
+ `# TYPE sb_postgres_pool_idle gauge` ,
51
+ `sb_postgres_pool_idle ${ ( db as Postgres ) ?. getStats ?.( ) ?. pool ?. idle ?? - 1 } ` ,
52
+ `# HELP sb_postgres_pool_waiting The number of connections waiting in the postgres pool` ,
53
+ `# TYPE sb_postgres_pool_waiting gauge` ,
54
+ `sb_postgres_pool_waiting ${ ( db as Postgres ) ?. getStats ?.( ) ?. pool ?. waiting ?? - 1 } ` ,
55
+ `# HELP sb_postgres_private_active_requests The number of active requests to the private postgres database` ,
56
+ `# TYPE sb_postgres_private_active_requests gauge` ,
57
+ `sb_postgres_private_active_requests ${ ( privateDB as Postgres ) ?. getStats ?.( ) ?. activeRequests ?? - 1 } ` ,
58
+ `# HELP sb_postgres_private_avg_read_time The average read time of the private postgres database` ,
59
+ `# TYPE sb_postgres_private_avg_read_time gauge` ,
60
+ `sb_postgres_private_avg_read_time ${ ( privateDB as Postgres ) ?. getStats ?.( ) ?. avgReadTime ?? - 1 } ` ,
61
+ `# HELP sb_postgres_private_avg_write_time The average write time of the private postgres database` ,
62
+ `# TYPE sb_postgres_private_avg_write_time gauge` ,
63
+ `sb_postgres_private_avg_write_time ${ ( privateDB as Postgres ) ?. getStats ?.( ) ?. avgWriteTime ?? - 1 } ` ,
64
+ `# HELP sb_postgres_private_avg_failed_time The average failed time of the private postgres database` ,
65
+ `# TYPE sb_postgres_private_avg_failed_time gauge` ,
66
+ `sb_postgres_private_avg_failed_time ${ ( privateDB as Postgres ) ?. getStats ?.( ) ?. avgFailedTime ?? - 1 } ` ,
67
+ `# HELP sb_postgres_private_pool_total The total number of connections in the private postgres pool` ,
68
+ `# TYPE sb_postgres_private_pool_total gauge` ,
69
+ `sb_postgres_private_pool_total ${ ( privateDB as Postgres ) ?. getStats ?.( ) ?. pool ?. total ?? - 1 } ` ,
70
+ `# HELP sb_postgres_private_pool_idle The number of idle connections in the private postgres pool` ,
71
+ `# TYPE sb_postgres_private_pool_idle gauge` ,
72
+ `sb_postgres_private_pool_idle ${ ( privateDB as Postgres ) ?. getStats ?.( ) ?. pool ?. idle ?? - 1 } ` ,
73
+ `# HELP sb_postgres_private_pool_waiting The number of connections waiting in the private postgres pool` ,
74
+ `# TYPE sb_postgres_private_pool_waiting gauge` ,
75
+ `sb_postgres_private_pool_waiting ${ ( privateDB as Postgres ) ?. getStats ?.( ) ?. pool ?. waiting ?? - 1 } ` ,
76
+ `# HELP sb_redis_active_requests The number of active requests to redis` ,
77
+ `# TYPE sb_redis_active_requests gauge` ,
78
+ `sb_redis_active_requests ${ ( redis as any ) ?. getStats ?.( ) ?. activeRequests ?? - 1 } ` ,
79
+ `# HELP sb_redis_write_requests The number of write requests to redis` ,
80
+ `# TYPE sb_redis_write_requests gauge` ,
81
+ `sb_redis_write_requests ${ ( redis as any ) ?. getStats ?.( ) ?. writeRequests ?? - 1 } ` ,
82
+ `# HELP sb_redis_avg_read_time The average read time of redis` ,
83
+ `# TYPE sb_redis_avg_read_time gauge` ,
84
+ `sb_redis_avg_read_time ${ ( redis as any ) ?. getStats ?.( ) ?. avgReadTime ?? - 1 } ` ,
85
+ `# HELP sb_redis_avg_write_time The average write time of redis` ,
86
+ `# TYPE sb_redis_avg_write_time gauge` ,
87
+ `sb_redis_avg_write_time ${ ( redis as any ) ?. getStats ?.( ) ?. avgWriteTime ?? - 1 } ` ,
88
+ `# HELP sb_redis_memory_cache_hits The cache hit ratio in redis` ,
89
+ `# TYPE sb_redis_memory_cache_hits gauge` ,
90
+ `sb_redis_memory_cache_hits ${ ( redis as any ) ?. getStats ?.( ) ?. memoryCacheHits ?? - 1 } ` ,
91
+ `# HELP sb_redis_memory_cache_total_hits The cache hit ratio in redis including uncached items` ,
92
+ `# TYPE sb_redis_memory_cache_total_hits gauge` ,
93
+ `sb_redis_memory_cache_total_hits ${ ( redis as any ) ?. getStats ?.( ) ?. memoryCacheTotalHits ?? - 1 } ` ,
94
+ `# HELP sb_redis_memory_cache_length The length of the memory cache in redis` ,
95
+ `# TYPE sb_redis_memory_cache_length gauge` ,
96
+ `sb_redis_memory_cache_length ${ ( redis as any ) ?. getStats ?.( ) ?. memoryCacheLength ?? - 1 } ` ,
97
+ `# HELP sb_redis_memory_cache_size The size of the memory cache in redis` ,
98
+ `# TYPE sb_redis_memory_cache_size gauge` ,
99
+ `sb_redis_memory_cache_size ${ ( redis as any ) ?. getStats ?.( ) ?. memoryCacheSize ?? - 1 } ` ,
100
+ `# HELP sb_redis_last_invalidation The time of the last successful invalidation in redis` ,
101
+ `# TYPE sb_redis_last_invalidation gauge` ,
102
+ `sb_redis_last_invalidation ${ ( redis as any ) ?. getStats ?.( ) ?. lastInvalidation ?? - 1 } ` ,
103
+ `# HELP sb_redis_last_invalidation_message The time of the last invalidation message in redis` ,
104
+ `# TYPE sb_redis_last_invalidation_message gauge` ,
105
+ `sb_redis_last_invalidation_message ${ ( redis as any ) ?. getStats ?.( ) ?. lastInvalidationMessage ?? - 1 } ` ,
106
+ ] . join ( "\n" ) ) ;
107
+ }
0 commit comments