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

CON-181 Add prometheus metrics to CN #3166

Merged
merged 25 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 12 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
47 changes: 34 additions & 13 deletions creator-node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions creator-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"multer": "^1.4.0",
"pg": "^8.0.3",
"prettier-config-standard": "^4.0.0",
"prom-client": "^14.0.1",
"promise.any": "^2.0.2",
"rate-limit-redis": "^1.7.0",
"sequelize": "^4.44.4",
Expand Down
1 change: 1 addition & 0 deletions creator-node/src/apiHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports.handleResponse = (func) => {
}

sendResponse(req, res, resp)

next()
} catch (error) {
genericLogger.error('HandleResponse', error)
Expand Down
19 changes: 18 additions & 1 deletion creator-node/src/monitors/filesystem.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
const si = require('systeminformation')
const disk = require('diskusage')

const DiskManager = require('../diskManager')
const serviceRegistry = require('../serviceRegistry')

const prometheusRegistry = serviceRegistry.prometheusRegistry

const getStoragePathSize = async () => {
const storagePath = DiskManager.getConfigStoragePath()
const { total } = await disk.check(storagePath)

const storagePathSizeGaugeMetric = prometheusRegistry.getMetric(
prometheusRegistry.metricNames.STORAGE_PATH_SIZE_GAUGE
)
storagePathSizeGaugeMetric.set(total)

return total
}

const getStoragePathUsed = async () => {
const storagePath = DiskManager.getConfigStoragePath()
const { available, total } = await disk.check(storagePath)
return total - available
const used = total - available

const storagePathUsedGaugeMetric = prometheusRegistry.getMetric(
prometheusRegistry.metricNames.STORAGE_PATH_USED_GAUGE
)
storagePathUsedGaugeMetric.set(total)
SidSethi marked this conversation as resolved.
Show resolved Hide resolved

return used
}

// We first check '/var/k8s' in case the service operator has elected to
Expand Down
9 changes: 9 additions & 0 deletions creator-node/src/routes/prometheusMetricsRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = function (app) {
app.get('/prometheus_metrics', async (req, res) => {
const prometheusRegistry = req.app.get('serviceRegistry').prometheusRegistry
const metricData = await prometheusRegistry.getAllMetricData()

res.setHeader('Content-Type', prometheusRegistry.registry.contentType)
return res.end(metricData)
})
}
16 changes: 16 additions & 0 deletions creator-node/src/routes/tracks.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,17 @@ module.exports = function (app) {
transcodedTrackUUID
} = req.body

const prometheusRegistry =
req.app.get('serviceRegistry').prometheusRegistry
const routePostTracksDurationSecondsMetric = prometheusRegistry.getMetric(
prometheusRegistry.metricNames
.ROUTE_POST_TRACKS_DURATION_SECONDS_HISTOGRAM
)
const metricEndTimerFn = routePostTracksDurationSecondsMetric.startTimer()

// Input validation
if (!blockchainTrackId || !blockNumber || !metadataFileUUID) {
metricEndTimerFn({ code: 400 })
SidSethi marked this conversation as resolved.
Show resolved Hide resolved
return errorResponseBadRequest(
'Must include blockchainTrackId, blockNumber, and metadataFileUUID.'
)
Expand All @@ -314,6 +323,7 @@ module.exports = function (app) {
// Error on outdated blocknumber
const cnodeUser = req.session.cnodeUser
if (blockNumber < cnodeUser.latestBlockNumber) {
metricEndTimerFn({ code: 400 })
return errorResponseBadRequest(
`Invalid blockNumber param ${blockNumber}. Must be greater or equal to previously processed blocknumber ${cnodeUser.latestBlockNumber}.`
)
Expand All @@ -325,6 +335,7 @@ module.exports = function (app) {
where: { fileUUID: metadataFileUUID, cnodeUserUUID }
})
if (!file) {
metricEndTimerFn({ code: 400 })
return errorResponseBadRequest(
`No file db record found for provided metadataFileUUID ${metadataFileUUID}.`
)
Expand All @@ -339,11 +350,13 @@ module.exports = function (app) {
!Array.isArray(metadataJSON.track_segments) ||
!metadataJSON.track_segments.length
) {
metricEndTimerFn({ code: 500 })
return errorResponseServerError(
`Malformatted metadataJSON stored for metadataFileUUID ${metadataFileUUID}.`
)
}
} catch (e) {
metricEndTimerFn({ code: 500 })
return errorResponseServerError(
`No file stored on disk for metadataFileUUID ${metadataFileUUID} at storagePath ${file.storagePath}.`
)
Expand All @@ -357,6 +370,7 @@ module.exports = function (app) {
metadataJSON.cover_art_sizes
)
} catch (e) {
metricEndTimerFn({ code: 500 })
return errorResponseServerError(e.message)
}

Expand Down Expand Up @@ -543,10 +557,12 @@ module.exports = function (app) {

await issueAndWaitForSecondarySyncRequests(req)

metricEndTimerFn({ code: 200 })
return successResponse()
} catch (e) {
req.logger.error(e.message)
await transaction.rollback()
metricEndTimerFn({ code: 500 })
SidSethi marked this conversation as resolved.
Show resolved Hide resolved
return errorResponseServerError(e.message)
}
})
Expand Down
12 changes: 8 additions & 4 deletions creator-node/src/serviceRegistry.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const { createBullBoard } = require('@bull-board/api')
const { BullAdapter } = require('@bull-board/api/bullAdapter')
const { ExpressAdapter } = require('@bull-board/express')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just moved lines from below

const { libs: AudiusLibs } = require('@audius/sdk')
const redisClient = require('./redis')
const BlacklistManager = require('./blacklistManager')
Expand All @@ -6,10 +10,6 @@ const config = require('./config')
const URSMRegistrationManager = require('./services/URSMRegistrationManager')
const { logger } = require('./logging')
const utils = require('./utils')
const { createBullBoard } = require('@bull-board/api')
const { BullAdapter } = require('@bull-board/api/bullAdapter')
const { ExpressAdapter } = require('@bull-board/express')

const MonitoringQueue = require('./monitors/MonitoringQueue')
const SyncQueue = require('./services/sync/syncQueue')
const SkippedCIDsRetryQueue = require('./services/sync/skippedCIDsRetryService')
Expand All @@ -19,6 +19,7 @@ const TrustedNotifierManager = require('./services/TrustedNotifierManager')
const ImageProcessingQueue = require('./ImageProcessingQueue')
const TranscodingQueue = require('./TranscodingQueue')
const StateMachineManager = require('./services/stateMachineManager')
const PrometheusRegistry = require('./services/prometheusMonitoring/prometheusRegistry')

/**
* `ServiceRegistry` is a container responsible for exposing various
Expand Down Expand Up @@ -54,6 +55,7 @@ class ServiceRegistry {
this.syncQueue = null
this.skippedCIDsRetryQueue = null
this.trustedNotifierManager = null
this.prometheusRegistry = null

this.servicesInitialized = false
this.servicesThatRequireServerInitialized = false
Expand Down Expand Up @@ -142,6 +144,8 @@ class ServiceRegistry {
* - create bull queue monitoring dashboard, which needs other server-dependent services to be running
*/
async initServicesThatRequireServer(app) {
this.prometheusRegistry = new PrometheusRegistry()

// Cannot progress without recovering spID from node's record on L1 ServiceProviderFactory contract
// Retries indefinitely
await this._recoverNodeL1Identity()
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const promClient = require('prom-client')

const METRIC_PREFIX = 'cn_'
SidSethi marked this conversation as resolved.
Show resolved Hide resolved

/**
* @notice Counter and Summary metric types are currently disabled, since we should almost never be using them.
* - Gauge should always be used over Counter since it can be decreased, with no performance overhead.
* - Histogram should always be used over Summary, per docs below
* https://prometheus.io/docs/tutorials/understanding_metric_types/
*/
const MetricTypes = Object.freeze({
GAUGE: promClient.Gauge,
HISTOGRAM: promClient.Histogram
// COUNTER: promClient.Counter,
// SUMMARY: promClient.Summary
})

const MetricNames = Object.freeze({
STORAGE_PATH_SIZE_GAUGE: 'storage_path_size_bytes',
STORAGE_PATH_USED_GAUGE: 'storage_path_used_bytes',
SidSethi marked this conversation as resolved.
Show resolved Hide resolved
ROUTE_POST_TRACKS_DURATION_SECONDS_HISTOGRAM:
'route_post_tracks_duration_seconds'
})
SidSethi marked this conversation as resolved.
Show resolved Hide resolved

const Metrics = Object.freeze({
[MetricNames.STORAGE_PATH_SIZE_GAUGE]: {
metricType: MetricTypes.GAUGE,
metricConfig: {
name: MetricNames.STORAGE_PATH_SIZE_GAUGE,
help: 'Total disk space (free + used) (bytes)'
}
},
[MetricNames.STORAGE_PATH_USED_GAUGE]: {
metricType: MetricTypes.GAUGE,
metricConfig: {
name: MetricNames.STORAGE_PATH_USED_GAUGE,
help: 'Used disk space (bytes)'
}
},
[MetricNames.ROUTE_POST_TRACKS_DURATION_SECONDS_HISTOGRAM]: {
metricType: MetricTypes.HISTOGRAM,
metricConfig: {
name: MetricNames.ROUTE_POST_TRACKS_DURATION_SECONDS_HISTOGRAM,
help: 'Duration for POST /tracks route (seconds)',
labelNames: ['code'],
buckets: [0.1, 0.3, 0.5, 1, 3, 5, 10] // 0.1 to 10 seconds
}
}
})
SidSethi marked this conversation as resolved.
Show resolved Hide resolved

module.exports.METRIC_PREFIX = METRIC_PREFIX
module.exports.MetricTypes = MetricTypes
module.exports.MetricNames = MetricNames
module.exports.Metrics = Metrics
Loading