Skip to content

Commit

Permalink
feat: track content bytes total metric (#1199)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Feb 3, 2022
1 parent cfb2616 commit 695e4a5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
46 changes: 26 additions & 20 deletions packages/api/src/routes/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,38 @@ export async function metrics(_, { db }) {
* @returns {Promise<string>}
*/
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(
Expand Down
13 changes: 13 additions & 0 deletions packages/cron/src/jobs/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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))
Expand All @@ -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
Expand Down

0 comments on commit 695e4a5

Please sign in to comment.