能否将自定义数值数据存入Prometheus?如何推送该类数据?
Absolutely! Prometheus fully supports storing custom numerical data — this is a core capability that lets you monitor everything from application-specific metrics to custom sensor readings. Prometheus works with numerical metric types like counters (for incrementing values), gauges (for arbitrary numeric values that go up/down), histograms, and summaries—all perfect for custom numerical data.
Here are the two most common ways to push custom numerical data to Prometheus:
1. Use the Prometheus Pushgateway (for short-lived jobs/scripts)
The Pushgateway is an official tool designed for scenarios where your workload is short-lived (like cron jobs, one-off scripts) and can't expose an endpoint for Prometheus to scrape. Here's how to use it:
Step 1: Install and start Pushgateway
You can run it directly from the binary:
./pushgateway
Or use Docker:
docker run -p 9091:9091 prom/pushgateway
Step 2: Push your custom data
You can use curl to send raw metric data, or use a client library (more on that later). For example, to push a gauge metric tracking device temperature:
echo "custom_device_temperature{device_id=\"sensor_001\", location=\"server_room\"} 24.7" | curl --data-binary @- http://localhost:9091/metrics/job/temperature_monitor
Breakdown:
custom_device_temperature: Your custom metric name{device_id="sensor_001", ...}: Labels to add context to your data24.7: The numerical value you want to storejob/temperature_monitor: A job label to group related metrics
Step 3: Configure Prometheus to scrape Pushgateway
Add this to your prometheus.yml config file:
scrape_configs: - job_name: 'pushgateway' static_configs: - targets: ['localhost:9091']
Restart Prometheus, and it will start pulling your custom data from the Pushgateway.
2. Use official client libraries (for long-running services or scripted pushes)
For long-running applications (like web services, daemons), the best practice is to expose a metrics endpoint for Prometheus to pull data (aligning with Prometheus's pull-based model). But you can also use these libraries to push data directly to Pushgateway if needed.
Here's a quick Python example using the prometheus_client library:
Step 1: Install the library
pip install prometheus-client
Step 2: Push custom data via code
from prometheus_client import Gauge, push_to_gateway, CollectorRegistry # Create a registry to hold your metrics registry = CollectorRegistry() # Define a gauge metric with labels custom_temp = Gauge( 'custom_device_temperature', 'Current temperature of IoT device', ['device_id', 'location'], registry=registry ) # Set your custom numerical value custom_temp.labels(device_id='sensor_001', location='server_room').set(24.7) # Push to Pushgateway push_to_gateway('localhost:9091', job='temperature_monitor', registry=registry)
If you want Prometheus to pull data instead, you can expose an HTTP endpoint with start_http_server(8000) and let Prometheus scrape http://your-service:8000/metrics.
Key Notes
- Use Pushgateway only for short-lived jobs: It holds metrics until they're scraped, but doesn't handle deduplication or expiration automatically.
- For long-running services, prefer the pull model (exposing an endpoint) — it's more aligned with Prometheus's design and easier to maintain.
内容的提问来源于stack exchange,提问作者Deepak N

