Integrate Real‑Time Prometheus Pod Metrics into Probius Using ECharts
After integrating Kubernetes into Probius, this guide shows how to pull pod metrics from Prometheus using the query_range API, process them with a Python client, and visualize CPU, memory, bandwidth, and IOPS data in Probius via ECharts, completing a seamless container‑monitoring feature.
In a previous article we integrated Kubernetes into the Probius task system. After deployment, users still needed to view container metrics in Grafana. This tutorial explains how to embed Prometheus‑collected pod monitoring directly into Probius to streamline the workflow.
Getting Data
Prometheus provides two APIs:
queryfor a single point in time and
query_rangefor a time series. For the monitoring feature we need
query_range.
Endpoint:
<code>GET /api/v1/query_range</code>Supported parameters:
query : Prometheus expression string
start : Start time (RFC3339 or Unix timestamp)
end : End time (RFC3339 or Unix timestamp)
step : Interval, e.g.,
15stimeout : Request timeout
Python client example to fetch POD metrics:
<code>class PrometheusApi:
def __init__(self, range=1):
self.domain = "https://prometheus.ops-coffee.cn"
self.nowtime = datetime.datetime.now(datetime.timezone.utc)
self.start_time = self.nowtime - datetime.timedelta(hours=int(range))
def get_pod_metric(self, namespace, pod):
_data = {"datetime": [], "cpu": {}, "memory": {}, "bandwidth": {}, "iops": {}}
params = {
"query": "sum(node_namespace_pod_container:container_cpu_usage_seconds_total:sum_irate{namespace='%s', pod='%s'}) by (container)" % (namespace, pod),
"start": self.start_time.isoformat(),
"end": self.nowtime.isoformat(),
"step": "60s"
}
rc = requests.get(self.domain + '/api/v1/query_range', params=params)
for c in rc.json().get('data').get('result'):
_data['cpu'][c['metric']['container']] = [j[1] for j in c['values']]
return True, 200, _data
</code>If you are unfamiliar with Prometheus expressions, you can copy them from Grafana charts. Note that Prometheus stores timestamps in UTC, so conversion to local time may be required.
Chart Display
After obtaining the data, we use ECharts to render line charts. The following HTML layout hosts the selectors and chart containers:
<code><div class="tab-pane" id="tab-monitor">
<div class="row">
<div class="col-md-4">
<div class="input-group">
<div class="input-group-btn"><button class="btn btn-default">选择实例</button></div>
<select class="form-control" id="id_pod_monitor" onchange="getPodMonitor()"><option value="">------</option></select>
</div>
</div>
<div class="col-md-2">
<div class="input-group">
<div class="input-group-btn"><button class="btn btn-default">时间范围</button></div>
<select class="form-control" id="id_pod_monitor_time" onchange="getPodMonitor()">
<option value="1" selected="selected">最近1小时</option>
<option value="4">最近4小时</option>
<option value="24">最近24小时</option>
<option value="48">最近48小时</option>
<option value="72">最近72小时</option>
</select>
</div>
</div>
<div class="col-md-6">
<div class="pull-right">
<button class="btn btn-default" onclick="getPodMonitor()"><span class="glyphicon glyphicon-refresh"></span> 刷新</button>
</div>
</div>
</div>
<div class="row" style="margin-top:10px;">
<div class="col-md-6" id="id_chart_cpu" style="position: relative; height: 300px;"></div>
<div class="col-md-6" id="id_chart_memory" style="position: relative; height: 300px;"></div>
<div class="col-md-6" id="id_chart_bandwidth" style="position: relative; height: 300px;"></div>
<div class="col-md-6" id="id_chart_iops" style="position: relative; height: 300px;"></div>
</div>
</div>
</code>JavaScript initializes the charts and fetches data from the backend:
<code>var chart_cpu = echarts.init(document.getElementById('id_chart_cpu')),
chart_memory = echarts.init(document.getElementById('id_chart_memory')),
chart_bandwidth = echarts.init(document.getElementById('id_chart_bandwidth')),
chart_iops = echarts.init(document.getElementById('id_chart_iops'));
function getPodMonitor() {
var pod = $('#id_pod_monitor').val(), range = $('#id_pod_monitor_time').val();
$.get('/kubeops/namespace/{{ namespace }}/pod/' + pod + '/monitor/?range=' + range + '&format=json', function(data) {
if (data.state) {
let cpu_legend = [], cpu_series = [];
$.each(data.data.cpu, function(k, v) {
cpu_legend.push(k);
cpu_series.push({data: v, name: k, type: 'line'});
});
chart_cpu.setOption({
title: {text: 'CPU使用(单位:core)'},
tooltip: {trigger: 'axis'},
legend: {data: cpu_legend},
xAxis: {data: data.data.datetime},
yAxis: {type: 'value'},
series: cpu_series
});
}
});
}
</code>ECharts is used with simple line charts; no extra decorations are added. With this implementation, Probius now provides built‑in POD monitoring, turning a small but useful feature into a fully integrated part of the system.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.