### Run Browser Example with Docker Source: https://github.com/samber/prometheus-query-js/blob/master/CONTRIBUTING.md Sets up a local Nginx server using Docker to host and serve the project's browser examples. After running, the examples can be accessed in a web browser at http://localhost:8080/examples/browser. ```bash docker run --rm -it -p 8080:80 -v `pwd`:/usr/share/nginx/html nginx # Then open http://localhost:8080/examples/browser ``` -------------------------------- ### Run NodeJS Example Source: https://github.com/samber/prometheus-query-js/blob/master/CONTRIBUTING.md Executes a specific example script designed for NodeJS environments. This command is useful for testing or demonstrating server-side functionalities. ```bash npm run example ``` -------------------------------- ### Install Prometheus Query Client for Node.js Source: https://github.com/samber/prometheus-query-js/blob/master/README.md Installs the `prometheus-query` package using npm for Node.js projects, making the library available for server-side applications. ```sh npm install prometheus-query ``` -------------------------------- ### Perform a Range Query with PrometheusDriver Source: https://github.com/samber/prometheus-query-js/blob/master/README.md Executes a range Prometheus query using the `rangeQuery` method to fetch time-series data over a specified time range and step. The example demonstrates querying 'up' metric for the past 24 hours and logging each series metric and its values. ```js // up during past 24h const q = 'up'; const start = new Date().getTime() - 24 * 60 * 60 * 1000; const end = new Date(); const step = 6 * 60 * 60; // 1 point every 6 hours prom.rangeQuery(q, start, end, step) .then((res) => { const series = res.result; series.forEach((serie) => { console.log("Serie:", serie.metric.toString()); console.log("Values:\n" + serie.values.join('\n')); }); }) .catch(console.error); ``` -------------------------------- ### PrometheusDriver Class and Query Methods Source: https://github.com/samber/prometheus-query-js/blob/master/README.md API documentation for the `PrometheusDriver` class, detailing its constructor for initialization and core methods for interacting with the Prometheus query API, including instant and range queries. ```APIDOC PrometheusDriver(options: object) - Initializes the PrometheusDriver client. - Parameters: - options: An object containing configuration for the driver. - endpoint: string - The base URL of the Prometheus server (e.g., "https://prometheus.demo.do.prometheus.io"). - baseURL: string (optional) - The base path for API requests (default: "/api/v1"). instantQuery(query: string): Promise - Executes an instant Prometheus query. - Parameters: - query: string - The PromQL expression to query. - Returns: Promise - A promise that resolves with the query result, containing series data with metric, time, and value. rangeQuery(query: string, start: number | Date, end: number | Date, step: number): Promise - Executes a range Prometheus query. - Parameters: - query: string - The PromQL expression to query. - start: number | Date - The start timestamp for the query range. - end: number | Date - The end timestamp for the query range. - step: number - The time step between data points in seconds. - Returns: Promise - A promise that resolves with the query result, containing series data with metric and an array of values over time. ``` -------------------------------- ### Initialize PrometheusDriver for Prometheus API Interaction Source: https://github.com/samber/prometheus-query-js/blob/master/README.md Demonstrates how to import and initialize the `PrometheusDriver` with a Prometheus endpoint and an optional base URL. This sets up the client for making subsequent queries to the Prometheus API. ```js import { PrometheusDriver } from 'prometheus-query'; const prom = new PrometheusDriver({ endpoint: "https://prometheus.demo.do.prometheus.io", baseURL: "/api/v1" // default value }); ``` -------------------------------- ### Publish Project to npm Registry Source: https://github.com/samber/prometheus-query-js/blob/master/CONTRIBUTING.md Builds the project for production and then publishes the package to the npm registry. It is essential to increment the version number in `package.json` before running these commands to ensure a new version is published. ```bash npm run build npm publish ``` -------------------------------- ### Include Prometheus Query Client in Browser HTML Source: https://github.com/samber/prometheus-query-js/blob/master/README.md Includes the Prometheus Query UMD bundle via jsDelivr CDN in an HTML script tag for browser usage, then demonstrates how to initialize the PrometheusDriver object globally. ```html ``` -------------------------------- ### Build Project for Development Source: https://github.com/samber/prometheus-query-js/blob/master/CONTRIBUTING.md Executes the development build script defined in the project's package.json, typically used for local development and quick iteration. ```bash npm run dev ``` -------------------------------- ### Run Project Tests Source: https://github.com/samber/prometheus-query-js/blob/master/CONTRIBUTING.md Executes the test suite defined in the project's package.json. This command is crucial for verifying code correctness and preventing regressions. ```sh npm run test ``` -------------------------------- ### Configure PrometheusDriver with Basic Authentication in TypeScript Source: https://github.com/samber/prometheus-query-js/blob/master/README.md Illustrates how to initialize the `PrometheusDriver` with basic authentication credentials (username and password) for secure access to a Prometheus endpoint. This ensures that all subsequent requests made by the driver include the specified authentication headers. ```ts new PrometheusDriver({ endpoint: "https://prometheus.demo.do.prometheus.io", auth: { username: 'foo', password: 'bar' } }); ``` -------------------------------- ### Perform an Instant Query with PrometheusDriver Source: https://github.com/samber/prometheus-query-js/blob/master/README.md Executes an instant Prometheus query using the `instantQuery` method to fetch the latest value for a given PromQL expression. The result is then processed to log the series metric, timestamp, and value. ```js // last `up` value const q = 'up{instance="demo.do.prometheus.io:9090",job="node"}'; prom.instantQuery(q) .then((res) => { const series = res.result; series.forEach((serie) => { console.log("Serie:", serie.metric.toString()); console.log("Time:", serie.value.time); console.log("Value:", serie.value.value); }); }) .catch(console.error); ``` -------------------------------- ### Configure PrometheusDriver to Use an HTTP Proxy in TypeScript Source: https://github.com/samber/prometheus-query-js/blob/master/README.md Shows how to configure the `PrometheusDriver` to route its HTTP requests through a specified proxy server. This is useful for environments where direct internet access is restricted or for debugging network traffic. ```ts new PrometheusDriver({ endpoint: "https://prometheus.demo.do.prometheus.io", proxy: { host: 'proxy.acme.com', port: 8080 } }); ``` -------------------------------- ### Implement Request and Response Interceptors for PrometheusDriver in TypeScript Source: https://github.com/samber/prometheus-query-js/blob/master/README.md Demonstrates how to add custom interceptors to the `PrometheusDriver` to hook into the HTTP request and response lifecycle. This allows for modifying requests before they are sent or processing responses before they are returned, handling both fulfilled and rejected promises for advanced control. ```ts new PrometheusDriver({ endpoint: "https://prometheus.demo.do.prometheus.io", proxy: { host: 'proxy.acme.com', port: 8080 }, requestInterceptor: { onFulfilled: (config: AxiosRequestConfig) => { return config; }, onRejected: (error: any) => { return Promise.reject(error.message); } }, responseInterceptor: { onFulfilled: (res: AxiosResponse) => { return res; }, onRejected: (error: any) => { return Promise.reject(error.message); } } }); ``` -------------------------------- ### Configure PrometheusDriver with Cookie-based Authentication in TypeScript Source: https://github.com/samber/prometheus-query-js/blob/master/README.md Demonstrates how to configure the `PrometheusDriver` to use cookie-based authentication by setting `withCredentials` to true. This enables the client to send and receive cookies, which is essential for session-based authentication mechanisms. ```ts new PrometheusDriver({ endpoint: "https://prometheus.demo.do.prometheus.io", withCredentials: true }); ``` -------------------------------- ### Query Prometheus for Series Data using JavaScript Source: https://github.com/samber/prometheus-query-js/blob/master/README.md Demonstrates how to query Prometheus for time series data matching a specific query string within a given time range using the `prom.series` method. It logs the matching series names to the console. ```js const match = 'up'; const start = new Date().getTime() - 24 * 60 * 60 * 1000; const end = new Date(); prom.series(match, start, end) .then((res) => { console.log('Series:'); console.log(res.join('\n')); }) .catch(console.error); ``` -------------------------------- ### Retrieve Active Prometheus Alerts with JavaScript Source: https://github.com/samber/prometheus-query-js/blob/master/README.md Shows how to fetch all currently active alerts from Prometheus using the `prom.alerts` method. The result is an array of Alert objects, each containing details like labels, state, and active time. ```js prom.alerts() .then(console.log) .catch(console.error); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.