如何直接向Cortex推送指标?是否有Python客户端支持该操作?
Absolutely! You can skip Prometheus entirely and send metrics straight to Cortex—here’s how to do it, including Python tools to streamline the process:
1. Use Cortex's Built-in Remote Write API
Cortex is designed to work with Prometheus’s Remote Write API out of the box, which is exactly what you need for direct metric ingestion. The core endpoint to target is:http://<your-cortex-instance>/api/v1/push
Quick Test with curl
To verify this works, you can send a sample metric via curl (note: the payload needs to be in Prometheus’s Protobuf format):
# First, use promtool to convert a text metric to Protobuf (you'll need promtool installed) echo "test_counter_total{env=\"dev\"} 123" | promtool remote-write encode > metric.pb # Then send it to Cortex curl -X POST http://your-cortex-url/api/v1/push \ -H "Content-Type: application/x-protobuf" \ -H "X-Prometheus-Remote-Write-Version: 0.1.0" \ --data-binary @metric.pb
2. Python Implementation Options
You’ve got a couple solid choices for Python clients—here are the most straightforward ones:
Option 1: Official prometheus-client + requests
The official Prometheus Python library lets you create metrics easily, and you can pair it with requests to send them to Cortex. Here’s a working example:
import requests from prometheus_client import Counter, generate_latest from prometheus_client.parser import text_string_to_metric_families import prometheus_client.remote_write as remote_write # Create a sample counter metric test_counter = Counter('api_requests_total', 'Total API requests', ['endpoint']) test_counter.labels(endpoint='/checkout').inc() # Convert the metric to Protobuf (required for Remote Write) metric_families = list(text_string_to_metric_families(generate_latest().decode('utf-8'))) write_request = remote_write.build_write_request(metric_families) # Send to Cortex cortex_url = "http://your-cortex-url/api/v1/push" headers = { "Content-Type": "application/x-protobuf", "X-Prometheus-Remote-Write-Version": "0.1.0" # Add auth headers here if needed, e.g., "Authorization": "Bearer YOUR_TOKEN" } response = requests.post(cortex_url, data=write_request.SerializeToString(), headers=headers) response.raise_for_status() # Raises an error if the push fails print("Metrics pushed successfully to Cortex!")
Option 2: Third-Party prometheus-remote-write Library
If you want a more streamlined approach, the prometheus-remote-write library handles the Protobuf encoding and API calls for you. First install it:
pip install prometheus-remote-write
Then use it in your code:
from prometheus_remote_write import Client, Metric # Initialize the Cortex client client = Client("http://your-cortex-url/api/v1/push") # Create and send a gauge metric gauge_metric = Metric( name="system_cpu_usage", labels={"host": "web-server-01"}, value=78.2, type="gauge" ) client.send(gauge_metric) # You can also send multiple metrics at once another_metric = Metric( name="disk_free_gb", labels={"host": "web-server-01"}, value=45.6, type="gauge" ) client.send([gauge_metric, another_metric])
3. Key Things to Keep in Mind
- Authentication: If your Cortex deployment uses API keys, OAuth, or other auth methods, add the corresponding headers to your requests (e.g.,
Authorization: Bearer <your-token>). - Metric Format: Stick to Prometheus naming rules (alphanumeric characters and underscores only) to avoid ingestion errors.
- Rate Limits: Check your Cortex config for rate limits—don’t flood the endpoint with too many metrics at once.
内容的提问来源于stack exchange,提问作者neastlia

