From 695e4a5d7bbefc125291cfd001a743683efb6808 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Thu, 3 Feb 2022 10:26:38 +0000 Subject: [PATCH] feat: track content bytes total metric (#1199) --- packages/api/src/routes/metrics.js | 46 +++++++++++++++++------------- packages/cron/src/jobs/metrics.js | 13 +++++++++ 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/packages/api/src/routes/metrics.js b/packages/api/src/routes/metrics.js index 162c0e762a..292cd0577a 100644 --- a/packages/api/src/routes/metrics.js +++ b/packages/api/src/routes/metrics.js @@ -15,32 +15,38 @@ export async function metrics(_, { db }) { * @returns {Promise} */ async function exportPromMetrics(db) { - const [usersTotal, uploadsMetrics, pinsMetrics] = await Promise.all([ - db.getMetric('users_total'), - Promise.all( - UPLOAD_TYPES.map(async (t) => ({ - type: t, - total: await db.getMetric(`uploads_${t.toLowerCase()}_total`), - })) - ), - Promise.all( - PIN_SERVICES.map(async (svc) => ({ - service: svc, - totals: await Promise.all( - PIN_STATUSES.map(async (s) => { - const name = `pins_${svc.toLowerCase()}_${s.toLowerCase()}_total` - return { status: s, total: await db.getMetric(name) } - }) - ), - })) - ), - ]) + const [usersTotal, contentDagSizeTotal, uploadsMetrics, pinsMetrics] = + await Promise.all([ + db.getMetric('users_total'), + db.getMetric('content_dag_size_total'), + Promise.all( + UPLOAD_TYPES.map(async (t) => ({ + type: t, + total: await db.getMetric(`uploads_${t.toLowerCase()}_total`), + })) + ), + Promise.all( + PIN_SERVICES.map(async (svc) => ({ + service: svc, + totals: await Promise.all( + PIN_STATUSES.map(async (s) => { + const name = `pins_${svc.toLowerCase()}_${s.toLowerCase()}_total` + return { status: s, total: await db.getMetric(name) } + }) + ), + })) + ), + ]) return [ '# HELP nftstorage_users_total Total users registered.', '# TYPE nftstorage_users_total counter', `nftstorage_users_total ${usersTotal || 0}`, + '# HELP nftstorage_content_bytes_total Total bytes of all root DAGs stored.', + '# TYPE nftstorage_content_bytes_total counter', + `nftstorage_content_bytes_total ${contentDagSizeTotal || 0}`, + '# HELP nftstorage_uploads_total Total number of uploads by type.', '# TYPE nftstorage_uploads_total counter', ...uploadsMetrics.map( diff --git a/packages/cron/src/jobs/metrics.js b/packages/cron/src/jobs/metrics.js index 0a67dfadb1..fa9516a060 100644 --- a/packages/cron/src/jobs/metrics.js +++ b/packages/cron/src/jobs/metrics.js @@ -17,6 +17,8 @@ const COUNT_UPLOADS = 'SELECT COUNT(*) AS total FROM upload WHERE type = $1' const COUNT_PINS = 'SELECT COUNT(*) AS total FROM pin WHERE service = $1 AND status = $2' +const SUM_CONTENT_DAG_SIZE = `SELECT SUM(c.dag_size) AS "total" FROM content c` + const UPDATE_METRIC = ` INSERT INTO metric (name, value, updated_at) VALUES ($1, $2, TIMEZONE('utc', NOW())) @@ -32,6 +34,7 @@ ON CONFLICT (name) DO UPDATE export async function updateMetrics({ roPg, rwPg }) { const results = await settle([ updateUsersCount(roPg, rwPg), + updateContentRootDagSizeSum(roPg, rwPg), ...UPLOAD_TYPES.map((t) => updateUploadsCount(roPg, rwPg, t)), ...PIN_SERVICES.map((svc) => PIN_STATUSES.map((s) => updatePinsCount(roPg, rwPg, svc, s)) @@ -48,6 +51,16 @@ export async function updateMetrics({ roPg, rwPg }) { if (error) throw error } +/** + * @param {Client} roPg + * @param {Client} rwPg + */ +async function updateContentRootDagSizeSum(roPg, rwPg) { + const { rows } = await roPg.query(SUM_CONTENT_DAG_SIZE) + if (!rows.length) throw new Error('no rows returned counting users') + await rwPg.query(UPDATE_METRIC, ['content_dag_size_total', rows[0].total]) +} + /** * @param {Client} roPg * @param {Client} rwPg