Git Product home page Git Product logo

sails-hook-prometheus's Introduction

Build Status Quality Gate Status npm npm node NPM

sails-hook-prometheus

Gather Prometheus metrics for your SailsJS application. Also it will opens /metrics endpoint where Prometheus can scrap all statistics.

Installation

Install and save NPM dependency.

npm install --save sails-hook-prometheus

Configuration

You can override default configuration. Create a file in /config/prometheus.js with configuration values listed bellow.

Configuration in depth

All configuration values bellow are optional.

defaultMetrics.enabled (boolean)

module.exports.prometheus = {
  defaultMetrics: {
    enabled: true
  }
}

Enable/disable metric.

defaultMetrics.prefix (string)

module.exports.prometheus = {
  defaultMetrics: {
    prefix: ``
  }
}

Prefix for default collected metrics.

httpMetric.enabled (boolean)

module.exports.prometheus = {
  httpMetric: {
    enabled: true
  }
}

Enable/disable metric.

httpMetric.name (string)

module.exports.prometheus = {
  httpMetric: {
    name: `http_request_duration_seconds`
  }
}

Help text displayed as a metric title under /metrics page.

httpMetric.type (string)

module.exports.prometheus = {
  httpMetric: {
    type: `histogram`
  }
}

You can select between histogram or summary.

httpMetric.help (string)

module.exports.prometheus = {
  httpMetric: {
    help: `duration histogram of http responses labeled with: `
  }
}

Help text displayed next to the metric name.

httpMetric.buckets (array of numbers)

module.exports.prometheus = {
  httpMetric: {
    buckets: [0.003, 0.03, 0.1, 0.3, 1.5, 10]
  }
}

Buckets related to histogram metric.

httpMetric.route.exclude (array of strings)

module.exports.prometheus = {
  httpMetric: {
    exclude: [
      '\\.css$',
      '\\.js$',
      '\\.(ico|gif|jpg|jpeg|png)$',
      '\\.(eot|ttf|woff|woff2|svg)$'
    ]
  }
}

Define which routes are excluded from histogram. Every element in array has to be a regular expression.

httpMetric.urlQueryString (boolean)

module.exports.prometheus = {
  httpMetric: {
    urlQueryString: true
  }
}

Include URL query params in path label. It is true by default but please note your Prometheus server might not be ready for this amount of data (every unique URL).

httpMetric.urlParams (boolean)

module.exports.prometheus = {
  httpMetric: {
    urlParams: true
  }
}

Include URL params in path label. It is true by default but please note your Prometheus server might not be ready for this amount of data (every unique URL).

upMetric.enabled (boolean)

module.exports.prometheus = {
  upMetric: {
    enabled: true
  }
}

Enable/disable metric.

upMetric.name (string)

module.exports.prometheus = {
  upMetric: {
    name: `up`
  }
}

upMetric.help (string)

module.exports.prometheus = {
  upMetric: {
    help: '1 = up, 0 = not up'
  }
}

throughputMetric.enabled (boolean)

module.exports.prometheus = {
  throughputMetric: {
    enabled: false
  }
}

Enable/disable metric.

throughputMetric.name (string)

module.exports.prometheus = {
  throughputMetric: {
    name: `throughput`
  }
}

throughputMetric.help (string)

module.exports.prometheus = {
  throughputMetric: {
    help: 'The number of requests served'
  }
}

sockets.enabled (boolean)

module.exports.prometheus = {
  sockets: {
    enabled: false
  }
}

Log socket requests as well. Due to the fact that status code is reserved for HTTP protocol only, the result of status code is always going to be 0.

Custom metrics

You can add two kind of metrics on your own:

  • counter (increase a metric)
  • gauge (increase or decrease a metric)

Counter metric

let counter = sails.hooks.prometheus.counter.setup({
  name: `testcounter`,
  help: `help to the metric`
})

counter.inc() // increase a metric by default 1

 // increase a metric by 2
counter.inc({
  amount: 2
})

// increase a metric by 10
counter.inc({
  amount: 10
})

Gauge metric

let gauge = sails.hooks.prometheus.gauge.setup({
  name: `testgauged`,
  help: `help to the metric`
})

// increase a metric by default 1
gauge.inc()

// increase a metric by 10
gauge.inc({
  amount: 10
})

// decrease a metric by default 1
gauge.dec()

// decrease a metric by 2
gauge.dec({
  amount: 2
})

// set a metric to 100
gauge.set({
  amount: 100
})

Labels for custom metrics

Both count and gauge metrics comes with labels feature as well.

Example for counter metric

let counter = sails.hooks.prometheus.counter.setup({
  name: `testcounter`,
  help: `help to the metric`
  labelNames: [`counter1`, `counter2`]
})

counter.inc({
  amount: 1,
  labels: {
    counter1: `value`
  }
})

Note: Gauge metric goes the same.

Custom /metrics endpoint

You can configure your own public route by editing /config/routes.js file.

module.exports.routes = {
  'GET /api/v1/metrics': 'prometheus/metrics',
}

Contributors

This project was originally created by Daniel Rataj.

License

MIT

sails-hook-prometheus's People

Contributors

danielrataj avatar dependabot[bot] avatar whipstercz avatar yash2503 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

sails-hook-prometheus's Issues

Issue when .inc() with empty arguments

When we use method "inc()" without arguments, we got an error:

TypeError: Cannot read property 'amount' of undefined at Object.inc (/Users/drataj/workspace/test-project/node_modules/sails-hook-prometheus/src/index.js:100:14)

Do it works with sockets?

verbose: verbo: Interpreting socket.io message as VR (virtual request) to "put /api/v1/login"...
silly:      : Handling virtual request :: Running virtual querystring parser...
silly:      : Handling virtual request :: Running virtual body parser...
silly:      : Handling virtual request :: Running final "error" handler...
error: error: Sending 500 ("Server Error") response:
 TypeError: Cannot read property 'statusCode' of undefined
    at all /* (/certifirm/SailsBE_dev/node_modules/sails-hook-prometheus/src/index.js:43:36)
    at routeTargetFnWrapper (/certifirm/SailsBE_dev/node_modules/sails/lib/router/bind.js:392:9)
    at Layer.handle [as handle_request] (/certifirm/SailsBE_dev/node_modules/router/lib/layer.js:93:5)
    at next (/certifirm/SailsBE_dev/node_modules/router/lib/route.js:144:13)
    at Route.dispatch (/certifirm/SailsBE_dev/node_modules/router/lib/route.js:109:3)
    at handle (/certifirm/SailsBE_dev/node_modules/router/index.js:520:11)
    at Layer.handle [as handle_request] (/certifirm/SailsBE_dev/node_modules/router/lib/layer.js:93:5)
    at /certifirm/SailsBE_dev/node_modules/router/index.js:296:22
    at param (/certifirm/SailsBE_dev/node_modules/router/index.js:373:14)
    at param (/certifirm/SailsBE_dev/node_modules/router/index.js:384:14)
    at Function.process_params (/certifirm/SailsBE_dev/node_modules/router/index.js:429:3)
    at next (/certifirm/SailsBE_dev/node_modules/router/index.js:290:10)
    at next (/certifirm/SailsBE_dev/node_modules/router/lib/route.js:124:14)
    at logRequest (/certifirm/SailsBE_dev/node_modules/sails-hook-apianalytics/lib/private/log-request.middleware.js:109:10)
    at routeTargetFnWrapper (/certifirm/SailsBE_dev/node_modules/sails/lib/router/bind.js:392:9)
    at Layer.handle [as handle_request] (/certifirm/SailsBE_dev/node_modules/router/lib/layer.js:93:5)
    at next (/certifirm/SailsBE_dev/node_modules/router/lib/route.js:144:13)
    at Route.dispatch (/certifirm/SailsBE_dev/node_modules/router/lib/route.js:109:3)
    at handle (/certifirm/SailsBE_dev/node_modules/router/index.js:520:11)
    at Layer.handle [as handle_request] (/certifirm/SailsBE_dev/node_modules/router/lib/layer.js:93:5)
    at /certifirm/SailsBE_dev/node_modules/router/index.js:296:22
    at param (/certifirm/SailsBE_dev/node_modules/router/index.js:373:14)
    at param (/certifirm/SailsBE_dev/node_modules/router/index.js:384:14)
    at Function.process_params (/certifirm/SailsBE_dev/node_modules/router/index.js:429:3)
    at next (/certifirm/SailsBE_dev/node_modules/router/index.js:290:10)
    at next (/certifirm/SailsBE_dev/node_modules/router/lib/route.js:124:14)
    at fn (/certifirm/SailsBE_dev/api/hooks/custom/index.js:173:47)
    at routeTargetFnWrapper (/certifirm/SailsBE_dev/node_modules/sails/lib/router/bind.js:375:23)
    at /certifirm/SailsBE_dev/node_modules/sails/lib/router/bind.js:455:14
    at Layer.handle [as handle_request] (/certifirm/SailsBE_dev/node_modules/router/lib/layer.js:93:5)
    at next (/certifirm/SailsBE_dev/node_modules/router/lib/route.js:144:13)
    at Route.dispatch (/certifirm/SailsBE_dev/node_modules/router/lib/route.js:109:3)
    at handle (/certifirm/SailsBE_dev/node_modules/router/index.js:520:11)
    at Layer.handle [as handle_request] (/certifirm/SailsBE_dev/node_modules/router/lib/layer.js:93:5)

Use a prefix for the /metrics endpoint

Would be possible to introduce a config parameter to personalize the /metrics endpoint (i.e. /api/v1/metrics)?

We would like to use this, but are stuck on this.

TYIA

Only the first metric in a file gets registered

This is my Counter metrics. As you can see i have multiple in one file.

`
/* global sails */

const LoginAttemptCounter = sails.hooks.prometheus.counter.setup({
  name: 'app_login_attempt',
  help: 'Track the status of login attempts (success or failed).',
  labelNames: ['status', 'code'],
})

const InitiateOnboardingCounter = sails.hooks.prometheus.counter.setup({
  name: 'initiate_onboarding',
  help: 'Track the status of onboarding initiation.',
  labelNames: ['status', 'code', 'step'],
})

const ImageUploadCounter = sails.hooks.prometheus.counter.setup({
  name: 'image_upload',
  help: 'Track the status of image upload.',
  labelNames: ['status', 'code'],
})

const ImageDownloadCounter = sails.hooks.prometheus.counter.setup({
  name: 'image_download',
  help: 'Track the status of image download.',
  labelNames: ['status', 'code'],
})

const FaceVerificationCounter = sails.hooks.prometheus.counter.setup({
  name: 'face_verification',
  help: 'Track the status of facial verification.',
  labelNames: ['status', 'code'],
})

const CompleteOnboardingCounter = sails.hooks.prometheus.counter.setup({
  name: 'complete_onboarding',
  help: 'Track the status of the complete onboarding step.',
  labelNames: ['status', 'code'],
})


module.exports = {
  /**
   * Track login attempts metrics.
   */
  loginAttempts: {
    success: (code = 200) =>
      LoginAttemptCounter.inc({ amount: 1, labels: { status: 'success', code } }),
    failed: (code = 400) =>
      LoginAttemptCounter.inc({ amount: 1, labels: { status: 'failed', code } }),
  },

  /**
   * Track initiate-onboarding metrics.
   */
  initiateOnboarding: {
    success: (code = 200) =>
      InitiateOnboardingCounter.inc({ amount: 1, labels: { status: 'success', code } }),
    failed: (code = 400, step) =>
      InitiateOnboardingCounter.inc({ amount: 1, labels: { status: 'failed', code, step } }),
  },

  /**
   * Track image upload metrics.
   */
  imageUpload: {
    success: (code = 200) =>
      ImageUploadCounter.inc({ amount: 1, labels: { status: 'success', code } }),
    failed: (code = 400) =>
      ImageUploadCounter.inc({ amount: 1, labels: { status: 'failed', code } }),
  },

  /**
   * Track image download metrics.
   */
  imageDownload: {
    success: (code = 200) =>
      ImageDownloadCounter.inc({ amount: 1, labels: { status: 'success', code } }),
    failed: (code = 400) =>
      ImageDownloadCounter.inc({ amount: 1, labels: { status: 'failed', code } }),
  },

  /**
   * Track image face verification.
   */
  faceVerification: {
    success: (code = 200) =>
      FaceVerificationCounter.inc({ amount: 1, labels: { status: 'success', code } }),
    failed: (code = 400) =>
      FaceVerificationCounter.inc({ amount: 1, labels: { status: 'failed', code } }),
  },

  /**
   * Track image download metrics.
   */
  completeOnboarding: {
    success: (code = 200) =>
      CompleteOnboardingCounter.inc({ amount: 1, labels: { status: 'success', code } }),
    failed: (code = 400) =>
      CompleteOnboardingCounter.inc({ amount: 1, labels: { status: 'failed', code } }),
  },
};

`

However, when i run the application only the first counter in this file seems to get registered.

HTTP Metrics Failed Request Not Visible

The HTTP metrics is only showing the request where the status code is 200. Whenever a request failed, the counter for that failed request increases (http_request_duration_seconds_count works fine) but http_request_duration_seconds_sum doesn't show the failed request i.e. status code 4xx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.