Skip to content

Commit 29d20c7

Browse files
authored
Merge pull request #9094 from tangledbytes/utkarsh/feat/add-disk-cache-metrics
[NC | NSFS | GLACIER] Add disk usage info to nsfs metrics
2 parents ba9e8d8 + 752a89b commit 29d20c7

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,11 @@ config.NSFS_GLACIER_FORCE_EXPIRE_ON_GET = false;
966966
// interval
967967
config.NSFS_GLACIER_MIGRATE_LOG_THRESHOLD = 50 * 1024;
968968

969+
// NSFS_GLACIER_METRICS_STAT_PATHS if set NooBaa will start reporting the statfs info of that
970+
// path as part of its metrics report
971+
config.NSFS_GLACIER_METRICS_STATFS_PATHS = [];
972+
config.NSFS_GLACIER_METRICS_STATFS_INTERVAL = 60 * 1000; // Refresh statfs value every minute
973+
969974
/**
970975
* NSFS_GLACIER_RESERVED_BUCKET_TAGS defines an object of bucket tags which will be reserved
971976
* by the system and PUT operations for them via S3 API would be limited - as in they would be

src/server/analytic_services/prometheus_reporting.js

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const stats_aggregator = require('../system_services/stats_aggregator');
1313
const AggregatorRegistry = require('prom-client').AggregatorRegistry;
1414
const aggregatorRegistry = new AggregatorRegistry();
1515
const http_utils = require('../../util/http_utils');
16+
const nb_native = require('../../util/nb_native');
17+
const { get_process_fs_context } = require('../../util/native_fs_utils');
1618

1719
// Currenty supported reprots
1820
const reports = Object.seal({
@@ -51,9 +53,44 @@ async function export_all_metrics() {
5153
return all_metrics.join('\n\n');
5254
}
5355

56+
/** @type {Record<string, Record<string, number>>} */
57+
const nsfs_metrics_statfs_path_cache = {};
58+
59+
/**
60+
*
61+
* @param {Record<string, number>} statfs
62+
* @returns
63+
*/
64+
function get_disk_usage_perc(statfs) {
65+
if (!statfs || !statfs.blocks || typeof statfs.bfree !== 'number') return undefined;
66+
const ratio = (statfs.blocks - statfs.bfree) / statfs.blocks;
67+
return Number((ratio * 100).toFixed(3));
68+
}
69+
70+
/**
71+
* @param {Record<string, Record<string, number>>} statfs_map
72+
*/
73+
function get_disk_usage_report(statfs_map) {
74+
return Object.keys(statfs_map).reduce((acc, curr) => ({ ...acc, [curr]: get_disk_usage_perc(statfs_map[curr]) }), {});
75+
}
76+
77+
function setup_statfs_monitor() {
78+
setInterval(async () => {
79+
const fs_context = get_process_fs_context();
80+
await Promise.all(config.NSFS_GLACIER_METRICS_STATFS_PATHS?.map(async path => {
81+
try {
82+
nsfs_metrics_statfs_path_cache[path] = await nb_native().fs.statfs(fs_context, path);
83+
dbg.log4('updated \'nsfs_metrics_statfs_path_cache\' with', nsfs_metrics_statfs_path_cache);
84+
} catch (error) {
85+
dbg.warn('failed to updated \'nsfs_metrics_statfs_path_cache\':', error);
86+
}
87+
}) || []);
88+
}, config.NSFS_GLACIER_METRICS_STATFS_INTERVAL).unref();
89+
}
90+
5491
/**
5592
* Start Noobaa metrics server for http and https server
56-
*
93+
*
5794
* @param {number?} port prometheus metris port.
5895
* @param {number?} ssl_port prometheus metris https port.
5996
* @param {boolean?} fork_enabled request from frok or not.
@@ -71,10 +108,15 @@ async function start_server(
71108
if (!config.PROMETHEUS_ENABLED) {
72109
return;
73110
}
111+
112+
if (config.NSFS_GLACIER_METRICS_STATFS_PATHS?.length) {
113+
setup_statfs_monitor();
114+
}
115+
74116
const metrics_request_handler = async (req, res) => {
75117
if (config.NOOBAA_METRICS_AUTH_ENABLED && !http_utils.authorize_bearer(req, res, [ ROLE_METRICS, ROLE_ADMIN ])) {
76118
// Authorize bearer token metrics endpoint
77-
// Role 'metrics' is used in operator RPC call,
119+
// Role 'metrics' is used in operator RPC call,
78120
// Update operator RPC call first before changing role
79121
return;
80122
}
@@ -86,7 +128,8 @@ async function start_server(
86128
const nsfs_report = {
87129
nsfs_counters: io_stats_complete,
88130
op_stats_counters: ops_stats_complete,
89-
fs_worker_stats_counters: fs_worker_stats_complete
131+
fs_worker_stats_counters: fs_worker_stats_complete,
132+
disk_usage: get_disk_usage_report(nsfs_metrics_statfs_path_cache),
90133
};
91134
res.end(JSON.stringify(nsfs_report));
92135
return;
@@ -218,7 +261,8 @@ async function metrics_nsfs_stats_handler() {
218261
const nsfs_report = {
219262
nsfs_counters: nsfs_io_stats,
220263
op_stats_counters: op_stats_counters,
221-
fs_worker_stats_counters: fs_worker_stats_counters
264+
fs_worker_stats_counters: fs_worker_stats_counters,
265+
disk_usage: get_disk_usage_report(nsfs_metrics_statfs_path_cache),
222266
};
223267
dbg.log1('_create_nsfs_report: nsfs_report', nsfs_report);
224268
return JSON.stringify(nsfs_report);

0 commit comments

Comments
 (0)