Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 24 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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).
This exporter is released under the MIT License. See LICENSE for details (or replace with your preferred license).

31 changes: 31 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
8 changes: 1 addition & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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 .
11 changes: 0 additions & 11 deletions typesense_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading