├── .husky ├── .gitignore └── pre-commit ├── .gitignore ├── .eslintrc ├── .github └── workflows │ └── nodejs-ci-action.yml ├── package.json ├── index.js ├── CODE_OF_CONDUCT.md ├── README.md ├── CHANGELOG.md ├── LICENSE └── test └── prometheus-test.js /.husky/.gitignore: -------------------------------------------------------------------------------- 1 | _ 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .nyc_output 3 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | . "$(dirname "$0")/_/husky.sh" 3 | 4 | npm test 5 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "semistandard", 3 | "env": { 4 | "es6": true, 5 | "node": true 6 | }, 7 | "rules": { 8 | "standard/no-callback-literal": "off", 9 | "arrow-spacing": "error", 10 | "arrow-parens": ["error", "as-needed"], 11 | "arrow-body-style": ["error", "as-needed"], 12 | "prefer-template": "error", 13 | "max-len": ["warn", { "code": 80 }], 14 | "no-unused-vars": ["warn", { 15 | "argsIgnorePattern": "^_$|^e$|^reject$|^resolve$" 16 | }], 17 | "no-console": ["error", { 18 | "allow": ["warn", "error"] 19 | }], 20 | "valid-jsdoc": "error", 21 | "semi": ["error", "always"], 22 | "quotes": ["error", "single", { "allowTemplateLiterals": true }] 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /.github/workflows/nodejs-ci-action.yml: -------------------------------------------------------------------------------- 1 | name: Node.js CI 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | env: 10 | HUSKY: 0 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | strategy: 18 | matrix: 19 | node-version: [20.x, 22.x, 24.x] 20 | 21 | steps: 22 | - uses: actions/checkout@v2 23 | - name: Use Node.js ${{ matrix.node-version }} 24 | uses: actions/setup-node@v2 25 | with: 26 | node-version: ${{ matrix.node-version }} 27 | - run: npm ci 28 | - run: npm test 29 | - run: node_modules/nyc/bin/nyc.js report --reporter=lcovonly 30 | - name: Coveralls Parallel 31 | uses: coverallsapp/github-action@master 32 | with: 33 | github-token: ${{ secrets.github_token }} 34 | flag-name: run-${{ matrix.node-version }} 35 | parallel: true 36 | finish: 37 | needs: build 38 | runs-on: ubuntu-latest 39 | steps: 40 | - name: Coveralls Finished 41 | uses: coverallsapp/github-action@master 42 | with: 43 | github-token: ${{ secrets.github_token }} 44 | parallel-finished: true 45 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "opossum-prometheus", 3 | "version": "0.5.0", 4 | "description": "Prometheus metrics for opossum circuit breaker", 5 | "main": "index.js", 6 | "scripts": { 7 | "prepare": "husky || true", 8 | "lint": "eslint --ignore-path .gitignore .", 9 | "pretest": "npm run lint", 10 | "test": "nyc tape test/*.js | tap-spec", 11 | "prerelease": "npm run test", 12 | "release": "standard-version -a" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git://github.com/nodeshift/opossum-prometheus.git" 17 | }, 18 | "bugs": { 19 | "url": "https://github.com/nodeshift/opossum-prometheus/issues" 20 | }, 21 | "support": { 22 | "target": "LTS", 23 | "response": "REGULAR-7", 24 | "backing": "COMPANY" 25 | }, 26 | "keywords": [ 27 | "circuit breaker", 28 | "fail fast", 29 | "prometheus", 30 | "metrics" 31 | ], 32 | "author": "Red Hat, Inc.", 33 | "license": "Apache-2.0", 34 | "devDependencies": { 35 | "coveralls": "^3.1.1", 36 | "eslint": "^8.57.0", 37 | "eslint-config-semistandard": "^17.0.0", 38 | "eslint-config-standard": "^17.1.0", 39 | "eslint-plugin-import": "^2.29.1", 40 | "eslint-plugin-node": "^11.1.0", 41 | "eslint-plugin-promise": "^6.1.1", 42 | "husky": "^9.1.6", 43 | "nyc": "^17.1.0", 44 | "opossum": "^8.4.0", 45 | "standard-version": "^9.5.0", 46 | "tap-spec": "^5.0.0", 47 | "tape": "^5.9.0" 48 | }, 49 | "dependencies": { 50 | "prom-client": "^15.1.3" 51 | }, 52 | "engines": { 53 | "node": "^24 || ^22 || ^20" 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const client = require('prom-client'); 4 | 5 | // The current tests has circuit names like: 6 | // 'circuit one' (with blank space) and others like 7 | // 3beb8f49-62c0-46e0-b458-dcd4a62d0f48. 8 | // So to avoid "Error: Invalid metric name" we are changing the 9 | // circuit name to pass the tests. 10 | // More details: 11 | // https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels 12 | 13 | class PrometheusMetrics { 14 | constructor (options = {}) { 15 | this._registry = options.registry || client.register; 16 | this._metricPrefix = options.metricPrefix || ``; 17 | this._client = client; 18 | this._options = options; 19 | this._counter = new this._client.Counter({ 20 | name: `${this._metricPrefix}circuit`, 21 | help: `A count of all circuit' events`, 22 | registers: [this._registry], 23 | labelNames: ['name', 'event'] 24 | }); 25 | 26 | if (this.exposePerformanceMetrics()) { 27 | this._summary = new this._client.Summary({ 28 | name: `${this._metricPrefix}circuit_perf`, 29 | help: `A summary of all circuit's events`, 30 | registers: [this._registry], 31 | labelNames: ['name', 'event'] 32 | }); 33 | } 34 | 35 | if (!options.registry) { 36 | this._client.collectDefaultMetrics({ 37 | prefix: `${this._metricPrefix}opossum_`, 38 | register: this._registry 39 | }); 40 | } 41 | 42 | if (options.circuits) { 43 | this.add(options.circuits); 44 | } 45 | } 46 | 47 | exposePerformanceMetrics () { 48 | return this._options === undefined || 49 | this._options.exposePerformanceMetrics === undefined || 50 | this._options.exposePerformanceMetrics; 51 | } 52 | 53 | add (circuits) { 54 | if (!circuits) { 55 | return; 56 | } 57 | circuits = Array.isArray(circuits) ? circuits : [circuits]; 58 | 59 | circuits.forEach(circuit => { 60 | for (const eventName of circuit.eventNames()) { 61 | circuit.on(eventName, _ => { 62 | this._counter.labels(circuit.name, eventName).inc(); 63 | }); 64 | 65 | if (this.exposePerformanceMetrics() && 66 | (eventName === 'success' || eventName === 'failure')) { 67 | // not the timeout event because runtime == timeout 68 | circuit.on(eventName, (result, runTime) => { 69 | this._summary.labels(circuit.name, eventName).observe(runTime); 70 | }); 71 | } 72 | } 73 | }); 74 | } 75 | 76 | clear () { 77 | this._registry.clear(); 78 | } 79 | 80 | metrics () { 81 | return this._registry.metrics(); 82 | } 83 | 84 | get client () { 85 | return this._client; 86 | } 87 | } 88 | 89 | module.exports = PrometheusMetrics; 90 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 26 | * Trolling, insulting/derogatory comments, and personal or political attacks 27 | * Public or private harassment 28 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 29 | * Other conduct which could reasonably be considered inappropriate in a professional setting 30 | 31 | ## Our Responsibilities 32 | 33 | Project maintainers are responsible for clarifying the standards of acceptable 34 | behavior and are expected to take appropriate and fair corrective action in 35 | response to any instances of unacceptable behavior. 36 | 37 | Project maintainers have the right and responsibility to remove, edit, or 38 | reject comments, commits, code, wiki edits, issues, and other contributions 39 | that are not aligned to this Code of Conduct, or to ban temporarily or 40 | permanently any contributor for other behaviors that they deem inappropriate, 41 | threatening, offensive, or harmful. 42 | 43 | ## Scope 44 | 45 | This Code of Conduct applies both within project spaces and in public spaces 46 | when an individual is representing the project or its community. Examples of 47 | representing a project or community include using an official project e-mail 48 | address, posting via an official social media account, or acting as an appointed 49 | representative at an online or offline event. Representation of a project may be 50 | further defined and clarified by project maintainers. 51 | 52 | ## Enforcement 53 | 54 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 55 | reported by contacting the project team at nodeshift@redhat.com. All 56 | complaints will be reviewed and investigated and will result in a response that 57 | is deemed necessary and appropriate to the circumstances. The project team is 58 | obligated to maintain confidentiality with regard to the reporter of an incident. 59 | Further details of specific enforcement policies may be posted separately. 60 | 61 | Project maintainers who do not follow or enforce the Code of Conduct in good 62 | faith may face temporary or permanent repercussions as determined by other 63 | members of the project's leadership. 64 | 65 | ## Attribution 66 | 67 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 68 | available at 69 | 70 | homepage: 71 | 72 | For answers to common questions about this code of conduct, see 73 | 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Prometheus Metrics for Opossum Circuit Breaker 2 | 3 | ![Node.js CI](https://github.com/nodeshift/opossum-prometheus/workflows/Node.js%20CI/badge.svg) 4 | [![Coverage Status](https://coveralls.io/repos/github/nodeshift/opossum-prometheus/badge.svg?branch=main)](https://coveralls.io/github/nodeshift/opossum-prometheus?branch=main) 5 | [![Known Vulnerabilities](https://snyk.io/test/npm/opossum-prometheus/badge.svg)](https://snyk.io/test/npm/opossum-prometheus) 6 | 7 | This module provides [Prometheus](https://prometheus.io/) metrics for 8 | [opossum](https://github.com/nodeshift/opossum) circuit breakers. To use 9 | it with your circuit breakers, just pass them in to the `PrometheusMetrics` 10 | constructor. 11 | 12 | For each circuit breaker, the metrics are: 13 | 14 | * a `prometheus counter` for each event name 15 | * a `prometheus summary` for the events `success`, `failed` execution time. 16 | 17 | Example: 18 | 19 | ```js 20 | const CircuitBreaker = require('opossum'); 21 | const PrometheusMetrics = require('opossum-prometheus'); 22 | 23 | // create a couple of circuit breakers 24 | const c1 = new CircuitBreaker(someFunction); 25 | const c2 = new CircuitBreaker(someOtherFunction); 26 | 27 | // Provide them to the constructor 28 | const prometheus = new PrometheusMetrics({ circuits: [c1, c2] }); 29 | 30 | //... 31 | // Provide other circuit breaker later 32 | const c3 = new CircuitBreaker(someOtherFunction3); 33 | prometheus.add([c3]); 34 | 35 | // Write metrics to the console 36 | console.log(await prometheus.metrics()); 37 | ``` 38 | 39 | This module would typically be used in an application that can provide 40 | an endpoint for the Prometheus server to monitor. 41 | 42 | The `prometheusRegistry` constructor parameter allows you to provide an existing 43 | [prom-client](https://github.com/siimon/prom-client) registry. 44 | The metrics about the circuit will be added to the provided registry instead 45 | of the global registry. 46 | The [default metrics](https://github.com/siimon/prom-client#default-metrics) 47 | will not be added to the provided registry. 48 | 49 | ```js 50 | const CircuitBreaker = require('opossum'); 51 | const PrometheusMetrics = require('opossum-prometheus'); 52 | const { Registry } = require('prom-client'); 53 | 54 | // Create a registry 55 | const registry = new Registry(); 56 | 57 | // create a circuit 58 | const circuit = new CircuitBreaker(functionThatMightFail); 59 | const metrics = new PrometheusMetrics({ circuits: [circuit], registry: registry }) 60 | ``` 61 | 62 | ## Options 63 | The `PrometheusMetrics` constructor takes an options object as detailed below. 64 | 65 | ```js 66 | const options = {}; 67 | new PrometheusMetrics(options) 68 | ``` 69 | 70 | |Name |Description |Default | 71 | |--------------------------|------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------| 72 | |`circuits` |A list or individual circuit breaker to create metrics for |No circuits | 73 | |`registry` |An existing registry to use for prometheus metrics |`undefined` - The default prometheus registry will be used and default system metrics will be collected| 74 | |`exposePerformanceMetrics`|Measure the performance of breakers and report them through the registry|true | 75 | |`metricPrefix`|Prefix for circuit breakers metrics name|any string | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | ## [0.5.0](https://github.com/nodeshift/opossum-prometheus/compare/v0.4.0...v0.5.0) (2025-06-10) 6 | 7 | 8 | ### ⚠ BREAKING CHANGES 9 | 10 | * remove Node 18 support (#108) 11 | * Bump supported node.js version (#91) 12 | 13 | ### Features 14 | 15 | * add node 24 support ([#107](https://github.com/nodeshift/opossum-prometheus/issues/107)) ([f39e426](https://github.com/nodeshift/opossum-prometheus/commit/f39e426f5b047eb6a37d4c77f31c5e81ff79804f)) 16 | * Bump supported node.js version ([#91](https://github.com/nodeshift/opossum-prometheus/issues/91)) ([65886aa](https://github.com/nodeshift/opossum-prometheus/commit/65886aa4332a4f58143e061dfea301751c8ddd49)) 17 | * remove Node 18 support ([#108](https://github.com/nodeshift/opossum-prometheus/issues/108)) ([025d55f](https://github.com/nodeshift/opossum-prometheus/commit/025d55f6230ea495c2d581d0a2527196d2929d01)) 18 | 19 | 20 | ### Bug Fixes 21 | 22 | * upgrade opossum from 8.3.0 to 8.4.0 ([#96](https://github.com/nodeshift/opossum-prometheus/issues/96)) ([2dcebf3](https://github.com/nodeshift/opossum-prometheus/commit/2dcebf34ccd6fd01c987c27f1fbd4552295ecdbc)) 23 | * upgrade prom-client from 15.1.0 to 15.1.1 ([#80](https://github.com/nodeshift/opossum-prometheus/issues/80)) ([e29d378](https://github.com/nodeshift/opossum-prometheus/commit/e29d378a7ca5bad9e6aaf0cc0982bc74bdfc323e)) 24 | 25 | ## [0.4.0](https://github.com/nodeshift/opossum-prometheus/compare/v0.3.0...v0.4.0) (2024-04-08) 26 | 27 | 28 | ### ⚠ BREAKING CHANGES 29 | 30 | * Drop support for Node.js v14 31 | Update prom-client to v15 32 | metrics is now an async function 33 | * drop Node.js 12 34 | 35 | ### Features 36 | 37 | * support Node.js v20 ([#78](https://github.com/nodeshift/opossum-prometheus/issues/78)) ([1f3e050](https://github.com/nodeshift/opossum-prometheus/commit/1f3e0503c616a57b77eef8e8e31634e2ae79f598)) 38 | * upgrade opossum from 5.1.3 to 6.2.1 ([#62](https://github.com/nodeshift/opossum-prometheus/issues/62)) ([88e13d5](https://github.com/nodeshift/opossum-prometheus/commit/88e13d5a1e3f4a9cb87375e97c1be8f5742de522)) 39 | * using semistandard and greenkeeper badge removed ([bdfe980](https://github.com/nodeshift/opossum-prometheus/commit/bdfe9809dd46eac7d8cc4161ef2c21f4b5a1029f)) 40 | 41 | 42 | ### Bug Fixes 43 | 44 | * package.json & package-lock.json to reduce vulnerabilities ([#56](https://github.com/nodeshift/opossum-prometheus/issues/56)) ([5c57b1e](https://github.com/nodeshift/opossum-prometheus/commit/5c57b1e22a1147355c9cf983dcfc06ac4fd7cf7a)) 45 | * upgrade coveralls from 3.1.0 to 3.1.1 ([#51](https://github.com/nodeshift/opossum-prometheus/issues/51)) ([1bcbaae](https://github.com/nodeshift/opossum-prometheus/commit/1bcbaae923eea55c2e4b6159f2612c9890c54669)) 46 | * upgrade opossum from 5.0.0 to 5.0.1 ([#33](https://github.com/nodeshift/opossum-prometheus/issues/33)) ([e9e700a](https://github.com/nodeshift/opossum-prometheus/commit/e9e700ad77c7919c7de51234c3ce43ad5e26ee9f)) 47 | * upgrade opossum from 5.0.1 to 5.0.2 ([#36](https://github.com/nodeshift/opossum-prometheus/issues/36)) ([c45d4b1](https://github.com/nodeshift/opossum-prometheus/commit/c45d4b14970734474c78b31a6470a3073a79b3e7)) 48 | * upgrade opossum from 5.0.2 to 5.1.0 ([#38](https://github.com/nodeshift/opossum-prometheus/issues/38)) ([d3f509d](https://github.com/nodeshift/opossum-prometheus/commit/d3f509d313268548c6c5c362548c322d5abb673e)) 49 | * upgrade opossum from 5.1.0 to 5.1.1 ([#39](https://github.com/nodeshift/opossum-prometheus/issues/39)) ([ae9f69b](https://github.com/nodeshift/opossum-prometheus/commit/ae9f69b07f35ff07230ef15d7efa14fdbd272301)) 50 | * upgrade opossum from 5.1.1 to 5.1.2 ([#43](https://github.com/nodeshift/opossum-prometheus/issues/43)) ([f0c00fe](https://github.com/nodeshift/opossum-prometheus/commit/f0c00fe04518b100fc18f0be32fe7e6fd4cb530e)) 51 | * upgrade opossum from 5.1.2 to 5.1.3 ([#44](https://github.com/nodeshift/opossum-prometheus/issues/44)) ([78c826b](https://github.com/nodeshift/opossum-prometheus/commit/78c826b0b941f6a8a8dab142b2c27f3264b6f855)) 52 | * upgrade standard-version from 9.0.0 to 9.1.0 ([#40](https://github.com/nodeshift/opossum-prometheus/issues/40)) ([6c694ed](https://github.com/nodeshift/opossum-prometheus/commit/6c694eda67f0e7743c4dcf5001daa1f5955e6106)) 53 | * upgrade standard-version from 9.1.0 to 9.3.0 ([#49](https://github.com/nodeshift/opossum-prometheus/issues/49)) ([d29b608](https://github.com/nodeshift/opossum-prometheus/commit/d29b60878b76133bcda3be3d57328993b5c94441)) 54 | * upgrade tape from 5.0.1 to 5.1.0 ([#41](https://github.com/nodeshift/opossum-prometheus/issues/41)) ([c51f9d3](https://github.com/nodeshift/opossum-prometheus/commit/c51f9d3e8263b4f47cc7de0a3cd5009152c7ccd4)) 55 | * upgrade tape from 5.1.0 to 5.1.1 ([#42](https://github.com/nodeshift/opossum-prometheus/issues/42)) ([d45e86a](https://github.com/nodeshift/opossum-prometheus/commit/d45e86aa0b83d9b333f73f8f4407e43b16cd9452)) 56 | * upgrade tape from 5.1.1 to 5.2.2 ([#50](https://github.com/nodeshift/opossum-prometheus/issues/50)) ([ebeeed4](https://github.com/nodeshift/opossum-prometheus/commit/ebeeed431aa4589054d496616898a5241d39ce74)) 57 | 58 | 59 | * drop Node.js 12 ([b14eca3](https://github.com/nodeshift/opossum-prometheus/commit/b14eca32fc996d46f97b3055c295fb77e436b8fb)) 60 | 61 | ## [0.3.0](https://github.com/lholmquist/opossum-prometheus/compare/v0.2.0...v0.3.0) (2020-09-02) 62 | 63 | 64 | ### Features 65 | 66 | * Providing metric prefix for circuit metrics ([#32](https://github.com/lholmquist/opossum-prometheus/issues/32)) ([0d85a86](https://github.com/lholmquist/opossum-prometheus/commit/0d85a86a9f594c58f3f900cb98000e58a9eeb058)) 67 | 68 | 69 | ### Bug Fixes 70 | 71 | * upgrade standard-version from 8.0.1 to 8.0.2 ([#30](https://github.com/lholmquist/opossum-prometheus/issues/30)) ([e8edaa1](https://github.com/lholmquist/opossum-prometheus/commit/e8edaa11c2bd35908810477872519d6694fc4857)) 72 | 73 | ## [0.2.0](https://github.com/lholmquist/opossum-prometheus/compare/v0.1.0...v0.2.0) (2020-04-23) 74 | 75 | 76 | ### ⚠ BREAKING CHANGES 77 | 78 | * Options object is now used to configure custom registry and initial circuits 79 | 80 | * docs: Updates and adds documentation for options object configuration 81 | 82 | ### Features 83 | 84 | * adds option for performance metrics so they can be disabled, default is enabled ([#20](https://github.com/lholmquist/opossum-prometheus/issues/20)) ([2437eca](https://github.com/lholmquist/opossum-prometheus/commit/2437eca65e7e5d55d3685f213c24e589827d2899)) 85 | * Use options object for all configuration ([#19](https://github.com/lholmquist/opossum-prometheus/issues/19)) ([b353a59](https://github.com/lholmquist/opossum-prometheus/commit/b353a5907212a5eabae420ff4ef06c105f953d3f)) 86 | 87 | 88 | ### Bug Fixes 89 | 90 | * upgrade standard-version from 7.0.0 to 7.1.0 ([#17](https://github.com/lholmquist/opossum-prometheus/issues/17)) ([2b68517](https://github.com/lholmquist/opossum-prometheus/commit/2b68517ae6902837ff9d94cbcbab11621fba920d)) 91 | * upgrade tape from 4.11.0 to 4.13.2 ([#16](https://github.com/lholmquist/opossum-prometheus/issues/16)) ([6233d53](https://github.com/lholmquist/opossum-prometheus/commit/6233d53041727d8b44126b061a18cc411642bb34)) 92 | 93 | ## [0.1.0](https://github.com/lholmquist/opossum-prometheus/compare/v0.0.4...v0.1.0) (2020-03-13) 94 | 95 | 96 | ### ⚠ BREAKING CHANGES 97 | 98 | * Changes event metrics to use labels rather than seporate metrics 99 | * Changes metrics to use labels for circuit name rather than seporate metrics 100 | 101 | Co-authored-by: martlandh 102 | 103 | ### Features 104 | 105 | * Change event name to be a label ([#15](https://github.com/lholmquist/opossum-prometheus/issues/15)) ([075b033](https://github.com/lholmquist/opossum-prometheus/commit/075b033)) 106 | 107 | ### [0.0.4](https://github.com/lholmquist/opossum-prometheus/compare/v0.0.3...v0.0.4) (2019-10-02) 108 | 109 | 110 | ### Features 111 | 112 | * add stats (percentiles stats, reponse times) in Prometheus metrics ([#10](https://github.com/lholmquist/opossum-prometheus/issues/10)) ([712fa2d](https://github.com/lholmquist/opossum-prometheus/commit/712fa2d)) 113 | 114 | ### [0.0.3](https://github.com/lholmquist/opossum-prometheus/compare/v0.0.2...v0.0.3) (2019-09-18) 115 | 116 | 117 | ### Features 118 | 119 | * allow one circuit to be pass in without an array. ([#5](https://github.com/lholmquist/opossum-prometheus/issues/5)) ([02ca8f5](https://github.com/lholmquist/opossum-prometheus/commit/02ca8f5)), closes [#4](https://github.com/lholmquist/opossum-prometheus/issues/4) 120 | * allow to add circuits dynamicaly ([#7](https://github.com/lholmquist/opossum-prometheus/issues/7)) ([afbaef2](https://github.com/lholmquist/opossum-prometheus/commit/afbaef2)) 121 | 122 | ### 0.0.2 (2019-08-13) 123 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /test/prometheus-test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const test = require('tape'); 4 | const CircuitBreaker = require('opossum'); 5 | const PrometheusMetrics = require('../'); 6 | const client = require('prom-client'); 7 | const { Registry } = client; 8 | 9 | /** 10 | * Returns a promise that resolves if the parameter 11 | * 'x' evaluates to >= 0. Otherwise the returned promise fails. 12 | */ 13 | 14 | /* eslint prefer-promise-reject-errors: "off" */ 15 | function passFail (x) { 16 | return new Promise((resolve, reject) => { 17 | setTimeout(() => { 18 | (x > 0) ? resolve(x) : reject(`Error: ${x} is < 0`); 19 | }, 100); 20 | }); 21 | } 22 | 23 | test('The factory function accept no parameter', t => { 24 | t.plan(1); 25 | 26 | const prometheus = new PrometheusMetrics(); 27 | t.teardown(() => prometheus.clear()); 28 | t.ok(prometheus); 29 | 30 | t.end(); 31 | }); 32 | 33 | test('The factory function takes an object instead of just an Array', 34 | async t => { 35 | t.plan(3); 36 | const c1 = new CircuitBreaker(passFail, { name: 'fred' }); 37 | const prometheus = new PrometheusMetrics({ circuits: c1 }); 38 | t.teardown(() => prometheus.clear()); 39 | await c1.fire(1); 40 | const metrics = await prometheus.metrics(); 41 | t.equal(c1.name, 'fred'); 42 | t.ok(/circuit.*fred/.test(metrics)); 43 | t.ok(/circuit_perf.*fred/.test(metrics)); 44 | t.end(); 45 | }); 46 | 47 | test('The factory function provides access to metrics for all circuits', 48 | async t => { 49 | t.plan(6); 50 | const c1 = new CircuitBreaker(passFail, { name: 'fred' }); 51 | const c2 = new CircuitBreaker(passFail, { name: 'bob' }); 52 | const prometheus = new PrometheusMetrics({ circuits: [c1, c2] }); 53 | t.teardown(() => prometheus.clear()); 54 | await c1.fire(1); 55 | await c2.fire(1); 56 | const metrics = await prometheus.metrics(); 57 | t.equal(c1.name, 'fred'); 58 | t.equal(c2.name, 'bob'); 59 | t.ok(/circuit.*fred/.test(metrics)); 60 | t.ok(/circuit_perf.*fred/.test(metrics)); 61 | t.ok(/circuit.*bob/.test(metrics)); 62 | t.ok(/circuit_perf.*bob/.test(metrics)); 63 | t.end(); 64 | }); 65 | 66 | test('The factory function uses a custom prom-client registry', async t => { 67 | t.plan(10); 68 | const registry = new Registry(); 69 | const c1 = new CircuitBreaker(passFail, { 70 | name: 'fred' 71 | }); 72 | const c2 = new CircuitBreaker(passFail, { 73 | name: 'bob' 74 | }); 75 | const prometheus = new PrometheusMetrics({ 76 | circuits: [c1, c2], 77 | registry 78 | }); 79 | t.teardown(() => prometheus.clear()); 80 | await c1.fire(1); 81 | await c2.fire(1); 82 | const rMetrics = await registry.metrics(); 83 | const pMetrics = await prometheus.metrics(); 84 | t.equal(c1.name, 'fred'); 85 | t.equal(c2.name, 'bob'); 86 | t.ok(/circuit.*fred/.test(rMetrics)); 87 | t.ok(/circuit_perf.*fred/.test(rMetrics)); 88 | t.ok(/circuit.*bob/.test(rMetrics)); 89 | t.ok(/circuit_perf.*bob/.test(rMetrics)); 90 | t.ok(/circuit.*bob/.test(pMetrics)); 91 | t.ok(/circuit_perf.*fred/.test(pMetrics)); 92 | t.ok(/circuit.*bob/.test(pMetrics)); 93 | t.ok(/circuit_perf.*bob/.test(pMetrics)); 94 | t.end(); 95 | }); 96 | 97 | test('The add function takes an object instead of just an Array', async t => { 98 | t.plan(3); 99 | const c1 = new CircuitBreaker(passFail, { name: 'fred' }); 100 | const prometheus = new PrometheusMetrics(); 101 | t.teardown(() => prometheus.clear()); 102 | prometheus.add(c1); 103 | await c1.fire(1); 104 | const metrics = await prometheus.metrics(); 105 | t.equal(c1.name, 'fred'); 106 | t.ok(/circuit.*fred.*/.test(metrics)); 107 | t.ok(/circuit_perf.*fred.*/.test(metrics)); 108 | t.end(); 109 | }); 110 | 111 | test('The add function provides access to metrics for all circuits', 112 | async t => { 113 | t.plan(9); 114 | const c1 = new CircuitBreaker(passFail, { name: 'fred' }); 115 | const c2 = new CircuitBreaker(passFail, { name: 'bob' }); 116 | const c3 = new CircuitBreaker(passFail, { name: 'foo' }); 117 | const prometheus = new PrometheusMetrics({ circuits: [c1] }); 118 | t.teardown(() => prometheus.clear()); 119 | prometheus.add([c2, c3]); 120 | await c1.fire(1); 121 | await c2.fire(1); 122 | await c3.fire(1); 123 | const metrics = await prometheus.metrics(); 124 | t.equal(c1.name, 'fred'); 125 | t.equal(c2.name, 'bob'); 126 | t.equal(c3.name, 'foo'); 127 | t.ok(/circuit.*fred/.test(metrics)); 128 | t.ok(/circuit_perf.*fred/.test(metrics)); 129 | t.ok(/circuit.*bob/.test(metrics)); 130 | t.ok(/circuit_perf.*bob/.test(metrics)); 131 | t.ok(/circuit.*foo/.test(metrics)); 132 | t.ok(/circuit_perf.*foo/.test(metrics)); 133 | t.end(); 134 | }); 135 | 136 | test('The add function called without parameter do nothing', async t => { 137 | t.plan(1); 138 | const prometheus = new PrometheusMetrics(); 139 | t.teardown(() => prometheus.clear()); 140 | prometheus.add(); 141 | t.ok(/circuit/.test(await prometheus.metrics())); 142 | t.end(); 143 | }); 144 | 145 | test('Circuit fire/success/failure are counted', async t => { 146 | const circuit = new CircuitBreaker(passFail); 147 | const fire = /circuit\{name="passFail",event="fire"\} 2/; 148 | const success = /circuit\{name="passFail",event="success"\} 1/; 149 | const failure = /circuit\{name="passFail",event="failure"\} 1/; 150 | const prometheus = new PrometheusMetrics({ circuits: [circuit] }); 151 | t.teardown(() => prometheus.clear()); 152 | t.plan(3); 153 | try { 154 | await circuit.fire(1); 155 | await circuit.fire(-1); 156 | } catch (e) { 157 | const metrics = await prometheus.metrics(); 158 | t.ok(fire.test(metrics), fire); 159 | t.ok(success.test(metrics), success); 160 | t.ok(failure.test(metrics), failure); 161 | t.end(); 162 | } 163 | }); 164 | 165 | test('Metrics are enabled for all circuit events', async t => { 166 | const circuit = new CircuitBreaker(passFail); 167 | circuit.on = (event, callback) => { 168 | callback(null, 1); 169 | }; 170 | const prometheus = new PrometheusMetrics({ circuits: [circuit] }); 171 | t.teardown(() => prometheus.clear()); 172 | const metrics = await prometheus.metrics(); 173 | t.plan(circuit.eventNames().length); 174 | for (const name of circuit.eventNames()) { 175 | const match = new RegExp(`circuit{name="passFail",event="${name}"}`); 176 | t.ok(match.test(metrics), name); 177 | } 178 | t.end(); 179 | }); 180 | 181 | test('Default prometheus metrics are enabled', async t => { 182 | const circuit = new CircuitBreaker(passFail); 183 | const prometheus = new PrometheusMetrics({ circuits: [circuit] }); 184 | t.teardown(() => prometheus.clear()); 185 | const metrics = await prometheus.metrics(); 186 | const names = [ 187 | 'process_cpu_seconds_total', 188 | 'process_resident_memory_bytes', 189 | 'process_start_time_seconds' 190 | ]; 191 | if (process.platform === 'linux') { 192 | names.concat([ 193 | 'process_virtual_memory_bytes', 194 | 'process_heap_bytes', 195 | 'process_open_fds', 196 | 'process_max_fds' 197 | ]); 198 | } 199 | t.plan(names.length); 200 | for (const name of names) { 201 | const match = new RegExp(`opossum_${name}`); 202 | t.ok(match.test(metrics), name); 203 | } 204 | t.end(); 205 | }); 206 | 207 | test('Should not add default metrics to custom registry', async t => { 208 | const registry = new Registry(); 209 | const circuit = new CircuitBreaker(passFail); 210 | const prometheus = new PrometheusMetrics({ 211 | circuits: [circuit], 212 | registry 213 | }); 214 | t.teardown(() => prometheus.clear()); 215 | const metrics = await prometheus.metrics(); 216 | const names = [ 217 | 'process_cpu_seconds_total', 218 | 'process_open_fds', 219 | 'process_max_fds', 220 | 'process_virtual_memory_bytes', 221 | 'process_resident_memory_bytes', 222 | 'process_heap_bytes', 223 | 'process_start_time_seconds' 224 | ]; 225 | t.plan(names.length); 226 | for (const name of names) { 227 | const match = new RegExp(`opossum_${name}`); 228 | t.notOk(match.test(metrics), name); 229 | } 230 | t.end(); 231 | }); 232 | 233 | test('Default prometheus metrics are enabled without circuit', async t => { 234 | const registry = new Registry(); 235 | const prometheus = new PrometheusMetrics({ registry }); 236 | t.teardown(() => prometheus.clear()); 237 | const metrics = await prometheus.metrics(); 238 | const names = [ 239 | 'nodejs_eventloop_lag', 240 | 'nodejs_active_handles', 241 | 'nodejs_active_requests', 242 | 'nodejs_heap_size_total_bytes', 243 | 'nodejs_heap_size_used_bytes', 244 | 'nodejs_external_memory_bytes', 245 | 'nodejs_heap_space_size_total_bytes', 246 | 'nodejs_heap_space_size_used_bytes', 247 | 'nodejs_heap_space_size_available_bytes', 248 | 'nodejs_version_info', 249 | 'process_cpu_seconds_total', 250 | 'process_open_fds', 251 | 'process_max_fds', 252 | 'process_virtual_memory_bytes', 253 | 'process_resident_memory_bytes', 254 | 'process_heap_bytes', 255 | 'process_start_time_seconds' 256 | ]; 257 | t.plan(names.length); 258 | for (const name of names) { 259 | const match = new RegExp(`opossum_${name}`); 260 | t.notOk(match.test(metrics), name); 261 | } 262 | t.end(); 263 | }); 264 | 265 | test('Node.js specific metrics are enabled', async t => { 266 | const circuit = new CircuitBreaker(passFail); 267 | const prometheus = new PrometheusMetrics({ circuits: [circuit] }); 268 | t.teardown(() => prometheus.clear()); 269 | const metrics = await prometheus.metrics(); 270 | const names = [ 271 | 'nodejs_eventloop_lag', 272 | 'nodejs_active_handles', 273 | 'nodejs_active_requests', 274 | 'nodejs_heap_size_total_bytes', 275 | 'nodejs_heap_size_used_bytes', 276 | 'nodejs_external_memory_bytes', 277 | 'nodejs_heap_space_size_total_bytes', 278 | 'nodejs_heap_space_size_used_bytes', 279 | 'nodejs_heap_space_size_available_bytes', 280 | 'nodejs_version_info' 281 | ]; 282 | t.plan(names.length); 283 | for (const name of names) { 284 | const match = new RegExp(`opossum_${name}`); 285 | t.ok(match.test(metrics), name); 286 | } 287 | t.end(); 288 | }); 289 | 290 | test('Performance metrics are not created when disabled', 291 | async t => { 292 | t.plan(3); 293 | const c1 = new CircuitBreaker(passFail, { name: 'fred' }); 294 | const prometheus = new PrometheusMetrics({ 295 | circuits: [c1], 296 | exposePerformanceMetrics: false 297 | }); 298 | t.teardown(() => prometheus.clear()); 299 | await c1.fire(1); 300 | const metrics = await prometheus.metrics(); 301 | t.equal(c1.name, 'fred'); 302 | t.ok(/circuit.*fred/.test(metrics)); 303 | t.notOk(/circuit_perf.*fred/.test(metrics)); 304 | t.end(); 305 | }); 306 | 307 | test('Performance metrics are created when not configured in options', 308 | async t => { 309 | t.plan(3); 310 | const c1 = new CircuitBreaker(passFail, { name: 'fred' }); 311 | const prometheus = new PrometheusMetrics({ circuits: [c1] }); 312 | t.teardown(() => prometheus.clear()); 313 | await c1.fire(1); 314 | const metrics = await prometheus.metrics(); 315 | t.equal(c1.name, 'fred'); 316 | t.ok(/circuit.*fred/.test(metrics)); 317 | t.ok(/circuit_perf.*fred/.test(metrics)); 318 | t.end(); 319 | }); 320 | 321 | test('Performance metrics are created when enabled in options', 322 | async t => { 323 | t.plan(3); 324 | const c1 = new CircuitBreaker(passFail, { name: 'fred' }); 325 | const prometheus = new PrometheusMetrics({ 326 | circuits: [c1], 327 | exposePerformanceMetrics: true 328 | }); 329 | t.teardown(() => prometheus.clear()); 330 | await c1.fire(1); 331 | const metrics = await prometheus.metrics(); 332 | t.equal(c1.name, 'fred'); 333 | t.ok(/circuit.*fred/.test(metrics)); 334 | t.ok(/circuit_perf.*fred/.test(metrics)); 335 | t.end(); 336 | }); 337 | 338 | test('The factory function provides metric prefix and it append to metric name', 339 | async t => { 340 | t.plan(6); 341 | const c1 = new CircuitBreaker(passFail, { name: 'fred' }); 342 | const c2 = new CircuitBreaker(passFail, { name: 'bob' }); 343 | const prometheus = new PrometheusMetrics({ 344 | circuits: [c1, c2], 345 | metricPrefix: 'some_prefix_' 346 | }); 347 | t.teardown(() => prometheus.clear()); 348 | await c1.fire(1); 349 | await c2.fire(1); 350 | const metrics = await prometheus.metrics(); 351 | t.equal(c1.name, 'fred'); 352 | t.equal(c2.name, 'bob'); 353 | t.ok(/some_prefix_circuit.*fred/.test(metrics)); 354 | t.ok(/some_prefix_circuit_perf.*fred/.test(metrics)); 355 | t.ok(/some_prefix_circuit.*bob/.test(metrics)); 356 | t.ok(/some_prefix_circuit_perf.*bob/.test(metrics)); 357 | t.end(); 358 | }); 359 | --------------------------------------------------------------------------------