# PushGateway 数据上报采集

### 安装

{% code fullWidth="false" %}

```bash
docker run -d \
--restart always \
-p 9091:9091\
-v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime \
-v /tmp/prometheus-data:/prometheus-data/ \
--name prometheus_pushgateway \
prom/pushgateway:latest
```

{% endcode %}

### Prometheus配置

```yaml
scrape_configs:
  - job_name: 'pushgateway'
    honor_labels: true # 避免收集数据本身的 job 和 instance 被覆盖
    static_configs:
    - targets: ['10.11.19.141:9091']
      labels:
        instance: pushgateway
```

### PushGateway 数据上报采集

* API 方式 Push 数据到 PushGateway
* 默认 API URL 地址为： http\://:9091/metrics/job/{/\<LABEL\_NAME>/\<LABEL\_VALUE>}，
  * 其中 是必填项，为 job 标签值，后边可以跟任意数量的标签对，
  * 一般我们会添加一个 instance/\<INSTANCE\_NAME> 实例名称标签，来方便区分各个指标。

**简单指标数据**

```bash
echo "java_status{env=\"test_env\", name=\"test_demo\"} 1" | curl --data-binary @- http://10.11.19.141:9091/metrics/job/app/instance/app-8.8.8.8
```

<figure><img src="https://2134947750-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvILwbD2PrkBCkSitM3m4%2Fuploads%2FzKWUiaG84RHDH3chkuSR%2Fimage.png?alt=media&#x26;token=fc0f606f-9a59-4e09-8298-f5199bf745c5" alt=""><figcaption></figcaption></figure>

**少量指标数据**

> 每个指标添加 TYPE 及 HELP 说明。

<figure><img src="https://2134947750-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvILwbD2PrkBCkSitM3m4%2Fuploads%2FD0wlFT4EyIuNxpLjgo5E%2Fimage.png?alt=media&#x26;token=e931df78-2738-487b-9767-3c1adaa5e2fc" alt=""><figcaption></figcaption></figure>

**大量指标数据**

```bash
# vim pgdata.txt

# TYPE http_request_total counter
# HELP http_request_total get interface request count with different code.
http_request_total{env="test_env", name="test_demo", code="200",interface="/v1/save"} 276
http_request_total{env="test_env", name="test_demo", code="404",interface="/v1/delete"} 0
http_request_total{env="test_env", name="test_demo", code="500",interface="/v1/save"} 1

# TYPE http_request_time gauge
# HELP http_request_time get core interface http request time.
http_request_time{env="test_env",code="200",interface="/v1/core"} 0.122
```

```bash
curl -XPOST --data-binary @pgdata.txt http://10.11.19.141:9091/metrics/job/app/instance/app-8.8.8.8
```

<figure><img src="https://2134947750-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvILwbD2PrkBCkSitM3m4%2Fuploads%2FbNjfEwS1vjVs5CfpWy06%2Fimage.png?alt=media&#x26;token=220cdbe2-33d5-47c2-a919-baa06aedea53" alt=""><figcaption></figcaption></figure>

### 用 Client SDK Push 数据到 Pushgateway 示例

略

### 使用 PushGateway 注意事项

* 指标值只能是数字类型，非数字类型报错。
* 指标值支持最大长度为 16 位，超过16 位后默认置为 0

### PushGateway 数据持久化操作

> 默认 PushGateway 不做数据持久化操作，当 PushGateway 重启或者异常挂掉，导致数据的丢失，我们可以通过启动时添加 -persistence.file 和 -persistence.interval 参数来持久化数据。-persistence.file 表示本地持久化的文件，将 Push 的指标数据持久化保存到指定文件，-persistence.interval 表示本地持久化的指标数据保留时间，若设置为 5m，则表示 5 分钟后将删除存储的指标数据。

```bash
docker run -d -p 9091:9091 prom/pushgateway "-persistence.file=pg_file –persistence.interval=5m"
```

### PushGateway 推送及 Prometheus 拉取时间设置

> Prometheus 每次从 PushGateway 拉取的数据，并不是拉取周期内用户推送上来的所有数据，而是最后一次 Push 到 PushGateway 上的数据，所以推荐设置推送时间小于或等于 Prometheus 拉取的时间，这样保证每次拉取的数据是最新 Push 上来的。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/centos/prometheus/pushgateway-shu-ju-shang-bao-cai-ji.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
