From f890ad424618e3e9286e79fafb7d1aa34857ac03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Thu, 5 Dec 2024 16:32:50 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20prometheus=20sd?= =?UTF-8?q?=E5=8F=91=E7=8E=B0=20=E5=85=BC=E5=AE=B9=E6=90=BA=E5=B8=A6contex?= =?UTF-8?q?t-path=E7=9A=84=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/prometheus/prometheus.yml | 5 +++++ .../modules/monitor/controller/PrometheusController.java | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/docker/prometheus/prometheus.yml b/docker/prometheus/prometheus.yml index a336e5f66..f42001fbf 100644 --- a/docker/prometheus/prometheus.yml +++ b/docker/prometheus/prometheus.yml @@ -52,3 +52,8 @@ scrape_configs: basic_auth: username: ruoyi password: 123456 + relabel_configs: + # 如果目标有context_path标签,则修改metrics_path以包含该上下文路径 + - source_labels: [__meta_http_sd_context_path] + target_label: __metrics_path__ + replacement: '${1}/actuator/prometheus' diff --git a/ruoyi-visual/ruoyi-monitor/src/main/java/org/dromara/modules/monitor/controller/PrometheusController.java b/ruoyi-visual/ruoyi-monitor/src/main/java/org/dromara/modules/monitor/controller/PrometheusController.java index 300b5dfdf..b294c4c06 100644 --- a/ruoyi-visual/ruoyi-monitor/src/main/java/org/dromara/modules/monitor/controller/PrometheusController.java +++ b/ruoyi-visual/ruoyi-monitor/src/main/java/org/dromara/modules/monitor/controller/PrometheusController.java @@ -45,6 +45,10 @@ public class PrometheusController { // labels.put("__meta_datacenter", "beijing"); // 服务名 labels.put("__meta_prometheus_job", service); + String contextPath = instances.get(0).getMetadata().get("management.context-path"); + if (contextPath != null) { + labels.put("__meta_http_sd_context_path", contextPath.replaceAll("/actuator", "")); + } Map group = new HashMap<>(2); group.put("targets", targets); group.put("labels", labels);