Lightweight Prometheus API serves as a powerful tool for managing your Prometheus configuration remotely. This code was developed as part of one of our latest projects, and I've decided to publish it because I find it incredibly useful. Written in Rust, the project offers basic authentication and various API endpoints for versatile management of your Prometheus setup.
- Flexible Endpoints: Numerous API calls available for managing Prometheus settings.
 - Basic Authentication: Secure your Prometheus configurations with a configurable 
x-authtoken. - Rust-powered: Built with the speed and safety of Rust.
 
- Rust (latest stable version)
 - Prometheus installed and running
 
To get started, follow the instructions below.
git clone https://github.com/amallek/prometheus-api.gitcd prometheus-api# Uncomment the next line if Rust is not installed
# curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
cargo build --release# Uncomment to generate self-signed TLS certs
# ./helpers/create-cert.sh
./target/release/prometheus-apiIf desired, you can install prometheus-api as a service. An example script is available in helpers/create-service.sh.
# Uncomment the next line to execute the script
# ./helpers/create-service.sh
systemctl start prometheus-apiThe API provides a range of functionalities through the following endpoints:
- Endpoint: 
GET /info - Description: Retrieves basic information about the Prometheus API
 - Authentication: Required (
x-authtoken) 
- Endpoint: 
GET /config - Description: Fetches the current 
prometheus.ymlconfiguration in JSON format. - Authentication: Required (
x-authtoken) 
- Endpoint: 
GET /endpoint/{endpoint} - Description: Obtains details about a specified Prometheus scraping endpoint.
 - Authentication: Required (
x-authtoken) 
- Endpoint: 
PUT /endpoint - Description: Adds a new scraping endpoint to the Prometheus configuration.
 - Payload: JSON object with endpoint details.
 - Authentication: Required (
x-authtoken) 
- Endpoint: 
DELETE /endpoint/{endpoint} - Description: Removes a specified scraping endpoint from the Prometheus configuration.
 - Authentication: Required (
x-authtoken) 
- Endpoint: 
POST /endpoint - Description: Updates an existing scraping endpoint in the Prometheus configuration.
 - Payload: JSON object with updated endpoint details.
 - Authentication: Required (
x-authtoken) 
The payload required consists of literally what you would see in the prometheus.yaml file, but JSON encoded. For instance:
curl -ikX PUT \
    'https://localhost:9090/endpoint' \
    -H 'x-auth: my_password' \
    -d '{
 "job_name": "my-job-name",
 "scrape_interval": "60",
 "scrape_timeout": "5",
 "scheme": "https",
 "static_configs": [
    {
        "targets": ["my.machine.example.com:9091"],
        "labels": {"label1":"val","label2":"val"}
    }
 ]
}'
#{"success":true}curl -ik 'https://localhost:9090/endpoint/my-job-name' \
    -H 'x-auth: my_password'curl -ikX DELETE \
    'https://localhost:9090/endpoint/my-job-name' \
    -H 'x-auth: my_password'Include an x-auth token in every API call to ensure authentication. The token can be updated or replaced by custom logic in the verify_token() method (see src/security.rs).
This project is licensed under the terms of the MIT License (see LICENSE.)