{"id":19288520,"url":"https://github.com/samber/prometheus-query-js","last_synced_at":"2025-04-12T21:33:54.553Z","repository":{"id":41998732,"uuid":"239358125","full_name":"samber/prometheus-query-js","owner":"samber","description":"📊 A Javascript client for Prometheus query API","archived":false,"fork":false,"pushed_at":"2024-11-11T16:33:55.000Z","size":817,"stargazers_count":64,"open_issues_count":4,"forks_count":17,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-11T14:23:21.867Z","etag":null,"topics":["admin","api","client","javascript","label","metrics","prometheus","query","series","timeseries","tsdb"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/prometheus-query","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/samber.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["samber"]}},"created_at":"2020-02-09T19:05:15.000Z","updated_at":"2025-01-14T02:30:48.000Z","dependencies_parsed_at":"2023-02-08T06:31:18.727Z","dependency_job_id":"087bf73c-b28a-4d5d-9fbb-4d3236e22187","html_url":"https://github.com/samber/prometheus-query-js","commit_stats":{"total_commits":67,"total_committers":8,"mean_commits":8.375,"dds":"0.22388059701492535","last_synced_commit":"8183a5be0250e8836b89a0cfec7d22be669378cf"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samber%2Fprometheus-query-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samber%2Fprometheus-query-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samber%2Fprometheus-query-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samber%2Fprometheus-query-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samber","download_url":"https://codeload.github.com/samber/prometheus-query-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248420245,"owners_count":21100344,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["admin","api","client","javascript","label","metrics","prometheus","query","series","timeseries","tsdb"],"created_at":"2024-11-09T22:09:15.400Z","updated_at":"2025-04-12T21:33:54.529Z","avatar_url":"https://github.com/samber.png","language":"TypeScript","funding_links":["https://github.com/sponsors/samber","https://www.patreon.com/samber"],"categories":[],"sub_categories":[],"readme":"\n# Welcome to prometheus-query  👋\n[![NPM version](https://img.shields.io/npm/v/prometheus-query.svg?style=flat-square)](https://npmjs.org/package/prometheus-query)\n\u003ca href=\"https://www.jsdelivr.com/package/npm/prometheus-query\"\u003e\u003cimg src=\"https://data.jsdelivr.com/v1/package/npm/prometheus-query/badge\" alt=\"jsDelivr Downloads\"\u003e\u003c/img\u003e\u003c/a\u003e\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](#)\n\n\u003e A Javascript client for Prometheus **query** API.\n\n## ✨ Features\n\n- Thin \u0026 **minimal low-level HTTP client** to interact with Prometheus's API\n- Works both on the **browser** and **node.js**\n- **UMD compatible**, you can use it with any module loader\n- Supports **query** and admin [APIs](https://prometheus.io/docs/prometheus/latest/querying/api/)\n\n⚠️ This library does not export metrics. Please use [prom-client](https://github.com/siimon/prom-client) instead.\n\nFor building shiny Charts, you may like this chartjs plugin: [samber/chartjs-plugin-datasource-prometheus](https://github.com/samber/chartjs-plugin-datasource-prometheus).\n\n\u003cdiv align=\"center\"\u003e\n  \u003chr\u003e\n  \u003csup\u003e\u003cb\u003eSponsored by:\u003c/b\u003e\u003c/sup\u003e\n  \u003cbr\u003e\n  \u003ca href=\"https://quickwit.io?utm_campaign=github_sponsorship\u0026utm_medium=referral\u0026utm_content=samber-prometheus-query-js\u0026utm_source=github\"\u003e\n    \u003cdiv\u003e\n      \u003cimg src=\"https://github.com/samber/oops/assets/2951285/49aaaa2b-b8c6-4f21-909f-c12577bb6a2e\" width=\"240\" alt=\"Quickwit\"\u003e\n    \u003c/div\u003e\n    \u003cdiv\u003e\n      Cloud-native search engine for observability - An OSS alternative to Splunk, Elasticsearch, Loki, and Tempo.\n    \u003c/div\u003e\n  \u003c/a\u003e\n  \u003chr\u003e\n\u003c/div\u003e\n\n## 🚀 Install\n\n### NodeJS\n\n```sh\nnpm install prometheus-query\n```\n\n### Upgrade from v2 to v3\n\n- `prometheus-query-js` has been recoded into Typescript.\n- Type definitions.\n- API update:\n  - `PrometheusQuery` is not the default export anymore.\n  - `PrometheusQuery` has been renamed as `PrometheusDriver`.\n  - [See examples](./examples/nodejs/app.ts)\n\n### Browser\n\n```html\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/prometheus-query/dist/prometheus-query.umd.min.js\"\u003e\u003c/script\u003e\n\n\u003cscript type=\"application/javacript\"\u003e\n\tconst prom = new Prometheus.PrometheusDriver(...);\n\u003c/script\u003e\n```\n\n## 💡 Quick start\n\n```js\nimport { PrometheusDriver } from 'prometheus-query';\n\nconst prom = new PrometheusDriver({\n    endpoint: \"https://prometheus.demo.do.prometheus.io\",\n    baseURL: \"/api/v1\" // default value\n});\n```\n\n### Instant query\n\n```js\n// last `up` value\nconst q = 'up{instance=\"demo.do.prometheus.io:9090\",job=\"node\"}';\nprom.instantQuery(q)\n    .then((res) =\u003e {\n        const series = res.result;\n        series.forEach((serie) =\u003e {\n            console.log(\"Serie:\", serie.metric.toString());\n            console.log(\"Time:\", serie.value.time);\n            console.log(\"Value:\", serie.value.value);\n        });\n    })\n    .catch(console.error);\n```\n\nOutput:\n\n```txt\nSerie: up{instance=\"prometheus.demo.do.prometheus.io:9100\", job=\"node\"}\nTime: Sun Feb 16 2020 18:33:59 GMT+0100 (Central European Standard Time)\nValue: 1\n```\n\n### Range query\n\n```js\n// up during past 24h\nconst q = 'up';\nconst start = new Date().getTime() - 24 * 60 * 60 * 1000;\nconst end = new Date();\nconst step = 6 * 60 * 60; // 1 point every 6 hours\n\nprom.rangeQuery(q, start, end, step)\n    .then((res) =\u003e {\n        const series = res.result;\n        series.forEach((serie) =\u003e {\n            console.log(\"Serie:\", serie.metric.toString());\n            console.log(\"Values:\\n\" + serie.values.join('\\n'));\n        });\n    })\n    .catch(console.error);\n```\n\nOutput:\n\n```txt\nSerie: up{instance=\"prometheus.demo.do.prometheus.io:9090\", job=\"prometheus\"}\nValues:\nSat Feb 15 2020 18:21:47 GMT+0100 (Central European Standard Time): 1\nSun Feb 16 2020 00:21:47 GMT+0100 (Central European Standard Time): 1\nSun Feb 16 2020 06:21:47 GMT+0100 (Central European Standard Time): 1\nSun Feb 16 2020 12:21:47 GMT+0100 (Central European Standard Time): 1\nSun Feb 16 2020 18:21:47 GMT+0100 (Central European Standard Time): 1\n\nSerie: up{instance=\"prometheus.demo.do.prometheus.io:9093\", job=\"alertmanager\"}\nValues:\nSat Feb 15 2020 18:21:47 GMT+0100 (Central European Standard Time): 1\nSun Feb 16 2020 00:21:47 GMT+0100 (Central European Standard Time): 1\nSun Feb 16 2020 06:21:47 GMT+0100 (Central European Standard Time): 1\nSun Feb 16 2020 12:21:47 GMT+0100 (Central European Standard Time): 1\nSun Feb 16 2020 18:21:47 GMT+0100 (Central European Standard Time): 1\n\nSerie: up{instance=\"prometheus.demo.do.prometheus.io:9100\", job=\"node\"}\nValues:\nSat Feb 15 2020 18:20:51 GMT+0100 (Central European Standard Time): 1\nSun Feb 16 2020 00:20:51 GMT+0100 (Central European Standard Time): 1\nSun Feb 16 2020 06:20:51 GMT+0100 (Central European Standard Time): 1\nSun Feb 16 2020 12:20:51 GMT+0100 (Central European Standard Time): 1\nSun Feb 16 2020 18:20:51 GMT+0100 (Central European Standard Time): 1\n```\n\n### List series matching query\n\n```js\nconst match = 'up';\nconst start = new Date().getTime() - 24 * 60 * 60 * 1000;\nconst end = new Date();\n\nprom.series(match, start, end)\n    .then((res) =\u003e {\n        console.log('Series:');\n        console.log(res.join('\\n'));\n    })\n    .catch(console.error);\n```\n\nOutput:\n\n```txt\nup{instance=\"demo.do.prometheus.io:9090\", job=\"prometheus\"}\nup{instance=\"demo.do.prometheus.io:9093\", job=\"alertmanager\"}\nup{instance=\"demo.do.prometheus.io:9100\", job=\"node\"}\n```\n\n### List all active alerts\n\n```js\nprom.alerts()\n    .then(console.log)\n    .catch(console.error);\n```\n\nOutput:\n\n```js\n[\n  Alert {\n    activeAt: 2019-11-14T20:04:36.629Z,\n    annotations: {},\n    labels: { alertname: 'ExampleAlertAlwaysFiring', job: 'alertmanager' },\n    state: 'firing',\n    value: 1\n  },\n  Alert {\n    activeAt: 2019-11-14T20:04:36.629Z,\n    annotations: {},\n    labels: { alertname: 'ExampleAlertAlwaysFiring', job: 'node' },\n    state: 'firing',\n    value: 1\n  },\n  Alert {\n    activeAt: 2019-11-14T20:04:36.629Z,\n    annotations: {},\n    labels: { alertname: 'ExampleAlertAlwaysFiring', job: 'prometheus' },\n    state: 'firing',\n    value: 1\n  },\n  Alert {\n    activeAt: 2019-11-14T20:04:36.629Z,\n    annotations: {},\n    labels: { alertname: 'ExampleAlertAlwaysFiring', job: 'pushgateway' },\n    state: 'firing',\n    value: 1\n  }\n]\n```\n\n### Authenticated query\n\nUsing basic auth:\n\n```ts\nnew PrometheusDriver({\n    endpoint: \"https://prometheus.demo.do.prometheus.io\",\n    auth: {\n        username: 'foo',\n        password: 'bar'\n    }\n});\n```\n\nUsing cookies:\n\n```ts\nnew PrometheusDriver({\n    endpoint: \"https://prometheus.demo.do.prometheus.io\",\n    withCredentials: true\n});\n```\n\n### Proxy\n\n```ts\nnew PrometheusDriver({\n    endpoint: \"https://prometheus.demo.do.prometheus.io\",\n    proxy: {\n        host: 'proxy.acme.com',\n        port: 8080\n    }\n});\n```\n\n### Hook HTTP requests and responses\n\n```ts\nnew PrometheusDriver({\n    endpoint: \"https://prometheus.demo.do.prometheus.io\",\n    proxy: {\n        host: 'proxy.acme.com',\n        port: 8080\n    },\n    requestInterceptor: {\n        onFulfilled: (config: AxiosRequestConfig) =\u003e {\n            return config;\n        },\n        onRejected: (error: any) =\u003e {\n            return Promise.reject(error.message);\n        }\n    },\n    responseInterceptor: {\n        onFulfilled: (res: AxiosResponse) =\u003e {\n            return res;\n        },\n        onRejected: (error: any) =\u003e {\n            return Promise.reject(error.message);\n        }\n    }\n});\n```\n\n## 🔐 Security advisory\n\nIf you open a Prometheus instance on Internet, it would be a good idea to block some routes.\n\nStart by blocking `/api/v1/admin`. I'm pretty sure allowing only `/api/v1/query` and `/api/v1/query_range` will match your needs.\n\nAlso don't use Prometheus as a multitenant timeseries database!\n\nAt your own risk... 😘\n\n## 🤝 Contributing\n\nThe Prometheus Query client is open source and contributions from the community (you!) are welcome.\n\nThere are many ways to contribute: writing code, documentation, reporting issues...\n\n[How-to](./CONTRIBUTING.md)\n\n## Author\n\n👤 **Samuel Berthe**\n\n* Twitter: [@samuelberthe](https://twitter.com/samuelberthe)\n* Github: [@samber](https://github.com/samber)\n\n## 💫 Show your support\n\nGive a ⭐️ if this project helped you!\n\n[![support us](https://c5.patreon.com/external/logo/become_a_patron_button.png)](https://www.patreon.com/samber)\n\n## 📝 License\n\nCopyright © 2020 [Samuel Berthe](https://github.com/samber).\n\nThis project is [MIT](./LICENSE) licensed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamber%2Fprometheus-query-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamber%2Fprometheus-query-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamber%2Fprometheus-query-js/lists"}