Skip to content
Snippets Groups Projects
Select Git revision
  • c41a203bfc5e31db4ce439f63c991e0892e2d863
  • main default protected
  • 3-rewrite-in-typescript
  • 1.13.6
  • 1.13.5
  • 1.13.4
  • 1.13.3
  • 1.13.2
  • 1.13.1
  • 1.13.0
  • 1.12.4
  • 1.12.3
  • 1.12.2
  • 1.12.1
  • 1.12.0
  • 1.11.0
  • 1.10.1
  • 1.10.0
  • 1.9.0
  • v1.7.0
  • 1.8.0
  • v1.7.1
  • v1.6.2
23 results

index.js

Blame
  • index.ts 3.65 KiB
    import * as express from 'express'
    
    export interface Options {
      ignore: string[]
      disableRouteCounter: boolean
      disableErrorCounter: boolean
      disableDurationCounter: boolean
      disableDefaultMetrics: boolean
    }
    
    export class Metrics {
      public readonly _ignore: string[]
      public readonly _disableRouteCounter: boolean
      public readonly _disableErrorCounter: boolean
      public readonly _disableDurationCounter: boolean
      public readonly _disableDefaultMetrics: boolean
    
      public readonly _client: any
    
      public readonly _httpRequestDurationMicroseconds: any
      public readonly _numOfRequests: any
      public readonly _numOfErrors: any
    
      constructor (options: Partial<Options> = {}) {
        this._client = require('prom-client')
    
        if (typeof options.ignore !== 'undefined') {
          this._ignore = options.ignore
          this._ignore.push('/_metrics')
          this._ignore.push('/favicon.ico')
        } else {
          this._ignore = ['/_metrics', '/favicon.ico']
        }
        if (typeof options.disableRouteCounter !== 'undefined') {
          this._disableRouteCounter = options.disableRouteCounter
        } else {
          this._disableRouteCounter = false
        }
        if (typeof options.disableErrorCounter !== 'undefined') {
          this._disableErrorCounter = options.disableErrorCounter
        } else {
          this._disableErrorCounter = false
        }
        if (typeof options.disableDurationCounter !== 'undefined') {
          this._disableDurationCounter = options.disableDurationCounter
        } else {
          this._disableDurationCounter = false
        }
        if (typeof options.disableDefaultMetrics !== 'undefined') {
          this._disableDefaultMetrics = options.disableDefaultMetrics
        } else {
          this._disableDefaultMetrics = false
        }
        if (!this._disableDefaultMetrics) {
          this._client._collectDefaultMetrics = this._client.collectDefaultMetrics
        }
        if (!this._disableErrorCounter) {
          this._numOfErrors = new this._client.Counter({
            name: 'numOfErrors',
            help: 'Number of errors',
            labelNames: ['error']
          })
        }
        if (!this._disableRouteCounter) {
          this._numOfRequests = new this._client.Counter({
            name: 'numOfRequests',
            help: 'Number of requests made to a route',
            labelNames: ['route']
          })
        }