Skip to content
Snippets Groups Projects
Select Git revision
  • db6b48722e562927780beb39275bb8fe802fc97c
  • main default protected
  • next
  • chore/update-static-data
  • renovate/main-redis-5.x
  • feat/gnupg
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • refactor/pin-new-value
  • feat/36219--git-x509-signing
  • feat/structured-logger
  • hotfix/39.264.1
  • feat/skip-dangling
  • gh-readonly-queue/next/pr-36034-7a061c4ca1024a19e2c295d773d9642625d1c2be
  • hotfix/39.238.3
  • refactor/gitlab-auto-approve
  • feat/template-strings
  • gh-readonly-queue/next/pr-35654-137d934242c784e0c45d4b957362214f0eade1d7
  • fix/32307-global-extends-merging
  • fix/32307-global-extends-repositories
  • gh-readonly-queue/next/pr-35009-046ebf7cb84ab859f7fefceb5fa53a54ce9736f8
  • 41.55.3
  • 41.55.2
  • 41.55.1
  • 41.55.0
  • 41.54.0
  • 41.53.1
  • 41.53.0
  • 41.52.3
  • 41.52.2
  • 41.52.1
  • 41.52.0
  • 41.51.2
  • 41.51.1
  • 41.51.0
  • 41.50.0
  • 41.49.1
  • 41.49.0
  • 41.48.1
  • 41.48.0
  • 41.47.1
41 results

index.ts

Blame
  • core_server_prometheus-metrics.js.html 8.68 KiB
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <title>JSDoc: Source: core/server/prometheus-metrics.js</title>
    
        <script src="scripts/prettify/prettify.js"> </script>
        <script src="scripts/prettify/lang-css.js"> </script>
        <!--[if lt IE 9]>
          <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
        <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
    </head>
    
    <body>
    
    <div id="main">
    
        <h1 class="page-title">Source: core/server/prometheus-metrics.js</h1>
    
        
    
    
    
        
        <section>
            <article>
                <pre class="prettyprint source linenums"><code>import decamelize from 'decamelize'
    import prometheus from 'prom-client'
    
    export default class PrometheusMetrics {
      constructor({ register } = {}) {
        this.register = register || new prometheus.Registry()
        this.counters = {
          numRequests: new prometheus.Counter({
            name: 'service_requests_total',
            help: 'Total service requests',
            labelNames: ['category', 'family', 'service'],
            registers: [this.register],
          }),
          responseTime: new prometheus.Histogram({
            name: 'service_response_millis',
            help: 'Service response time in milliseconds',
            // 250 ms increments up to 2 seconds, then 500 ms increments up to 8
            // seconds, then 1 second increments up to 15 seconds.
            buckets: [
              250, 500, 750, 1000, 1250, 1500, 1750, 2000, 2250, 2500, 2750, 3000,
              3250, 3500, 3750, 4000, 4500, 5000, 5500, 6000, 6500, 7000, 7500,
              8000, 9000, 10000, 11000, 12000, 13000, 14000, 15000,
            ],
            registers: [this.register],
          }),
          serviceResponseSize: new prometheus.Histogram({
            name: 'service_response_bytes',
            help: 'Service response size in bytes',
            labelNames: ['category', 'family', 'service'],
            // buckets: 64KiB, 128KiB, 256KiB, 512KiB, 1MiB, 2MiB, 4MiB, 8MiB
            buckets: prometheus.exponentialBuckets(64 * 1024, 2, 8),
            registers: [this.register],
          }),
        }
        this.interval = prometheus.collectDefaultMetrics({
          register: this.register,
        })
      }
    
      async registerMetricsEndpoint(server) {
        const { register } = this
    
        server.route(/^\/metrics$/, async (data, match, end, ask) => {
          ask.res.setHeader('Content-Type', register.contentType)
          ask.res.end(await register.metrics())
        })
      }
    
      stop() {
        this.register.clear()
        if (this.interval) {
          clearInterval(this.interval)
          this.interval = undefined
        }
      }
    
      async metrics() {
        return await this.register.getMetricsAsJSON()
      }
    
      /**
       * @param {object} attrs Refer to individual attrs
       * @param {string} attrs.category e.g: 'build'
       * @param {string} attrs.serviceFamily e.g: 'npm'
       * @param {string} attrs.name e.g: 'NpmVersion'
       * @returns {object} `{ inc() {} }`.
       */
      createNumRequestCounter({ category, serviceFamily, name }) {
        const service = decamelize(name)
        return this.counters.numRequests.labels(category, serviceFamily, service)
      }
    
      noteResponseTime(responseTime) {
        return this.counters.responseTime.observe(responseTime)
      }
    
      createServiceResponseSizeHistogram({ category, serviceFamily, name }) {
        const service = decamelize(name)
        return this.counters.serviceResponseSize.labels(
          category,
          serviceFamily,
          service
        )
      }
    }
    </code></pre>
            </article>
        </section>
    
    
    
    
    </div>
    
    <nav>
        <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-badge-maker.html">badge-maker</a></li><li><a href="module-badge-maker_lib_xml.html">badge-maker/lib/xml</a></li><li><a href="module-core_base-service_base.html">core/base-service/base</a></li><li><a href="module-core_base-service_base-graphql.html">core/base-service/base-graphql</a></li><li><a href="module-core_base-service_base-json.html">core/base-service/base-json</a></li><li><a href="module-core_base-service_base-svg-scraping.html">core/base-service/base-svg-scraping</a></li><li><a href="module-core_base-service_base-xml.html">core/base-service/base-xml</a></li><li><a href="module-core_base-service_base-yaml.html">core/base-service/base-yaml</a></li><li><a href="module-core_base-service_errors.html">core/base-service/errors</a></li><li><a href="module-core_base-service_graphql.html">core/base-service/graphql</a></li><li><a href="module-core_server_server.html">core/server/server</a></li><li><a href="module-core_service-test-runner_create-service-tester.html">core/service-test-runner/create-service-tester</a></li><li><a href="module-core_service-test-runner_icedfrisby-shields.html">core/service-test-runner/icedfrisby-shields</a></li><li><a href="module-core_service-test-runner_infer-pull-request.html">core/service-test-runner/infer-pull-request</a></li><li><a href="module-core_service-test-runner_runner.html">core/service-test-runner/runner</a></li><li><a href="module-core_service-test-runner_service-tester.html">core/service-test-runner/service-tester</a></li><li><a href="module-core_service-test-runner_services-for-title.html">core/service-test-runner/services-for-title</a></li><li><a href="module-core_token-pooling_token-pool.html">core/token-pooling/token-pool</a></li><li><a href="module-services_dynamic_json-path.html">services/dynamic/json-path</a></li><li><a href="module-services_steam_steam-base.html">services/steam/steam-base</a></li></ul><h3>Classes</h3><ul><li><a href="module.exports.html">exports</a></li><li><a href="module-badge-maker_lib_xml-XmlElement.html">XmlElement</a></li><li><a href="module-core_base-service_base-graphql-BaseGraphqlService.html">BaseGraphqlService</a></li><li><a href="module-core_base-service_base-json-BaseJsonService.html">BaseJsonService</a></li><li><a href="module-core_base-service_base-svg-scraping-BaseSvgScrapingService.html">BaseSvgScrapingService</a></li><li><a href="module-core_base-service_base-xml-BaseXmlService.html">BaseXmlService</a></li><li><a href="module-core_base-service_base-yaml-BaseYamlService.html">BaseYamlService</a></li><li><a href="module-core_base-service_base-BaseService.html">BaseService</a></li><li><a href="module-core_base-service_errors-Deprecated.html">Deprecated</a></li><li><a href="module-core_base-service_errors-ImproperlyConfigured.html">ImproperlyConfigured</a></li><li><a href="module-core_base-service_errors-Inaccessible.html">Inaccessible</a></li><li><a href="module-core_base-service_errors-InvalidParameter.html">InvalidParameter</a></li><li><a href="module-core_base-service_errors-InvalidResponse.html">InvalidResponse</a></li><li><a href="module-core_base-service_errors-NotFound.html">NotFound</a></li><li><a href="module-core_base-service_errors-ShieldsRuntimeError.html">ShieldsRuntimeError</a></li><li><a href="module-core_server_server-Server.html">Server</a></li><li><a href="module-core_service-test-runner_runner-Runner.html">Runner</a></li><li><a href="module-core_service-test-runner_service-tester-ServiceTester.html">ServiceTester</a></li><li><a href="module-core_token-pooling_token-pool-Token.html">Token</a></li><li><a href="module-core_token-pooling_token-pool-TokenPool.html">TokenPool</a></li><li><a href="module-services_steam_steam-base-BaseSteamAPI.html">BaseSteamAPI</a></li></ul><h3>Tutorials</h3><ul><li><a href="tutorial-TUTORIAL.html">TUTORIAL</a></li><li><a href="tutorial-badge-urls.html">badge-urls</a></li><li><a href="tutorial-code-walkthrough.html">code-walkthrough</a></li><li><a href="tutorial-deprecating-badges.html">deprecating-badges</a></li><li><a href="tutorial-input-validation.html">input-validation</a></li><li><a href="tutorial-json-format.html">json-format</a></li><li><a href="tutorial-logos.html">logos</a></li><li><a href="tutorial-performance-testing.html">performance-testing</a></li><li><a href="tutorial-production-hosting.html">production-hosting</a></li><li><a href="tutorial-releases.html">releases</a></li><li><a href="tutorial-self-hosting.html">self-hosting</a></li><li><a href="tutorial-server-secrets.html">server-secrets</a></li><li><a href="tutorial-service-tests.html">service-tests</a></li></ul><h3>Global</h3><ul><li><a href="global.html#createNumRequestCounter">createNumRequestCounter</a></li><li><a href="global.html#getColor">getColor</a></li><li><a href="global.html#isMetricWithPattern">isMetricWithPattern</a></li><li><a href="global.html#render">render</a></li><li><a href="global.html#validateAffiliations">validateAffiliations</a></li></ul>
    </nav>
    
    <br class="clear">
    
    <footer>
        Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Sat Jul 24 2021 11:45:38 GMT+0000 (Coordinated Universal Time)
    </footer>
    
    <script> prettyPrint(); </script>
    <script src="scripts/linenumber.js"> </script>
    </body>
    </html>