[英]Prometheus create label from metric label
We are running the node-exporter in containers.我们在容器中运行节点导出器。 To quickly identify on which host each node-exporter is running, I created a metric that looks like this:
host{host="$HOSTNAME",node="$CONTAINER_ID"} 1
为了快速识别每个节点导出器在哪个主机上运行,我创建了一个如下所示的指标:
host{host="$HOSTNAME",node="$CONTAINER_ID"} 1
I'm looking for a way to extract the hostname in host=
and create a label for each node-exporter instance as a hostname label. I tried numerous configurations and none seem to work.我正在寻找一种方法来提取
host=
中的主机名,并为每个节点导出器实例创建一个 label 作为主机名 label。我尝试了多种配置,但似乎都没有用。 Current prometheus config looks like this:当前的普罗米修斯配置如下所示:
scrape_configs:
- job_name: 'node'
scrape_interval: 10s
scrape_timeout: 5s
metrics_path: /metrics
scheme: http
dns_sd_configs:
- names:
- tasks.master-nodeexporter
refresh_interval: 30s
type: A
port: 9100
relabel_configs:
- source_labels: ['host']
regex: '"(.*)".*'
target_label: 'hostname'
replacement: '$1'
This is not possible, as target relabelling happens before the scrape.这是不可能的,因为目标重新标记发生在刮擦之前。
What you want to do here is use service discovery to have the right hostname in the first place, which is not possible with dns_sd_configs
.您在这里要做的是首先使用服务发现来获得正确的主机名,而这在
dns_sd_configs
是不可能的。 You might look at something like Consul andhttps://www.robustperception.io/controlling-the-instance-label/您可能会查看 Consul 和https://www.robustperception.io/controlling-the-instance-label/ 之类的内容
If someone comes across this:如果有人遇到这个:
Create this as docker-entrypoint.sh and make it executable.将其创建为 docker-entrypoint.sh 并使其可执行。
#!/bin/sh -e
# Must be executable by others
NODE_NAME=$(cat /etc/nodename) echo "node_meta{node_id=\"$NODE_ID\", container_label_com_docker_swarm_node_id=\"$NODE_ID\", node_name=\"$NODE_NAME\"} 1" > /etc/node-exporter/node-meta.prom
set -- /bin/node_exporter "$@"
exec "$@"
Than create a Dockerfile like this比创建这样的 Dockerfile
FROM prom/node-exporter:latest
ENV NODE_ID=none
USER root
COPY conf /etc/node-exporter/
ENTRYPOINT [ "/etc/node-exporter/docker-entrypoint.sh" ]
CMD [ "/bin/node_exporter" ]
Than build it and you will always get the Hostname as a node_meta metric比构建它,您将始终获得主机名作为 node_meta 指标
This answer explains how to export node name via node_name
label at node_meta
metric.这个答案解释了如何通过
node_name
label 在node_meta
度量中导出节点名称。 Then it is possible to add node_name
label to any metric exposed by node_exporter
with group_left()
modifier during querying.然后可以在查询期间使用
group_left()
修饰符将node_name
label 添加到node_exporter
公开的任何指标。 For example, the following PromQL query adds node_name
label from node_meta
metric to node_memory_Active_bytes
metric:例如,以下 PromQL 查询将
node_name
label 从node_meta
指标添加到node_memory_Active_bytes
指标:
node_memory_Active_bytes
* on(job,instance) group_left(node_name)
node_meta
See more details about group_left()
modifier in these docs and this article .在这些文档和本文中查看有关
group_left()
修饰符的更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.