The API overview

HTTP API

All of Prometheus’s stable HTTP apis are in the/API /v1 path. When we have data query requirements, we can request monitoring data through the query API, and submit data by using remote Write protocol or Pushgateway.

Supported by the API

The authentication method

Authentication is enabled by default, so all interfaces need to be authenticated and all protocols support Bearer Token and Basic Auth.

Bearer Token

Bearer Tokens are generated as instances are generated and can be queried via the console. To learn more about Bearer tokens, see Bearer Authentication.

Basic Auth

Basic Auth is compatible with native Prometheus Query authentication with user names as user appids and passwords as bearer tokens (generated when instances are generated) that can be queried from the console. For more information about Basic Auth, see Basic Authentication.

Data return format

The response data format for all apis is JSON. A 2XX status code is returned for each successful request.

An invalid request returns a JSON-formatted data containing the error object, as well as a status code in the following table:

The response data format for all apis is JSON. A 2XX status code is returned for each successful request.

An invalid request returns a JSON-formatted data containing the error object, as well as a status code in the following table:

Ii Data Writing

The operation scenario is similar to that of Flink Jobs reporting data. We need to write data directly to Prometheus through the API because the job life cycle may be too short to wait for Prometheus to fetch data. Data can be written using Remote Write or Pushgateway.

Remote Write

POST /api/v1/prom/write
Copy the code

Remote Write is a protocol of the Prometheus standard. For details, see Prometheus- Remote Write. Remote Write allows other Prometheus data in a VPC to be written to the Prometheus monitoring service, which is a good way to improve data stability and migration.

Pushgateway and NodeExporter

In the architecture design of Prometheus, Prometheus Server does not directly monitor specific targets; its main tasks are data collection, storage, and external data query support. So in order to be able to monitor something like the CPU usage of the host, we need to use MY Exporter. Prometheus periodically pulls monitoring samples from HTTP service addresses (usually /metrics) exposed to its Exporter. It can be a standalone program running independently of the monitoring target, or it can be built directly into the monitoring target. As long as monitoring sample data can be provided to Prometheus in a standard format.

http://localhost:9091/metrics

The metric is empty because we’re routing to #, or we’re accessing the url directly

Go_gc_duration_seconds Summary of the garbage collection period pause duration go_gC_duration_seconds {quantile=”0″} 3.05E-05 Go_gc_duration_seconds {quantile = “0.25”} 4.18 e-05 go_gc_duration_seconds 4.65 e-05 {quantile = “0.5”} Go_gc_duration_seconds {quantile = “0.75”} 8.34 e-05 go_gc_duration_seconds {quantile = “1”} 0.0020116 Go_gc_duration_seconds_sum 0.0114824 go_gC_duration_SECONds_count 94 For example, 50% go GC duration seconds is less than or equal to 4.65E-05 Of a second

Pushgateway_http_push_duration_seconds Specifies the duration of the HTTP request pushed to Pushgateway. Pushgateway_http_push_duration_seconds {method = “put”, quantile = “0.1”} 0.0013008 Pushgateway_http_push_duration_seconds {method = “put”, quantile = “0.5”} 0.0027658 Pushgateway_http_push_duration_seconds {method = “put”, quantile = “0.9”} 0.0092754 0.013342 pushgateway_http_push_duration_seconds_sum {method = “put”} pushgateway_http_push_duration_seconds_count{method=”put”} 3

Pushgateway_http_push_size_bytes Specifies the size of the HTTP request pushed to Pushgateway Pushgateway_http_push_size_bytes {method = “put”, quantile = “0.1”} 407 Pushgateway_http_push_size_bytes {method = “put”, quantile = “0.5”} 408 Pushgateway_http_push_size_bytes {method = “put”, quantile = “0.9”} 408 pushgateway_http_push_size_bytes_sum {method = “put”} 1223 pushgateway_http_push_size_bytes_count{method=”put”} 3

Pushgateway_http_requests_total Total NUMBER of HTTP requests processed by Pushgateway, excluding crawlers. pushgateway_http_requests_total{code=”200″,handler=”push”,method=”put”} 3 pushgateway_http_requests_total{code=”200″,handler=”status”,method=”get”} 1


from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
from prometheus_client.exposition import basic_auth_handler

def my_auth_handler(url, method, timeout, headers, data) :
    username = 'admin'
    password = 'admin'
    return basic_auth_handler(url, method, timeout, headers, data, username, password)
registry = CollectorRegistry()
g = Gauge('job_last_success_unixtime_xuliang'.'Last time a batch job successfully finished', registry=registry)
g.set_to_current_time()
push_to_gateway('localhost:9091', job='batchA', registry=registry, handler=my_auth_handler)

Copy the code

Parameter caught

In addition to these, you may also see the following monitoring indicators on the current page, depending on the physical host system:

Node_boot_time: system startup time node_CPU: system CPU usage nodedisk* : disk IO nodefilesystem* : filesystem usage node_load1: system load nodememeory* : Memory usage nodenetwork* : network bandwidth node_time: current system time GO_ * : GO indicators in a node. Process_ * : Running indicators of a node's processesCopy the code