Skip to content

Commit 822bf1c

Browse files
committed
build(pyproject): initial support
1 parent ed48ac8 commit 822bf1c

File tree

4 files changed

+56
-49
lines changed

4 files changed

+56
-49
lines changed

README.md

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,26 @@ A **Prometheus** exporter that queries a [Typesense](https://typesense.org/) clu
99
- **Collections**: Exposes per-collection document counts.
1010
- **Automatic SSL Verification Control**: Optionally disable SSL cert verification for development or when using self-signed certs.
1111

12-
## Requirements
13-
14-
- Python 3.7+
15-
- [typesense](https://pypi.org/project/typesense/) (Python client)
16-
- [prometheus_client](https://pypi.org/project/prometheus_client/)
17-
- [requests](https://pypi.org/project/requests/)
18-
19-
You can install these with:
20-
21-
```bash
22-
pip install prometheus_client requests typesense
23-
```
24-
2512
## Usage
2613

2714
1. Clone or copy the script into your environment.
2815

2916
2. Install Dependencies:
17+
3018
```bash
31-
pip install prometheus_client requests typesense
19+
pip install -r requirements.txt
3220
```
3321

3422
3. Run the Exporter:
23+
3524
```bash
3625
./typesense_exporter.py --port 8000 \
3726
--typesense-api-key "YOUR_API_KEY" \
3827
--typesense-nodes "host1:8108,host2:8108"
3928
```
29+
4030
Or rely on environment variables:
31+
4132
```bash
4233
export TYPESENSE_API_KEY="YOUR_API_KEY"
4334
export TYPESENSE_NODES="host1:8108,host2:8108"
@@ -47,6 +38,7 @@ export TYPESENSE_NODES="host1:8108,host2:8108"
4738
4. Scrape with Prometheus:
4839

4940
Add to your Prometheus `prometheus.yml`:
41+
5042
```yaml
5143
scrape_configs:
5244
- job_name: "typesense_exporter"
@@ -60,33 +52,34 @@ Navigate to http://localhost:8000/metrics in your browser or use `curl http://lo
6052

6153
## Command-Line Arguments
6254

63-
| Argument | Env Var | Default | Description |
64-
|---------------------------|----------------------------|---------------------------------------------|------------------------------------------------------------------------------------------------------------------|
65-
| `--typesense-api-key` | `TYPESENSE_API_KEY` | *(none)* | Your Typesense API key. |
66-
| `--typesense-metrics-url` | `TYPESENSE_METRICS_URL` | `https://localhost:8108/metrics.json` | The full URL to `metrics.json` endpoint. |
67-
| `--typesense-stats-url` | `TYPESENSE_STATS_URL` | `https://localhost:8108/stats.json` | The full URL to `stats.json` endpoint. |
68-
| `--typesense-debug-url` | `TYPESENSE_DEBUG_URL` | `https://localhost:8108/debug` | The full URL to `stats.json` endpoint. |
69-
| `--typesense-nodes` | `TYPESENSE_NODES` | `localhost:8108` | A comma-separated list of `host:port` entries for Typesense nodes (e.g., `node1:8108,node2:8108`). |
70-
| `--verify` | `VERIFY_SSL` | `False` | Verify SSL certificates. Set `--verify` to enable, or `VERIFY_SSL=true` for environment. |
71-
| `--port` | *(not applicable)* | `8000` | Which port the exporter will listen on for Prometheus scrapes. |
55+
| Argument | Env Var | Default | Description |
56+
| ------------------------- | ----------------------- | ------------------------------------- | -------------------------------------------------------------------------------------------------- |
57+
| `--typesense-api-key` | `TYPESENSE_API_KEY` | _(none)_ | Your Typesense API key. |
58+
| `--typesense-metrics-url` | `TYPESENSE_METRICS_URL` | `https://localhost:8108/metrics.json` | The full URL to `metrics.json` endpoint. |
59+
| `--typesense-stats-url` | `TYPESENSE_STATS_URL` | `https://localhost:8108/stats.json` | The full URL to `stats.json` endpoint. |
60+
| `--typesense-debug-url` | `TYPESENSE_DEBUG_URL` | `https://localhost:8108/debug` | The full URL to `stats.json` endpoint. |
61+
| `--typesense-nodes` | `TYPESENSE_NODES` | `localhost:8108` | A comma-separated list of `host:port` entries for Typesense nodes (e.g., `node1:8108,node2:8108`). |
62+
| `--verify` | `VERIFY_SSL` | `False` | Verify SSL certificates. Set `--verify` to enable, or `VERIFY_SSL=true` for environment. |
63+
| `--port` | _(not applicable)_ | `8000` | Which port the exporter will listen on for Prometheus scrapes. |
7264

7365
> **Tip**: Command-line arguments override environment variables, which override the defaults.
7466

7567
## How It Works
7668

77-
* The script registers a custom TypesenseCollector with Prometheus.
78-
* Every time Prometheus sends a GET request to /metrics:
79-
* The collector fetches /metrics.json, /stats.json, and the list of collections from the configured Typesense node(s).
80-
* Each field is converted to a Prometheus metric and yielded dynamically.
69+
- The script registers a custom TypesenseCollector with Prometheus.
70+
- Every time Prometheus sends a GET request to /metrics:
71+
- The collector fetches /metrics.json, /stats.json, and the list of collections from the configured Typesense node(s).
72+
- Each field is converted to a Prometheus metric and yielded dynamically.
8173

8274
This design guarantees that metrics are always up-to-date at scrape time (with no in-memory caching or stale metrics).
8375

8476
## Customization
85-
* Modify `_collect_metrics_json` or `_collect_stats_json` to handle additional fields or parse additional endpoints as needed.
86-
* Adjust `_sanitize_metric_name` if you want to add or remove transformations for metric names.
87-
* Wrap the exporter in Docker or a systemd service to manage it in production environments.
8877

78+
- Modify `_collect_metrics_json` or `_collect_stats_json` to handle additional fields or parse additional endpoints as needed.
79+
- Adjust `_sanitize_metric_name` if you want to add or remove transformations for metric names.
80+
- Wrap the exporter in Docker or a systemd service to manage it in production environments.
8981

9082
## License
9183

92-
This exporter is released under the MIT License. See LICENSE for details (or replace with your preferred license).
84+
This exporter is released under the MIT License. See LICENSE for details (or replace with your preferred license).
85+

pyproject.toml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
[project]
2+
name = "typesense_exporter"
3+
version = "0.1.0"
4+
description = "A Prometheus exporter for Typesense."
5+
authors = [{name = "Solvik"}]
6+
readme = "README.md"
7+
license = {file = "LICENSE"}
8+
requires-python = ">=3.8"
9+
10+
[project.urls]
11+
homepage = "https://github.com/numberly/typesense_exporter"
12+
repository = "https://github.com/numberly/typesense_exporter"
13+
14+
[dependencies]
15+
certifi = "2024.12.14"
16+
charset-normalizer = "3.4.1"
17+
idna = "3.10"
18+
prometheus-client = "0.16.0"
19+
requests = "2.32.2"
20+
typesense = "0.21.0"
21+
urllib3 = "2.3.0"
22+
23+
[dev-dependencies]
24+
pytest = "^7.0"
25+
mypy = "^1.14"
26+
types-requests = "^2.32"
27+
28+
[build-system]
29+
requires = ["setuptools", "wheel"]
30+
build-backend = "setuptools.build_meta"
31+
132
[tool.commitizen]
233
name = "cz_conventional_commits"
334
tag_format = "$version"

requirements.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
certifi==2024.12.14
2-
charset-normalizer==3.4.1
3-
idna==3.10
4-
prometheus-client==0.16.0
5-
requests==2.32.2
6-
typesense==0.21.0
7-
urllib3==2.3.0
1+
-e .

typesense_exporter.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,6 @@
44
each time Prometheus scrapes the /metrics endpoint. This approach
55
ensures fresh metrics on every scrape, without running a separate
66
polling loop.
7-
8-
Usage:
9-
1. Install dependencies:
10-
pip install prometheus_client requests typesense
11-
2. Run this script, e.g.:
12-
./typesense_exporter.py --port=8000
13-
3. Add a scrape config in Prometheus to target this exporter:
14-
scrape_configs:
15-
- job_name: "typesense_exporter"
16-
static_configs:
17-
- targets: ["localhost:8000"]
187
"""
198

209
import os

0 commit comments

Comments
 (0)