How to use express-prometheus-middleware - 1 common examples

To help you get started, we’ve selected a few express-prometheus-middleware examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github NERC-CEH / datalab / code / workspaces / service-chassis / src / service / middleware.js View on Github external
import { validationResult } from 'express-validator';
import prometheusMiddleware from 'express-prometheus-middleware';

const validator = (validations, logger) => async (req, res, next) => {
  await Promise.all(validations.map(validation => validation.run(req)));

  const errors = validationResult(req);
  if (errors.isEmpty()) {
    return next();
  }

  logger.debug('Error validating request', errors.array());
  return res.status(400).json({ errors: errors.array() });
};

const metricsMiddleware = prometheusMiddleware({
  metricsPath: '/metrics',
  collectDefaultMetrics: true,
  requestDurationBuckets: [0.1, 0.5, 1, 1.5],
});

// Error wrapper function to allow controller functions to omit try/catch block
const errorWrapper = fn => (req, res, next) => {
  Promise.resolve(fn(req, res, next)).catch(next);
};

const createErrorHandler = logger => (error, request, response, next) => { // eslint-disable-line no-unused-vars
  const errorObject = {
    method: request.method,
    url: request.url,
    message: error.message,
  };

express-prometheus-middleware

RED/USE metrics for express applications

MIT
Latest version published 4 years ago

Package Health Score

53 / 100
Full package analysis

Popular express-prometheus-middleware functions