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-spring-boot.git


The following commit(s) were added to refs/heads/main by this push:
     new 3d22182582d feat(starters): micrometer advertise Prometheus port and 
endpoint
3d22182582d is described below

commit 3d22182582dfc2ed16a7d2fd64f59e7006194d03
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Fri Dec 4 11:19:22 2026 +0100

    feat(starters): micrometer advertise Prometheus port and endpoint
    
    Add a startup log to show port and path where the Prometheus service is 
exposed
    
    Ref https://issues.apache.org/jira/browse/CAMEL-27899
---
 .../camel-micrometer-starter/pom.xml               |  5 ++
 .../PrometheusStartupLogAutoConfiguration.java     | 96 ++++++++++++++++++++++
 ...rk.boot.autoconfigure.AutoConfiguration.imports |  3 +-
 .../springboot/PrometheusStartupLogTest.java       | 51 ++++++++++++
 4 files changed, 154 insertions(+), 1 deletion(-)

diff --git a/components-starter/camel-micrometer-starter/pom.xml 
b/components-starter/camel-micrometer-starter/pom.xml
index 1cd7f0c1f33..abf15f6a409 100644
--- a/components-starter/camel-micrometer-starter/pom.xml
+++ b/components-starter/camel-micrometer-starter/pom.xml
@@ -60,6 +60,11 @@
       <artifactId>camel-servlet-starter</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>io.micrometer</groupId>
+      <artifactId>micrometer-registry-prometheus</artifactId>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>org.awaitility</groupId>
       <artifactId>awaitility</artifactId>
diff --git 
a/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/metrics/PrometheusStartupLogAutoConfiguration.java
 
b/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/metrics/PrometheusStartupLogAutoConfiguration.java
new file mode 100644
index 00000000000..9cb14edef75
--- /dev/null
+++ 
b/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/metrics/PrometheusStartupLogAutoConfiguration.java
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.micrometer.springboot.metrics;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import 
org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
+import org.springframework.boot.autoconfigure.AutoConfiguration;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
+import 
org.springframework.boot.micrometer.metrics.autoconfigure.export.prometheus.PrometheusScrapeEndpoint;
+import org.springframework.boot.web.server.context.WebServerApplicationContext;
+import org.springframework.boot.web.server.context.WebServerInitializedEvent;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.event.EventListener;
+import org.springframework.core.env.Environment;
+
+/**
+ * Logs at startup the port and the path where the Prometheus scrape endpoint 
is exposed.
+ */
+@AutoConfiguration(afterName = {
+        
"org.springframework.boot.micrometer.metrics.autoconfigure.export.prometheus.PrometheusMetricsExportAutoConfiguration",
+        
"org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration"
 })
+@ConditionalOnClass(PrometheusScrapeEndpoint.class)
+@ConditionalOnBean(PrometheusScrapeEndpoint.class)
+@ConditionalOnAvailableEndpoint(endpoint = PrometheusScrapeEndpoint.class)
+public class PrometheusStartupLogAutoConfiguration {
+
+    @Bean
+    public PrometheusStartupLogListener 
prometheusStartupLogListener(Environment environment) {
+        return new PrometheusStartupLogListener(environment);
+    }
+
+    public static class PrometheusStartupLogListener {
+
+        private static final Logger LOG = 
LoggerFactory.getLogger(PrometheusStartupLogListener.class);
+
+        private static final String MANAGEMENT_NAMESPACE = "management";
+
+        private final Environment environment;
+
+        public PrometheusStartupLogListener(Environment environment) {
+            this.environment = environment;
+        }
+
+        @EventListener
+        public void onWebServerInitialized(WebServerInitializedEvent event) {
+            Integer managementPort = 
environment.getProperty("management.server.port", Integer.class);
+            if (managementPort != null && managementPort < 0) {
+                // management endpoints are disabled
+                return;
+            }
+            String namespace = 
WebServerApplicationContext.getServerNamespace(event.getApplicationContext());
+            int port = event.getWebServer().getPort();
+            boolean management = MANAGEMENT_NAMESPACE.equals(namespace);
+            if (!management && managementPort != null && managementPort != 
port) {
+                // management endpoints are served by another web server
+                return;
+            }
+            LOG.info("MicrometerPrometheus enabled on port {} with path {}", 
port,
+                    path());
+        }
+
+        private String path() {
+            String managementBasePath = 
environment.getProperty("management.server.base-path", "");
+            String basePath = 
environment.getProperty("management.endpoints.web.base-path", "/actuator");
+            String mapping = 
environment.getProperty("management.endpoints.web.path-mapping.prometheus", 
"prometheus");
+            return normalize(managementBasePath) + normalize(basePath) + 
normalize(mapping);
+        }
+
+        private static String normalize(String path) {
+            String answer = path.trim();
+            while (answer.endsWith("/")) {
+                answer = answer.substring(0, answer.length() - 1);
+            }
+            if (!answer.isEmpty() && !answer.startsWith("/")) {
+                answer = "/" + answer;
+            }
+            return answer;
+        }
+    }
+}
diff --git 
a/components-starter/camel-micrometer-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
 
b/components-starter/camel-micrometer-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
index db7ad2cee34..f17e671c23a 100644
--- 
a/components-starter/camel-micrometer-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
+++ 
b/components-starter/camel-micrometer-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
@@ -18,4 +18,5 @@
 org.apache.camel.component.micrometer.springboot.MicrometerComponentConverter
 
org.apache.camel.component.micrometer.springboot.MicrometerComponentAutoConfiguration
 
org.apache.camel.component.micrometer.springboot.MicrometerTagsAutoConfiguration
-org.apache.camel.component.micrometer.springboot.metrics.CamelMetricsAutoConfiguration
\ No newline at end of file
+org.apache.camel.component.micrometer.springboot.metrics.CamelMetricsAutoConfiguration
+org.apache.camel.component.micrometer.springboot.metrics.PrometheusStartupLogAutoConfiguration
diff --git 
a/components-starter/camel-micrometer-starter/src/test/java/org/apache/camel/component/micrometer/springboot/PrometheusStartupLogTest.java
 
b/components-starter/camel-micrometer-starter/src/test/java/org/apache/camel/component/micrometer/springboot/PrometheusStartupLogTest.java
new file mode 100644
index 00000000000..ae6eafc9335
--- /dev/null
+++ 
b/components-starter/camel-micrometer-starter/src/test/java/org/apache/camel/component/micrometer/springboot/PrometheusStartupLogTest.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.micrometer.springboot;
+
+import org.apache.camel.spring.boot.CamelAutoConfiguration;
+import org.apache.camel.test.spring.junit6.CamelSpringBootTest;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.system.CapturedOutput;
+import org.springframework.boot.test.system.OutputCaptureExtension;
+import org.springframework.test.annotation.DirtiesContext;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * The port and the path where the Prometheus endpoint is exposed must be 
logged at startup.
+ */
+@DirtiesContext
+@CamelSpringBootTest
+@EnableAutoConfiguration
+@ExtendWith(OutputCaptureExtension.class)
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
+                classes = CamelAutoConfiguration.class,
+                properties = { "management.server.port=0",
+                        "management.endpoints.web.exposure.include=prometheus",
+                        "management.endpoints.web.base-path=/observe",
+                        
"management.endpoints.web.path-mapping.prometheus=metrics" })
+public class PrometheusStartupLogTest {
+
+    @Test
+    void logsManagementPortAndPath(CapturedOutput output) {
+        assertThat(output.getOut()).containsPattern(
+                "MicrometerPrometheus enabled on port [1-9]\\d* with path 
/observe/metrics");
+    }
+}

Reply via email to