浏览代码

update 优化 prometheus sd发现 兼容携带context-path的服务

疯狂的狮子Li 1 年之前
父节点
当前提交
f890ad4246

+ 5 - 0
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'

+ 4 - 0
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<String, Object> group = new HashMap<>(2);
             group.put("targets", targets);
             group.put("labels", labels);