From 822bf1c047d195ae803000a4cb2ce53223ab9b7a Mon Sep 17 00:00:00 2001 From: Lujeni Date: Sat, 28 Dec 2024 15:05:09 +0100 Subject: [PATCH] build(pyproject): initial support --- README.md | 55 +++++++++++++++++++------------------------ pyproject.toml | 31 ++++++++++++++++++++++++ requirements.txt | 8 +------ typesense_exporter.py | 11 --------- 4 files changed, 56 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index ecf577b..d456238 100644 --- a/README.md +++ b/README.md @@ -9,35 +9,26 @@ A **Prometheus** exporter that queries a [Typesense](https://typesense.org/) clu - **Collections**: Exposes per-collection document counts. - **Automatic SSL Verification Control**: Optionally disable SSL cert verification for development or when using self-signed certs. -## Requirements - -- Python 3.7+ -- [typesense](https://pypi.org/project/typesense/) (Python client) -- [prometheus_client](https://pypi.org/project/prometheus_client/) -- [requests](https://pypi.org/project/requests/) - -You can install these with: - -```bash -pip install prometheus_client requests typesense -``` - ## Usage 1. Clone or copy the script into your environment. 2. Install Dependencies: + ```bash -pip install prometheus_client requests typesense +pip install -r requirements.txt ``` 3. Run the Exporter: + ```bash ./typesense_exporter.py --port 8000 \ --typesense-api-key "YOUR_API_KEY" \ --typesense-nodes "host1:8108,host2:8108" ``` + Or rely on environment variables: + ```bash export TYPESENSE_API_KEY="YOUR_API_KEY" export TYPESENSE_NODES="host1:8108,host2:8108" @@ -47,6 +38,7 @@ export TYPESENSE_NODES="host1:8108,host2:8108" 4. Scrape with Prometheus: Add to your Prometheus `prometheus.yml`: + ```yaml scrape_configs: - job_name: "typesense_exporter" @@ -60,33 +52,34 @@ Navigate to http://localhost:8000/metrics in your browser or use `curl http://lo ## Command-Line Arguments -| Argument | Env Var | Default | Description | -|---------------------------|----------------------------|---------------------------------------------|------------------------------------------------------------------------------------------------------------------| -| `--typesense-api-key` | `TYPESENSE_API_KEY` | *(none)* | Your Typesense API key. | -| `--typesense-metrics-url` | `TYPESENSE_METRICS_URL` | `https://localhost:8108/metrics.json` | The full URL to `metrics.json` endpoint. | -| `--typesense-stats-url` | `TYPESENSE_STATS_URL` | `https://localhost:8108/stats.json` | The full URL to `stats.json` endpoint. | -| `--typesense-debug-url` | `TYPESENSE_DEBUG_URL` | `https://localhost:8108/debug` | The full URL to `stats.json` endpoint. | -| `--typesense-nodes` | `TYPESENSE_NODES` | `localhost:8108` | A comma-separated list of `host:port` entries for Typesense nodes (e.g., `node1:8108,node2:8108`). | -| `--verify` | `VERIFY_SSL` | `False` | Verify SSL certificates. Set `--verify` to enable, or `VERIFY_SSL=true` for environment. | -| `--port` | *(not applicable)* | `8000` | Which port the exporter will listen on for Prometheus scrapes. | +| Argument | Env Var | Default | Description | +| ------------------------- | ----------------------- | ------------------------------------- | -------------------------------------------------------------------------------------------------- | +| `--typesense-api-key` | `TYPESENSE_API_KEY` | _(none)_ | Your Typesense API key. | +| `--typesense-metrics-url` | `TYPESENSE_METRICS_URL` | `https://localhost:8108/metrics.json` | The full URL to `metrics.json` endpoint. | +| `--typesense-stats-url` | `TYPESENSE_STATS_URL` | `https://localhost:8108/stats.json` | The full URL to `stats.json` endpoint. | +| `--typesense-debug-url` | `TYPESENSE_DEBUG_URL` | `https://localhost:8108/debug` | The full URL to `stats.json` endpoint. | +| `--typesense-nodes` | `TYPESENSE_NODES` | `localhost:8108` | A comma-separated list of `host:port` entries for Typesense nodes (e.g., `node1:8108,node2:8108`). | +| `--verify` | `VERIFY_SSL` | `False` | Verify SSL certificates. Set `--verify` to enable, or `VERIFY_SSL=true` for environment. | +| `--port` | _(not applicable)_ | `8000` | Which port the exporter will listen on for Prometheus scrapes. | > **Tip**: Command-line arguments override environment variables, which override the defaults. ## How It Works -* The script registers a custom TypesenseCollector with Prometheus. -* Every time Prometheus sends a GET request to /metrics: - * The collector fetches /metrics.json, /stats.json, and the list of collections from the configured Typesense node(s). - * Each field is converted to a Prometheus metric and yielded dynamically. +- The script registers a custom TypesenseCollector with Prometheus. +- Every time Prometheus sends a GET request to /metrics: + - The collector fetches /metrics.json, /stats.json, and the list of collections from the configured Typesense node(s). + - Each field is converted to a Prometheus metric and yielded dynamically. This design guarantees that metrics are always up-to-date at scrape time (with no in-memory caching or stale metrics). ## Customization -* Modify `_collect_metrics_json` or `_collect_stats_json` to handle additional fields or parse additional endpoints as needed. -* Adjust `_sanitize_metric_name` if you want to add or remove transformations for metric names. -* Wrap the exporter in Docker or a systemd service to manage it in production environments. +- Modify `_collect_metrics_json` or `_collect_stats_json` to handle additional fields or parse additional endpoints as needed. +- Adjust `_sanitize_metric_name` if you want to add or remove transformations for metric names. +- Wrap the exporter in Docker or a systemd service to manage it in production environments. ## License -This exporter is released under the MIT License. See LICENSE for details (or replace with your preferred license). \ No newline at end of file +This exporter is released under the MIT License. See LICENSE for details (or replace with your preferred license). + diff --git a/pyproject.toml b/pyproject.toml index d98e25c..09bbbeb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,34 @@ +[project] +name = "typesense_exporter" +version = "0.1.0" +description = "A Prometheus exporter for Typesense." +authors = [{name = "Solvik"}] +readme = "README.md" +license = {file = "LICENSE"} +requires-python = ">=3.8" + +[project.urls] +homepage = "https://github.com/numberly/typesense_exporter" +repository = "https://github.com/numberly/typesense_exporter" + +[dependencies] +certifi = "2024.12.14" +charset-normalizer = "3.4.1" +idna = "3.10" +prometheus-client = "0.16.0" +requests = "2.32.2" +typesense = "0.21.0" +urllib3 = "2.3.0" + +[dev-dependencies] +pytest = "^7.0" +mypy = "^1.14" +types-requests = "^2.32" + +[build-system] +requires = ["setuptools", "wheel"] +build-backend = "setuptools.build_meta" + [tool.commitizen] name = "cz_conventional_commits" tag_format = "$version" diff --git a/requirements.txt b/requirements.txt index 0a9ad7f..d6e1198 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1 @@ -certifi==2024.12.14 -charset-normalizer==3.4.1 -idna==3.10 -prometheus-client==0.16.0 -requests==2.32.2 -typesense==0.21.0 -urllib3==2.3.0 +-e . diff --git a/typesense_exporter.py b/typesense_exporter.py index be30c6b..0605b94 100644 --- a/typesense_exporter.py +++ b/typesense_exporter.py @@ -4,17 +4,6 @@ each time Prometheus scrapes the /metrics endpoint. This approach ensures fresh metrics on every scrape, without running a separate polling loop. - -Usage: - 1. Install dependencies: - pip install prometheus_client requests typesense - 2. Run this script, e.g.: - ./typesense_exporter.py --port=8000 - 3. Add a scrape config in Prometheus to target this exporter: - scrape_configs: - - job_name: "typesense_exporter" - static_configs: - - targets: ["localhost:8000"] """ import os