Prometheus is a time-series db, it's query language called PromQL.
Here are some analogies from SQL which may help to understand the basics of PromQL better:
- metric ~ sql table
- label ~ sql column
-
Count how many containers running on each node:
count(container_last_seen) BY (container_label_com_docker_swarm_node_id)
the equivalent SQL would be:
SELECT container_label_com_docker_swarm_node_id, COUNT(container_label_com_docker_swarm_node_id) FROM container_last_seen GROUP BY container_label_com_docker_swarm_node_id
the result of this query will be table where each row is a unique container_label_com_docker_swarm_node_id and the value is the number of containers which run on this swarm node.
To get the corresponding node name as a label, we can use the following query:
count(container_last_seen) BY (container_label_com_docker_swarm_node_id) * ON (container_label_com_docker_swarm_node_id) GROUP_LEFT(node_name) node_meta
the equivalent SQL would be:
SELECT container_label_com_docker_swarm_node_id, node_name, COUNT(container_label_com_docker_swarm_node_id) FROM container_last_seen LEFT JOIN node_meta ON container_label_com_docker_swarm_node_id GROUP BY container_label_com_docker_swarm_node_id
let's break this down:
- ON - join on specific label
- GROUP_LEFT ~ SQL's LEFT JOIN
- node_meta - metric which has both container_label_com_docker_swarm_node_id and node_name labels