JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 13671
  • Score
    100M100P100Q168279F

A Javascript client for Prometheus query API

Package Exports

  • prometheus-query

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (prometheus-query) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Welcome to prometheus-query 👋

NPM version License: MIT

A Javascript client for Prometheus query and admin API.

⚠️ This library does not export metrics. Please use prom-client instead.

✨ Features

  • Thin & minimal low-level HTTP client to interact with Prometheus's API
  • Works both on the browser and node.js
  • UMD compatible, you can use it with any module loader
  • Supports query and admin APIs

🚀 Install

NodeJS

npm install prometheus-query

Browser

💡 Quick start

const PrometheusQuery = require('prometheus-query');

const pq = new PrometheusQuery({
    endpoint: "http://demo.robustperception.io:9090",
});

Instant query

// last `up` value
pq.instantQuery('up{instance="demo.robustperception.io:9100",job="node"}')
    .then((res) => {
        console.log("Time:", new Date(res.data.result[0].value[0] * 1000));
        console.log("Value:", res.data.result[0].value[1]);
    })
    .catch(console.error);

Output:

Time: Sun Feb 09 2020 22:04:41 GMT+0100 (Central European Standard Time)
Value: 1

Range query

// up during past 24h, 1 point every 6 hours
pq.rangeQuery("up", new Date().getTime() - 24 * 60 * 60 * 1000, new Date(), 6 * 60 * 60)
    .then((res) => {
        const series = res.data.result;
        series.forEach((serie) => {
            const name = serie.metric['__name__'];
            const labels = serie.metric;

            delete labels['__name__'];
            strLabels = Object.keys(labels).map((curr) => curr + '="' + labels[curr] + '"');
            const serieName = name + '{' + strLabels.join(', ') + '}';

            console.log("Serie:", serieName);
            console.log("Values:", JSON.stringify(serie.values));
        });
    })
    .catch(console.error(err));

Output:

Serie: up{instance="demo.robustperception.io:9090", job="prometheus"}
Values: [[1581196201.66,"1"],[1581217801.66,"1"],[1581239401.66,"1"],[1581261001.66,"1"],[1581282601.66,"1"]]

Serie: up{instance="demo.robustperception.io:9091", job="pushgateway"}
Values: [[1581196201.66,"1"],[1581217801.66,"1"],[1581239401.66,"1"],[1581261001.66,"1"],[1581282601.66,"1"]]

Serie: up{instance="demo.robustperception.io:9093", job="alertmanager"}
Values: [[1581196201.66,"1"],[1581217801.66,"1"],[1581239401.66,"1"],[1581261001.66,"1"],[1581282601.66,"1"]]

Serie: up{instance="demo.robustperception.io:9100", job="node"}
Values: [[1581196201.66,"1"],[1581217801.66,"1"],[1581239401.66,"1"],[1581261001.66,"1"],[1581282601.66,"1"]]

🏋️‍♂️ Documentation

✅ Run tests

npm run test

Contribute

The Prometheus Query client is open source and contributions from community (you!) are welcomed.

There are many ways to contribute: writing code, documentation, reporting issues...

Author

👤 Samuel Berthe

Show your support

Give a ⭐️ if this project helped you!

![support us](https://img.shields.io/badge/become-a patreon%20us-orange.svg?cacheSeconds=2592000)