Skip to content

Instantly share code, notes, and snippets.

@eeddaann
Last active March 2, 2021 14:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save eeddaann/7e35141782beb9b948eaf44aebd993eb to your computer and use it in GitHub Desktop.
Save eeddaann/7e35141782beb9b948eaf44aebd993eb to your computer and use it in GitHub Desktop.
query in PromQL to get docker swarm node names instead of node id

PromQL

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

example:

  • 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment