Skip to content

Instantly share code, notes, and snippets.

@sebastien-prudhomme
Created April 20, 2020 18:25
Show Gist options
  • Save sebastien-prudhomme/93ab307c8c03a9c5fdb1ff728f413855 to your computer and use it in GitHub Desktop.
Save sebastien-prudhomme/93ab307c8c03a9c5fdb1ff728f413855 to your computer and use it in GitHub Desktop.
Instrumenting FeathersJS with Prometheus
const prometheus = require('./hooks/prometheus')
module.exports = {
before: {
all: [prometheus()],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
after: {
all: [prometheus()],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
}
...
const prometheus = require('./prometheus')
...
app.configure(prometheus)
...
module.exports = (options = {}) => {
return async context => {
if (context.type === 'before') {
const labels = {
path: context.path,
method: context.method
}
context.endTimer = context.app.get('prometheusFeathersRequestsDurationSeconds').startTimer(labels)
}
if (context.type === 'after') {
context.endTimer()
}
return context
}
}
const prometheusClient = require('prom-client')
module.exports = function (app) {
prometheusClient.collectDefaultMetrics()
const prometheusFeathersRequestsDurationSeconds = new prometheusClient.Histogram({
name: 'feathers_requests_duration_seconds',
help: 'Duration in seconds of Feathers requests',
labelNames: ['path', 'method'],
buckets: [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10]
})
app.use('/metrics', (req, res) => {
res.set('content-type', prometheusClient.register.contentType)
res.end(prometheusClient.register.metrics())
})
app.set('prometheusFeathersRequestsDurationSeconds', prometheusFeathersRequestsDurationSeconds)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment