Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: track content bytes total metric #1199

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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