inspector-prometheus
Version:
Prometheus / Pushgateway metric reporter for nodejs
89 lines • 9.17 kB
JSON
{
"name": "inspector-prometheus",
"description": "Prometheus / Pushgateway metric reporter for nodejs",
"homepage": "https://rstiller.github.io/inspector-metrics/",
"version": "2.8.0",
"main": "./build/lib/metrics/index.js",
"typings": "./build/lib/metrics/index.d.ts",
"license": "MIT",
"licenses": [
{
"type": "MIT",
"url": "https://www.opensource.org/licenses/mit-license.php"
}
],
"repository": {
"type": "git",
"url": "https://github.com/rstiller/inspector-metrics"
},
"keywords": [
"metrics",
"monitoring",
"typescript",
"prometheus",
"prometheus-metrics"
],
"engines": {
"node": ">= 10",
"npm": ">= 3"
},
"peerDependencies": {
"inspector-metrics": ">=1.16.x"
},
"dependencies": {
"inspector-metrics": "^1.23.0",
"source-map-support": "^0.5.21"
},
"devDependencies": {
"@testdeck/mocha": "0.2.0",
"@types/chai": "4.3.1",
"@types/mocha": "9.1.0",
"@types/node": "17.0.25",
"@types/sinon": "10.0.11",
"@types/sinon-chai": "3.2.8",
"@typescript-eslint/eslint-plugin": "5.20.0",
"@typescript-eslint/parser": "5.20.0",
"chai": "4.3.6",
"eslint": "8.13.0",
"eslint-config-standard-with-typescript": "21.0.1",
"eslint-import-resolver-node": "0.3.6",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-promise": "6.0.0",
"mocha": "9.2.2",
"moment": "2.29.3",
"nock": "13.2.4",
"pkgsign": "0.2.0",
"reflect-metadata": "0.1.13",
"rimraf": "3.0.2",
"sinon": "13.0.2",
"sinon-chai": "3.7.0",
"ts-node": "10.7.0",
"typescript": "4.6.3"
},
"files": [
"build/lib/**/*"
],
"mocha": {
"require": [
"source-map-support/register",
"ts-node/register"
],
"reporter": "dot",
"extension": "ts",
"bail": true,
"full-trace": true,
"check-leaks": true,
"retries": 1
},
"scripts": {
"build": "npm run clean && npm run lint && npm run compile",
"sign": "pkgsign sign .",
"clean": "rimraf build",
"compile": "tsc --incremental -p .",
"deps": "npm-check --ignore @types/* --no-spinner --no-emoji --no-color",
"lint": "eslint ./lib/**/*.ts ./test/**/*.ts",
"test": "mocha test/**/*Test.ts"
},
"readme": "# inspector-prometheus\nTypescript metric reporter for [prometheus](https://prometheus.io).\n\n<p align=\"center\">\n <a href=\"https://www.npmjs.org/package/inspector-prometheus\">\n <img src=\"https://img.shields.io/npm/v/inspector-prometheus.svg\" alt=\"NPM Version\">\n </a>\n <a href=\"https://www.npmjs.org/package/inspector-prometheus\">\n <img src=\"https://img.shields.io/npm/l/inspector-prometheus.svg\" alt=\"License\">\n </a>\n <a href=\"https://github.com/rstiller/inspector-metrics/tree/master/packages/inspector-prometheus\">\n <img src=\"https://github.com/rstiller/inspector-metrics/workflows/CI/badge.svg\" alt=\"CI Status\">\n </a>\n</p>\n\nThis library is made for [inspector-metrics](https://github.com/rstiller/inspector-metrics)\nnode module and is meant to be used with `nodejs`. \n\nAll metrics from the [inspector-metrics](https://github.com/rstiller/inspector-metrics) library\ncan be pushed to a [pushgateway](https://github.com/prometheus/pushgateway) or be exposed with\na custom `/metrics` endpoint in your application.\n\nTake a look at the [Documentation](https://rstiller.github.io/inspector-metrics/).\n\n## install\n\n`npm install --save inspector-prometheus`\n\n## basic usage\n\n`example.ts`\n```typescript\nimport {\n MetricRegistry,\n} from \"inspector-metrics\";\n\nimport {\n PrometheusMetricReporter,\n} from \"inspector-prometheus\";\n\n// contains all metrics\nconst registry = new MetricRegistry();\n// exposes the metrics\nconst reporter = new PrometheusMetricReporter({});\n\n// register the registry within the reporter\nreporter.addMetricRegistry(registry);\n\n// common tags for all metrics\nconst tags = new Map();\ntags.set(\"app_version\", \"1.0.0\");\nreporter.setTags(tags);\n\n// a simple request timer used to report response latencies\nconst requests: Timer = registry.newTimer(\"requests\");\n// custom metric tag\nrequests.setTag(\"host\", \"127.0.0.3\");\n\n// some server implementation - could be anything KOA, Express, HAPI ...\nconst server = new Hapi.Server({ host: \"0.0.0.0\", port: 8080 });\n\n// '/metrics' is the standard route used by prometheus ...\nserver.route({\n method: \"GET\",\n path: \"/metrics\",\n handler(request, h) {\n console.log(\"reporting metrics\");\n return h.response(reporter.getMetricsString())\n .code(200)\n .type(\"text/plain\");\n },\n});\n\n// starts the server\nserver.start();\n```\n\n`/etc/prometheus/config.yml`\n```yaml\nglobal:\n scrape_interval: 15s\n evaluation_interval: 30s\n\nscrape_configs:\n- job_name: test-app\n metrics_path: /metrics\n static_configs:\n - targets:\n - localhost:8080\n```\n\n`example metrics report`\n```text\n# HELP requests request durations for some endpoint\n# TYPE requests summary\nrequests{app_version=\"1.0.0\",host=\"127.0.0.3\",quantile=\"0.01\"} 0\nrequests{app_version=\"1.0.0\",host=\"127.0.0.3\",quantile=\"0.05\"} 0\nrequests{app_version=\"1.0.0\",host=\"127.0.0.3\",quantile=\"0.5\"} 999936\nrequests{app_version=\"1.0.0\",host=\"127.0.0.3\",quantile=\"0.75\"} 999936\nrequests{app_version=\"1.0.0\",host=\"127.0.0.3\",quantile=\"0.9\"} 1000192\nrequests{app_version=\"1.0.0\",host=\"127.0.0.3\",quantile=\"0.95\"} 1000192\nrequests{app_version=\"1.0.0\",host=\"127.0.0.3\",quantile=\"0.98\"} 1999872\nrequests{app_version=\"1.0.0\",host=\"127.0.0.3\",quantile=\"0.99\"} 2000128\nrequests{app_version=\"1.0.0\",host=\"127.0.0.3\",quantile=\"0.999\"} 4000000\nrequests_count{app_version=\"1.0.0\",host=\"127.0.0.3\"} 362\nrequests_sum{app_version=\"1.0.0\",host=\"127.0.0.3\"} 283998208\n```\n\n### reporting options for PrometheusMetricReporter\n\n```typescript\nimport {\n PrometheusMetricReporter,\n} from \"inspector-prometheus\";\n\nconst reporter = new PrometheusMetricReporter({\n includeTimestamp: true,\n emitComments: true,\n useUntyped: false,\n});\n```\n\n### multi process support (nodejs cluster)\n\nDue to the nature of prometheus scraping multiple processes need to collect \nmetrics in order report all metrics of every process. \n\nTherefore the `PrometheusMetricReporter` implements an internal \nrequest/response mechanism to gather all metrics from all forked processes \nand wait for the response before serving all metrics data. \n\nYou should set the `pid` as reporter tag to be able to determine \nbetween the multiple metrics sources. \n\nOnly the master process should serve the metrics to the `prometheus` server. \n\n```typescript\nimport * as cluster from \"cluster\";\n\nimport {\n tagsToMap,\n} from \"inspector-metrics\";\n\nimport {\n PrometheusMetricReporter,\n} from \"inspector-prometheus\";\n\nconst reporter = new PrometheusMetricReporter({});\n\n// set \"pid\" to process id\nreporter.setTags(tagsToMap({\n pid: `${process.pid}`,\n}));\n\nif (cluster.isMaster) {\n // some server implementation - could be anything KOA, Express, HAPI ...\n const server = new Hapi.Server({ host: \"0.0.0.0\", port: 8080 });\n\n // '/metrics' is the standard route used by prometheus ...\n server.route({\n method: \"GET\",\n path: \"/metrics\",\n handler(request, h) {\n console.log(\"reporting metrics\");\n return h.response(reporter.getMetricsString())\n .code(200)\n .type(\"text/plain\");\n },\n });\n\n // starts the server\n server.start();\n}\n```\n\n## report metrics with pushgateway\n\n```typescript\nimport ...; // like in the example above\n\nimport {\n PrometheusMetricReporter,\n PushgatewayMetricReporter,\n} from \"inspector-prometheus\";\n\n// contains all metrics\nconst registry = new MetricRegistry();\n// exposes the metrics\nconst reporter = new PrometheusMetricReporter({});\n\n// register the registry within the reporter\nreporter.addMetricRegistry(registry);\n\nconst pushReporter = new PushgatewayMetricReporter({\n reporter,\n\n host: \"localhost\",\n port: 9091,\n job: \"pushgateway\",\n instance: \"127.0.0.4\",\n});\n\n// start reporting\nawait pushReporter.start();\n```\n\n### multi process support (nodejs cluster)\n\nBy default cluster support is disabled for `PushgatewayMetricReporter`. \nYou should set the `pid` as reporter tag. \n\n```typescript\nimport {\n tagsToMap,\n} from \"inspector-metrics\";\n\nimport {\n PrometheusMetricReporter,\n PushgatewayMetricReporter,\n} from \"inspector-prometheus\";\n\nconst reporter = new PrometheusMetricReporter({});\nconst pushReporter = new PushgatewayMetricReporter({\n reporter,\n ...\n});\n\n// set \"pid\" to process id\nreporter.setTags(tagsToMap({\n pid: `${process.pid}`,\n}));\n\n// start reporting\nawait pushReporter.start();\n```\n\n## License\n\n[MIT](https://www.opensource.org/licenses/mit-license.php)\n"
}