You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pc...@apache.org on 2026/04/28 13:21:20 UTC

(camel) branch main updated: feat(components): log on shutdown prometheus format

This is an automated email from the ASF dual-hosted git repository.

squakez pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new c76021c7e66a feat(components): log on shutdown prometheus format
c76021c7e66a is described below

commit c76021c7e66a7ceda87c3a68882bf5e2dff5a52b
Author: Pasquale Congiusti <pa...@gmail.com>
AuthorDate: Tue Apr 28 11:13:42 2026 +0200

    feat(components): log on shutdown prometheus format
    
    Possibility to select json (default) or prometheus format when logging metric traces
---
 .../prometheus/MicrometerPrometheusConfigurer.java |  6 ++
 .../prometheus/MicrometerPrometheus.java           | 65 ++++++++++++++++++++--
 .../resources/observability-services.properties    |  1 +
 3 files changed, 68 insertions(+), 4 deletions(-)

diff --git a/components/camel-micrometer-prometheus/src/generated/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheusConfigurer.java b/components/camel-micrometer-prometheus/src/generated/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheusConfigurer.java
index f26413cabeb9..9b435fe0b430 100644
--- a/components/camel-micrometer-prometheus/src/generated/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheusConfigurer.java
+++ b/components/camel-micrometer-prometheus/src/generated/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheusConfigurer.java
@@ -44,6 +44,8 @@ public class MicrometerPrometheusConfigurer extends org.apache.camel.support.com
         case "logMetricsOnShutdown": target.setLogMetricsOnShutdown(property(camelContext, boolean.class, value)); return true;
         case "logmetricsonshutdownfilters":
         case "logMetricsOnShutdownFilters": target.setLogMetricsOnShutdownFilters(property(camelContext, java.lang.String.class, value)); return true;
+        case "logmetricsonshutdownformat":
+        case "logMetricsOnShutdownFormat": target.setLogMetricsOnShutdownFormat(property(camelContext, java.lang.String.class, value)); return true;
         case "namingstrategy":
         case "namingStrategy": target.setNamingStrategy(property(camelContext, java.lang.String.class, value)); return true;
         case "path": target.setPath(property(camelContext, java.lang.String.class, value)); return true;
@@ -81,6 +83,8 @@ public class MicrometerPrometheusConfigurer extends org.apache.camel.support.com
         case "logMetricsOnShutdown": return boolean.class;
         case "logmetricsonshutdownfilters":
         case "logMetricsOnShutdownFilters": return java.lang.String.class;
+        case "logmetricsonshutdownformat":
+        case "logMetricsOnShutdownFormat": return java.lang.String.class;
         case "namingstrategy":
         case "namingStrategy": return java.lang.String.class;
         case "path": return java.lang.String.class;
@@ -119,6 +123,8 @@ public class MicrometerPrometheusConfigurer extends org.apache.camel.support.com
         case "logMetricsOnShutdown": return target.isLogMetricsOnShutdown();
         case "logmetricsonshutdownfilters":
         case "logMetricsOnShutdownFilters": return target.getLogMetricsOnShutdownFilters();
+        case "logmetricsonshutdownformat":
+        case "logMetricsOnShutdownFormat": return target.getLogMetricsOnShutdownFormat();
         case "namingstrategy":
         case "namingStrategy": return target.getNamingStrategy();
         case "path": return target.getPath();
diff --git a/components/camel-micrometer-prometheus/src/main/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheus.java b/components/camel-micrometer-prometheus/src/main/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheus.java
index e9fbd7147777..059db9be5c99 100644
--- a/components/camel-micrometer-prometheus/src/main/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheus.java
+++ b/components/camel-micrometer-prometheus/src/main/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheus.java
@@ -106,6 +106,8 @@ public class MicrometerPrometheus extends ServiceSupport implements CamelMetrics
     private boolean skipCamelInfo = false;
     @Metadata(defaultValue = "false")
     private boolean logMetricsOnShutdown = false;
+    @Metadata(defaultValue = "json", enums = "json,prometheus")
+    private String logMetricsOnShutdownFormat = "json";
     @Metadata(defaultValue = "0.0.4", enums = "0.0.4,1.0.0")
     private String textFormatVersion = "0.0.4";
     @Metadata
@@ -307,6 +309,18 @@ public class MicrometerPrometheus extends ServiceSupport implements CamelMetrics
         this.logMetricsOnShutdownFilters = logMetricsOnShutdownFilters;
     }
 
+    public String getLogMetricsOnShutdownFormat() {
+        return logMetricsOnShutdownFormat;
+    }
+
+    /**
+     * Format of metrics to log when application is shutting down. Either `json` (default) or `prometheus` format
+     * accepted.
+     */
+    public void setLogMetricsOnShutdownFormat(String logMetricsOnShutdownFormat) {
+        this.logMetricsOnShutdownFormat = logMetricsOnShutdownFormat;
+    }
+
     @Override
     protected void doInit() throws Exception {
         super.doInit();
@@ -358,10 +372,14 @@ public class MicrometerPrometheus extends ServiceSupport implements CamelMetrics
         if (isEnableExchangeEventNotifier()) {
             MicrometerExchangeEventNotifier notifier = new MicrometerExchangeEventNotifier();
             notifier.setSkipCamelInfo(isSkipCamelInfo());
-            notifier.setLogMetricsOnShutdown(isLogMetricsOnShutdown());
-            if (getLogMetricsOnShutdownFilters() != null) {
-                String[] meterFilters = getLogMetricsOnShutdownFilters().split(",");
-                notifier.setLogMetricsOnShutdownFilters(meterFilters);
+            // We delegate the print to the notifier only when the format
+            // configuration is json. If prometheus, we must take care in this component instead
+            if (getLogMetricsOnShutdownFormat().equals("json")) {
+                notifier.setLogMetricsOnShutdown(isLogMetricsOnShutdown());
+                if (getLogMetricsOnShutdownFilters() != null) {
+                    String[] meterFilters = getLogMetricsOnShutdownFilters().split(",");
+                    notifier.setLogMetricsOnShutdownFilters(meterFilters);
+                }
             }
             notifier.setBaseEndpointURI(isBaseEndpointURIExchangeEventNotifier());
             if ("legacy".equalsIgnoreCase(namingStrategy)) {
@@ -482,6 +500,45 @@ public class MicrometerPrometheus extends ServiceSupport implements CamelMetrics
         }
     }
 
+    @Override
+    protected void doStop() {
+        // NOTE: this components only takes care to trace when we set the "prometheus" format
+        if (logMetricsOnShutdown && logMetricsOnShutdownFormat.equals("prometheus")) {
+            LOG.warn("Micrometer service is stopping, here a list of metrics collected so far.");
+            // Default: all metrics
+            logMetricsOnShutdown(
+                    logMetricsOnShutdownFilters == null ? new String[] { "*" } : logMetricsOnShutdownFilters.split(","));
+        }
+    }
+
+    void logMetricsOnShutdown(String... filters) {
+        String[] scrapes = meterRegistry.scrape().split("\n");
+        for (String s : scrapes) {
+            if (matchesFilter(s, filters)) {
+                // We put on warn level to make sure it is printed even if the log is
+                // at higher levels. Important: we also include a start and end tag to make sure the
+                // scraper can more easily identify the metric content.
+                LOG.warn("#METRIC-START#" + s + "#METRIC-END#");
+            }
+        }
+    }
+
+    static boolean matchesFilter(String line, String... filters) {
+        for (String filter : filters) {
+            // Prometheus format does not use . but _ instead
+            filter = filter.replaceAll("\\.", "_");
+
+            if (filter.contains("*")) {
+                if (line.contains(filter.replace("*", ""))) {
+                    return true;
+                }
+            } else if (line.contains(filter)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     @Override
     protected void doShutdown() throws Exception {
         super.doShutdown();
diff --git a/components/camel-observability-services/src/main/resources/observability-services.properties b/components/camel-observability-services/src/main/resources/observability-services.properties
index fc7a3a700c53..7bae4ce22a7c 100644
--- a/components/camel-observability-services/src/main/resources/observability-services.properties
+++ b/components/camel-observability-services/src/main/resources/observability-services.properties
@@ -30,5 +30,6 @@ camel.metrics.path=/observe/metrics
 camel.metrics.binders=uptime,processor,jvm-info,jvm-memory,jvm-thread,jvm-gc
 camel.metrics.logMetricsOnShutdown=true
 camel.metrics.logMetricsOnShutdownFilters=app.info,camel.exchanges.*,process.cpu.usage,jvm.memory.max,jvm.memory.used
+camel.metrics.logMetricsOnShutdownFormat=prometheus
 # Opentelemetry
 camel.opentelemetry2.enabled=true