prometheus-http-sd


Nameprometheus-http-sd JSON
Version 1.3.7 PyPI version JSON
download
home_pagehttps://python-poetry.org://github.com/laixintao/prometheus-http-sd
SummaryPrometheus HTTP SD framework.
upload_time2024-01-05 04:19:02
maintainer
docs_urlNone
authorlaixintao
requires_python>=3.7,<4.0
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # prometheus-http-sd

This is a
[Prometheus HTTP SD](https://prometheus.io/docs/prometheus/latest/http_sd/)
framework.

[![Test](https://github.com/laixintao/prometheus-http-sd/actions/workflows/test.yaml/badge.svg?branch=main)](https://github.com/laixintao/prometheus-http-sd/actions/workflows/test.yaml)

<!-- vim-markdown-toc GFM -->

- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
  - [The Python Target Generator](#the-python-target-generator)
  - [Python Target Generator Cache and Throttle](#python-target-generator-cache-and-throttle)
  - [Manage prometheus-http-sd by systemd](#manage-prometheus-http-sd-by-systemd)
  - [Admin Page](#admin-page)
  - [Serve under a different root path](#serve-under-a-different-root-path)
  - [Sentry APM](#sentry-apm)
- [Define your targets](#define-your-targets)
  - [Your target generator](#your-target-generator)
  - [The Target Path](#the-target-path)
  - [Overwriting `job_name` labels](#overwriting-job_name-labels)
  - [Check and Validate your Targets](#check-and-validate-your-targets)
  - [Script Dependencies](#script-dependencies)
- [Update Your Scripts](#update-your-scripts)
- [Best Practice](#best-practice)

<!-- vim-markdown-toc -->

## Features

- Support static targets from Json file;
- Support static targets from Yaml file;
- Support generating target list using Python script;
- Support `check` command, to testing the generated target is as expected, and
  counting the targets;
- You can monitoring your target generator via `/metrics`, see
  [metrics](./docs/metrics.txt);
- Admin page to list all target paths;
- Auto reload when generator or targets changed;
- Support managing targets in a hierarchy way;
- Throttle parallel execution and cache the result for Python script;
- Support Sentry APM.

## Installation

```shell
pip install prometheus-http-sd
```

## Usage

First, you need a directory, everything in this directory will be used to
generate targets for prometheus-http-sd.

```shell
$ mkdir targets
```

In this directory, every file is called a target "generator":

- Filename that ending with `.json` will be exposed directly
- Filename that ending with `.yaml` will be exposed directly
- Filename that ending with `.py` must include a `generate_targets()` function,
  the function will be run, and it must return a `TargetList` (Type helper in
  `prometheus_http_sd.targets.`)
- Filename that starts with `_` will be ignored, so you can have some python
  utils there, for e.g. `_utils/__init__.py` that you can import in you
  `generate_targets()`
- Filename that starts with `.` (hidden file in Linux) will also be ignored

Let write our first target generator by yaml, put this into your
`targets/first_target.yaml`:

```yaml
---
- targets:
    - "10.1.1.9:9100"
    - "10.1.1.10:9100"
  labels:
    job: node
    datacenter: nyc
    group: g1
- targets:
    - "10.2.1.9:9100"
    - "10.2.1.10:9100"
  labels:
    job: node
    datacenter: sg
    group: g2
```

If you use json, the data structure is the same, just in Json format.

### The Python Target Generator

Let's put another generator using Python:

Put this into your `targets/by_python.py`:

```python
def generate_targets(**extra_parameters):
  return [{"targets": ["10.1.1.22:2379"], "labels": {"app": "etcd"}}]
```

Then you can run `prometheus-http-sd serve -h 0.0.0.0 -p 8080 /tmp/targets`,
prometheus-http-sd will start to expose targets at: http://0.0.0.0:8080/targets

The `-h` and `-p` is optional, defaults to `127.0.0.1` and `8080`.

```shell
$ prometheus-http-sd serve /tmp/targets # replace this to your target path
[2022-07-24 00:52:03,896] {wasyncore.py:486} INFO - Serving on http://127.0.0.1:8080
```

If you run `curl http://127.0.0.1:8080/targets` you will get:

```shell
{"targets": "10.1.1.22:2379", "labels": {"app": "etcd"}}
```

Finally, you can tell your Prometheus to find targets under
http://127.0.0.1:8080/targets, by adding this into your Prometheus config:

```yaml
scrape_configs:
  - job_name: "etcd"
    http_sd_config:
      url: http://127.0.0.1:8080/targets/
```

The Python target generator also support URL query params. You can check the
params in your `generate_targets()` function.

For example:

```python
def generate_targets(**params):
  cluster = params.get("cluster")
  return [{"targets": ["10.1.1.22:2379"], "labels": {"app": "etcd", "cluster": cluster}}]
```

Then `curl http://127.0.0.1:8080/targets?cluster=us1` you will get:

```shell
{"targets": "10.1.1.22:2379", "labels": {"app": "etcd", "cluster": "us1"}}
```

### Python Target Generator Cache and Throttle

Support you have 10 Prometheus instance request http-sd for targets every
minutes, for Python script target generator, it doesn't make sense that the same
script run 10 times in every minute, instead, it should run only once, and use
this result to respond for 10 Prometheus instances.

prometheus-http-sd has cache and throttle by default, that means:

- At any time there is only one python script running
- The result will be cached for 1minute (This means that every script at max
  will be only running one time per minute, and your target update will delay at
  most 1 minute)

### Manage prometheus-http-sd by systemd

Just put this file under `/lib/systemd/system/http-sd.service` (remember to
change your installation path and root_dir path):

```
# /lib/systemd/system/http-sd.service
[Unit]
Description=Prometheus HTTP SD Service
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
ExecStart=/opt/httpsd_env/bin/prometheus-http-sd serve \
    -h 0.0.0.0                                         \
    -p 8080                                            \
    /opt/httpsd_targets

Restart=always
RestartSec=90

[Install]
WantedBy=multi-user.target
```

### Admin Page

You can open the root path, `http://127.0.0.1:8080/` in this example, and you
will see all of the available paths list in the admin page.

![](./docs/admin1.png)

### Serve under a different root path

If you put prometheus-http-sd behind a reverse proxy like Nginx, like this:

```
location /http_sd/ {
      proxy_pass http://prometheus_http_sd;
}
```

Then you need to tell prometheus_http_sd to serve all HTTP requests under this
path, by using the `--url_prefix /http_sd` cli option, (or `-r /http_sd` for
short).

### Change Certificate

By default, `prometheus-http-sd` has caching capabilities for Python targets to avoid server crashes due to too many queries.
```
    +------------+
    |            |
    |            |
    |            |                  +-----------+
    |  Caller 1  +----+             |           |
    |            |    |             |           |
    |            |    |             |           |
    |            |    |             |           |
    +------------+    |             |           |
                      |             |           |
                      |             |           |
    +------------+    |             |           |                  +----------+
    |            |    |             |           |                  |          |
    |            |    | call at the |           | only single call |          |
    |            |    |  same time  |  Timeout  |    to the back   |          |
    |  Caller 2  +----|------------>+   Cache   +----------------->+ Function |
    |            |    |             |           |                  |          |
    |            |    |             |           |                  |          |
    |            |    |             |           |                  |          |
    +------------+    |             |           |                  +----------+
                      |             |           |
                      |             |           |
    +------------+    |             |           |
    |            |    |             |           |
    |            |    |             |           |
    |            |    |             |           |
    |  Caller 3  +----+             |           |
    |            |                  +-----------+
    |            |
    |            |
    +------------+
```

To change this behavior, you can use the option `--cache-type` to change the cache behavior.

Also, you can use the option `--cache-opt` to change the variable.
For example:

```bash
prometheus-http-sd serve       \
    -h 0.0.0.0                 \
    -p 8080                    \
    --cache-type="Timeout"     \
    --cache-opt="timeout=360"  \
    /opt/httpsd_targets

```

#### Timeout

This is the default value, It will cache the result or exception from the target function.
* `timeout=<seconds>`:
  function timeout. if exceed, raise TimeoutException (in sec).
* `cache_time=<seconds>`:
  after function return normally, how long should we cache the result (in sec).
* `cache_exception_time=<seconds>`:
  after function return incorrectly, how long should we cache the exception (in sec).
* `name=<str>`:
  prometheus_client metrics prefix
* `garbage_collection_count=<seconds>`:
  garbage collection threshold
* `garbage_collection_interval=<seconds>`:
  the second to avoid collection too often.
* `copy_response=<bool>`:
  if true, use copy.deepcopy on the response from the target function.
    
    
#### None

This is a dummy function if you don't need any cache method.

### Sentry APM

You can use the option `--sentry-url <you-sentry-url>` (or `-s <your-sentry-url>`)
to enable Sentry APM.

The Exception from user's script will be sent to Sentry.

## Define your targets

### Your target generator

Please see the [Usage](#usage) to know how to define your generator.

### The Target Path

prometheus-http-sd support sub-pathes.

For example, if we use `prometheus-http-sd serve gateway`, and the `gateway`
directory's structure is as follows:

```shell
gateway
├── nginx
│   ├── edge.py
│   └── targets.json
└── targets.json
```

Then:

- `/targets/gateway` will return the targets from:
  - `gateway/nginx/edge.py`
  - `gateway/nginx/targets.json`
  - `gateway/targets.json`
- `/targets/gateway/nginx` will return the targets from:
  - `gateway/nginx/edge.py`
  - `gateway/nginx/targets.json`

This is very useful when you use vertical scaling. Say you have 5 Prometheus
instances, and you want each one of them scrape for different targets, then you
can use the sub-path feature of prometheus-http-sd.

For example, in one Prometheus's scrape config:

```yaml
scrape_configs:
  - job_name: "nginx"
    http_sd_config:
      url: http://prometheus-http-sd:8080/targets/nginx

  - job_name: "etcd"
    http_sd_config:
      url: http://prometheus-http-sd:8080/targets/etcd
```

And in another one:

```yaml
scrape_configs:
  - job_name: "nginx"
    http_sd_config:
      url: http://prometheus-http-sd:8080/targets/database

  - job_name: "etcd"
    http_sd_config:
      url: http://prometheus-http-sd:8080/targets/application
```

### Overwriting `job_name` labels

You may want to put all of etcd targets in one generator, including port 2379
for etcd metrics and 9100 for node_exporter metrics of the etcd server. But the
`job_name` setting was based on per URL.

The trick is that, you can overwrite the `job` label in the target labels, like
this:

```yaml
---
- targets:
    - "10.1.1.9:9100"
  labels:
    job: node
    datacenter: nyc
    group: g1
- targets:
    - "10.1.1.9:2379"
  labels:
    job: etcd
    datacenter: nyc
    group: g1
```

### Check and Validate your Targets

You can use `prometheus-http-sd check` command to test your targets dir. It will
run all of you generators, validate the targets, and print the targets count
that each generator generates.

```shell
$ prometheus-http-sd check test/test_generator/root
[2022-08-06 00:50:11,095] {validate.py:16} INFO - Run generator test/test_generator/root/json/target.json, took 0.0011398792266845703s, generated 1 targets.
[2022-08-06 00:50:11,100] {validate.py:16} INFO - Run generator test/test_generator/root/yaml/target.yaml, took 0.0043718814849853516s, generated 2 targets.
[2022-08-06 00:50:11,100] {validate.py:22} INFO - Done! Generated {total_targets} in total.
```

It's a good idea to use `prometheus-http-sd check` in your CI system to validate
your targets generator scripts and target files.

For Python script, `prometheus-http-sd check` command will run
`generate_targets` in each script, without any params. However, you can
overwrite the `check` logic by providing a function called
`test_generate_targets()`(without any function args), then `check` will run
`test_generate_targets` instead. (So you can call `generate_targets(foo="bar")`
to set the test logic of your own.

### Script Dependencies

If you want your scripts to use some other python library, just install them
into the **same virtualenv** that you install prometheus-http-sd, so that
prometheus-http-sd can import them.

## Update Your Scripts

If you want to update your script file or target json file, just upload and
overwrite with your new version, it will take effect immediately after you
making changes, **there is no need to restart** prometheus-http-sd,
prometheus-http-sd will read the file (or reload the python script) every time
serving a request.

It is worth noting that restarting is safe because if Prometheus failed to get
the target list via HTTP request, it won't update its current target list to
empty, instead,
[it will keep using the current list](https://prometheus.io/docs/prometheus/latest/http_sd/).

> Prometheus caches target lists. If an error occurs while fetching an updated
> targets list, Prometheus keeps using the current targets list.

For the same reason, if there are 3 scripts under `/targets/mysystem` and only
one failed for a request, prometheus-http-sd will return a HTTP 500 Error for
the whole request instead of returning the partial targets from the other two
scripts.

Also for the same reason, if your script met any error, you should throw out
`Exception` all the way to the top instead of catch it in your script and return
a null `TargetList`, if you return a null `TargetList`, prometheus-http-sd will
think that your script run successfully and empty the target list as well.

You can notice this error from stdout logs or `/metrics` from
prometheus-http-sd.

## Best Practice

You can use a git repository to manage your target generator.

            

Raw data

            {
    "_id": null,
    "home_page": "https://python-poetry.org://github.com/laixintao/prometheus-http-sd",
    "name": "prometheus-http-sd",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "laixintao",
    "author_email": "laixintaoo@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/1e/0f/d11077a903a2ef1decef357ddf27502344f4d1fbd659800d5124b967bb9e/prometheus_http_sd-1.3.7.tar.gz",
    "platform": null,
    "description": "# prometheus-http-sd\n\nThis is a\n[Prometheus HTTP SD](https://prometheus.io/docs/prometheus/latest/http_sd/)\nframework.\n\n[![Test](https://github.com/laixintao/prometheus-http-sd/actions/workflows/test.yaml/badge.svg?branch=main)](https://github.com/laixintao/prometheus-http-sd/actions/workflows/test.yaml)\n\n<!-- vim-markdown-toc GFM -->\n\n- [Features](#features)\n- [Installation](#installation)\n- [Usage](#usage)\n  - [The Python Target Generator](#the-python-target-generator)\n  - [Python Target Generator Cache and Throttle](#python-target-generator-cache-and-throttle)\n  - [Manage prometheus-http-sd by systemd](#manage-prometheus-http-sd-by-systemd)\n  - [Admin Page](#admin-page)\n  - [Serve under a different root path](#serve-under-a-different-root-path)\n  - [Sentry APM](#sentry-apm)\n- [Define your targets](#define-your-targets)\n  - [Your target generator](#your-target-generator)\n  - [The Target Path](#the-target-path)\n  - [Overwriting `job_name` labels](#overwriting-job_name-labels)\n  - [Check and Validate your Targets](#check-and-validate-your-targets)\n  - [Script Dependencies](#script-dependencies)\n- [Update Your Scripts](#update-your-scripts)\n- [Best Practice](#best-practice)\n\n<!-- vim-markdown-toc -->\n\n## Features\n\n- Support static targets from Json file;\n- Support static targets from Yaml file;\n- Support generating target list using Python script;\n- Support `check` command, to testing the generated target is as expected, and\n  counting the targets;\n- You can monitoring your target generator via `/metrics`, see\n  [metrics](./docs/metrics.txt);\n- Admin page to list all target paths;\n- Auto reload when generator or targets changed;\n- Support managing targets in a hierarchy way;\n- Throttle parallel execution and cache the result for Python script;\n- Support Sentry APM.\n\n## Installation\n\n```shell\npip install prometheus-http-sd\n```\n\n## Usage\n\nFirst, you need a directory, everything in this directory will be used to\ngenerate targets for prometheus-http-sd.\n\n```shell\n$ mkdir targets\n```\n\nIn this directory, every file is called a target \"generator\":\n\n- Filename that ending with `.json` will be exposed directly\n- Filename that ending with `.yaml` will be exposed directly\n- Filename that ending with `.py` must include a `generate_targets()` function,\n  the function will be run, and it must return a `TargetList` (Type helper in\n  `prometheus_http_sd.targets.`)\n- Filename that starts with `_` will be ignored, so you can have some python\n  utils there, for e.g. `_utils/__init__.py` that you can import in you\n  `generate_targets()`\n- Filename that starts with `.` (hidden file in Linux) will also be ignored\n\nLet write our first target generator by yaml, put this into your\n`targets/first_target.yaml`:\n\n```yaml\n---\n- targets:\n    - \"10.1.1.9:9100\"\n    - \"10.1.1.10:9100\"\n  labels:\n    job: node\n    datacenter: nyc\n    group: g1\n- targets:\n    - \"10.2.1.9:9100\"\n    - \"10.2.1.10:9100\"\n  labels:\n    job: node\n    datacenter: sg\n    group: g2\n```\n\nIf you use json, the data structure is the same, just in Json format.\n\n### The Python Target Generator\n\nLet's put another generator using Python:\n\nPut this into your `targets/by_python.py`:\n\n```python\ndef generate_targets(**extra_parameters):\n  return [{\"targets\": [\"10.1.1.22:2379\"], \"labels\": {\"app\": \"etcd\"}}]\n```\n\nThen you can run `prometheus-http-sd serve -h 0.0.0.0 -p 8080 /tmp/targets`,\nprometheus-http-sd will start to expose targets at: http://0.0.0.0:8080/targets\n\nThe `-h` and `-p` is optional, defaults to `127.0.0.1` and `8080`.\n\n```shell\n$ prometheus-http-sd serve /tmp/targets # replace this to your target path\n[2022-07-24 00:52:03,896] {wasyncore.py:486} INFO - Serving on http://127.0.0.1:8080\n```\n\nIf you run `curl http://127.0.0.1:8080/targets` you will get:\n\n```shell\n{\"targets\": \"10.1.1.22:2379\", \"labels\": {\"app\": \"etcd\"}}\n```\n\nFinally, you can tell your Prometheus to find targets under\nhttp://127.0.0.1:8080/targets, by adding this into your Prometheus config:\n\n```yaml\nscrape_configs:\n  - job_name: \"etcd\"\n    http_sd_config:\n      url: http://127.0.0.1:8080/targets/\n```\n\nThe Python target generator also support URL query params. You can check the\nparams in your `generate_targets()` function.\n\nFor example:\n\n```python\ndef generate_targets(**params):\n  cluster = params.get(\"cluster\")\n  return [{\"targets\": [\"10.1.1.22:2379\"], \"labels\": {\"app\": \"etcd\", \"cluster\": cluster}}]\n```\n\nThen `curl http://127.0.0.1:8080/targets?cluster=us1` you will get:\n\n```shell\n{\"targets\": \"10.1.1.22:2379\", \"labels\": {\"app\": \"etcd\", \"cluster\": \"us1\"}}\n```\n\n### Python Target Generator Cache and Throttle\n\nSupport you have 10 Prometheus instance request http-sd for targets every\nminutes, for Python script target generator, it doesn't make sense that the same\nscript run 10 times in every minute, instead, it should run only once, and use\nthis result to respond for 10 Prometheus instances.\n\nprometheus-http-sd has cache and throttle by default, that means:\n\n- At any time there is only one python script running\n- The result will be cached for 1minute (This means that every script at max\n  will be only running one time per minute, and your target update will delay at\n  most 1 minute)\n\n### Manage prometheus-http-sd by systemd\n\nJust put this file under `/lib/systemd/system/http-sd.service` (remember to\nchange your installation path and root_dir path):\n\n```\n# /lib/systemd/system/http-sd.service\n[Unit]\nDescription=Prometheus HTTP SD Service\nWants=network-online.target\nAfter=network-online.target\n\n[Service]\nType=simple\nExecStart=/opt/httpsd_env/bin/prometheus-http-sd serve \\\n    -h 0.0.0.0                                         \\\n    -p 8080                                            \\\n    /opt/httpsd_targets\n\nRestart=always\nRestartSec=90\n\n[Install]\nWantedBy=multi-user.target\n```\n\n### Admin Page\n\nYou can open the root path, `http://127.0.0.1:8080/` in this example, and you\nwill see all of the available paths list in the admin page.\n\n![](./docs/admin1.png)\n\n### Serve under a different root path\n\nIf you put prometheus-http-sd behind a reverse proxy like Nginx, like this:\n\n```\nlocation /http_sd/ {\n      proxy_pass http://prometheus_http_sd;\n}\n```\n\nThen you need to tell prometheus_http_sd to serve all HTTP requests under this\npath, by using the `--url_prefix /http_sd` cli option, (or `-r /http_sd` for\nshort).\n\n### Change Certificate\n\nBy default, `prometheus-http-sd` has caching capabilities for Python targets to avoid server crashes due to too many queries.\n```\n    +------------+\n    |            |\n    |            |\n    |            |                  +-----------+\n    |  Caller 1  +----+             |           |\n    |            |    |             |           |\n    |            |    |             |           |\n    |            |    |             |           |\n    +------------+    |             |           |\n                      |             |           |\n                      |             |           |\n    +------------+    |             |           |                  +----------+\n    |            |    |             |           |                  |          |\n    |            |    | call at the |           | only single call |          |\n    |            |    |  same time  |  Timeout  |    to the back   |          |\n    |  Caller 2  +----|------------>+   Cache   +----------------->+ Function |\n    |            |    |             |           |                  |          |\n    |            |    |             |           |                  |          |\n    |            |    |             |           |                  |          |\n    +------------+    |             |           |                  +----------+\n                      |             |           |\n                      |             |           |\n    +------------+    |             |           |\n    |            |    |             |           |\n    |            |    |             |           |\n    |            |    |             |           |\n    |  Caller 3  +----+             |           |\n    |            |                  +-----------+\n    |            |\n    |            |\n    +------------+\n```\n\nTo change this behavior, you can use the option `--cache-type` to change the cache behavior.\n\nAlso, you can use the option `--cache-opt` to change the variable.\nFor example:\n\n```bash\nprometheus-http-sd serve       \\\n    -h 0.0.0.0                 \\\n    -p 8080                    \\\n    --cache-type=\"Timeout\"     \\\n    --cache-opt=\"timeout=360\"  \\\n    /opt/httpsd_targets\n\n```\n\n#### Timeout\n\nThis is the default value, It will cache the result or exception from the target function.\n* `timeout=<seconds>`:\n  function timeout. if exceed, raise TimeoutException (in sec).\n* `cache_time=<seconds>`:\n  after function return normally, how long should we cache the result (in sec).\n* `cache_exception_time=<seconds>`:\n  after function return incorrectly, how long should we cache the exception (in sec).\n* `name=<str>`:\n  prometheus_client metrics prefix\n* `garbage_collection_count=<seconds>`:\n  garbage collection threshold\n* `garbage_collection_interval=<seconds>`:\n  the second to avoid collection too often.\n* `copy_response=<bool>`:\n  if true, use copy.deepcopy on the response from the target function.\n    \n    \n#### None\n\nThis is a dummy function if you don't need any cache method.\n\n### Sentry APM\n\nYou can use the option `--sentry-url <you-sentry-url>` (or `-s <your-sentry-url>`)\nto enable Sentry APM.\n\nThe Exception from user's script will be sent to Sentry.\n\n## Define your targets\n\n### Your target generator\n\nPlease see the [Usage](#usage) to know how to define your generator.\n\n### The Target Path\n\nprometheus-http-sd support sub-pathes.\n\nFor example, if we use `prometheus-http-sd serve gateway`, and the `gateway`\ndirectory's structure is as follows:\n\n```shell\ngateway\n\u251c\u2500\u2500 nginx\n\u2502\u00a0\u00a0 \u251c\u2500\u2500 edge.py\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 targets.json\n\u2514\u2500\u2500 targets.json\n```\n\nThen:\n\n- `/targets/gateway` will return the targets from:\n  - `gateway/nginx/edge.py`\n  - `gateway/nginx/targets.json`\n  - `gateway/targets.json`\n- `/targets/gateway/nginx` will return the targets from:\n  - `gateway/nginx/edge.py`\n  - `gateway/nginx/targets.json`\n\nThis is very useful when you use vertical scaling. Say you have 5 Prometheus\ninstances, and you want each one of them scrape for different targets, then you\ncan use the sub-path feature of prometheus-http-sd.\n\nFor example, in one Prometheus's scrape config:\n\n```yaml\nscrape_configs:\n  - job_name: \"nginx\"\n    http_sd_config:\n      url: http://prometheus-http-sd:8080/targets/nginx\n\n  - job_name: \"etcd\"\n    http_sd_config:\n      url: http://prometheus-http-sd:8080/targets/etcd\n```\n\nAnd in another one:\n\n```yaml\nscrape_configs:\n  - job_name: \"nginx\"\n    http_sd_config:\n      url: http://prometheus-http-sd:8080/targets/database\n\n  - job_name: \"etcd\"\n    http_sd_config:\n      url: http://prometheus-http-sd:8080/targets/application\n```\n\n### Overwriting `job_name` labels\n\nYou may want to put all of etcd targets in one generator, including port 2379\nfor etcd metrics and 9100 for node_exporter metrics of the etcd server. But the\n`job_name` setting was based on per URL.\n\nThe trick is that, you can overwrite the `job` label in the target labels, like\nthis:\n\n```yaml\n---\n- targets:\n    - \"10.1.1.9:9100\"\n  labels:\n    job: node\n    datacenter: nyc\n    group: g1\n- targets:\n    - \"10.1.1.9:2379\"\n  labels:\n    job: etcd\n    datacenter: nyc\n    group: g1\n```\n\n### Check and Validate your Targets\n\nYou can use `prometheus-http-sd check` command to test your targets dir. It will\nrun all of you generators, validate the targets, and print the targets count\nthat each generator generates.\n\n```shell\n$ prometheus-http-sd check test/test_generator/root\n[2022-08-06 00:50:11,095] {validate.py:16} INFO - Run generator test/test_generator/root/json/target.json, took 0.0011398792266845703s, generated 1 targets.\n[2022-08-06 00:50:11,100] {validate.py:16} INFO - Run generator test/test_generator/root/yaml/target.yaml, took 0.0043718814849853516s, generated 2 targets.\n[2022-08-06 00:50:11,100] {validate.py:22} INFO - Done! Generated {total_targets} in total.\n```\n\nIt's a good idea to use `prometheus-http-sd check` in your CI system to validate\nyour targets generator scripts and target files.\n\nFor Python script, `prometheus-http-sd check` command will run\n`generate_targets` in each script, without any params. However, you can\noverwrite the `check` logic by providing a function called\n`test_generate_targets()`(without any function args), then `check` will run\n`test_generate_targets` instead. (So you can call `generate_targets(foo=\"bar\")`\nto set the test logic of your own.\n\n### Script Dependencies\n\nIf you want your scripts to use some other python library, just install them\ninto the **same virtualenv** that you install prometheus-http-sd, so that\nprometheus-http-sd can import them.\n\n## Update Your Scripts\n\nIf you want to update your script file or target json file, just upload and\noverwrite with your new version, it will take effect immediately after you\nmaking changes, **there is no need to restart** prometheus-http-sd,\nprometheus-http-sd will read the file (or reload the python script) every time\nserving a request.\n\nIt is worth noting that restarting is safe because if Prometheus failed to get\nthe target list via HTTP request, it won't update its current target list to\nempty, instead,\n[it will keep using the current list](https://prometheus.io/docs/prometheus/latest/http_sd/).\n\n> Prometheus caches target lists. If an error occurs while fetching an updated\n> targets list, Prometheus keeps using the current targets list.\n\nFor the same reason, if there are 3 scripts under `/targets/mysystem` and only\none failed for a request, prometheus-http-sd will return a HTTP 500 Error for\nthe whole request instead of returning the partial targets from the other two\nscripts.\n\nAlso for the same reason, if your script met any error, you should throw out\n`Exception` all the way to the top instead of catch it in your script and return\na null `TargetList`, if you return a null `TargetList`, prometheus-http-sd will\nthink that your script run successfully and empty the target list as well.\n\nYou can notice this error from stdout logs or `/metrics` from\nprometheus-http-sd.\n\n## Best Practice\n\nYou can use a git repository to manage your target generator.\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Prometheus HTTP SD framework.",
    "version": "1.3.7",
    "project_urls": {
        "Homepage": "https://python-poetry.org://github.com/laixintao/prometheus-http-sd"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a5da477e3a6fb05ea9df0cad01fecd7ccb0ba29e5d6ed60ba3c046d2a6938aa4",
                "md5": "0c4ce430240d0e0853a468a2e2c8dcd4",
                "sha256": "132f83b089fae5c7cc15ffa7ad2155626762e9c530b67ce462c2ec230b9f617b"
            },
            "downloads": -1,
            "filename": "prometheus_http_sd-1.3.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0c4ce430240d0e0853a468a2e2c8dcd4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7,<4.0",
            "size": 21223,
            "upload_time": "2024-01-05T04:19:00",
            "upload_time_iso_8601": "2024-01-05T04:19:00.683459Z",
            "url": "https://files.pythonhosted.org/packages/a5/da/477e3a6fb05ea9df0cad01fecd7ccb0ba29e5d6ed60ba3c046d2a6938aa4/prometheus_http_sd-1.3.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1e0fd11077a903a2ef1decef357ddf27502344f4d1fbd659800d5124b967bb9e",
                "md5": "1c9c084f37c701b29c75962f2ad35c2c",
                "sha256": "4a1b5c8369b6a8b17ba9f7f16a4b49430c7434a94f02cbec120e817db12fffd0"
            },
            "downloads": -1,
            "filename": "prometheus_http_sd-1.3.7.tar.gz",
            "has_sig": false,
            "md5_digest": "1c9c084f37c701b29c75962f2ad35c2c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7,<4.0",
            "size": 21650,
            "upload_time": "2024-01-05T04:19:02",
            "upload_time_iso_8601": "2024-01-05T04:19:02.413056Z",
            "url": "https://files.pythonhosted.org/packages/1e/0f/d11077a903a2ef1decef357ddf27502344f4d1fbd659800d5124b967bb9e/prometheus_http_sd-1.3.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-05 04:19:02",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "prometheus-http-sd"
}
        
Elapsed time: 0.15473s