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

davsclaus 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 bbc44b0f217 CAMEL-19168: camel-micrometer-starter - Make it possible 
to capture static uri path as tag
bbc44b0f217 is described below

commit bbc44b0f217d73b3e94d52dc5ca2054a7169b788
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Sun Mar 19 11:57:21 2023 +0100

    CAMEL-19168: camel-micrometer-starter - Make it possible to capture static 
uri path as tag
---
 .../camel-micrometer-starter/pom.xml               |  5 +++
 .../src/main/docs/micrometer.json                  | 11 +++++--
 .../MicrometerTagsAutoConfiguration.java           | 36 +++++++++++++++++-----
 .../metrics/CamelMetricsConfiguration.java         | 22 +++++++++++--
 .../ServletMappingAutoConfiguration.java           | 17 ++++++++--
 5 files changed, 77 insertions(+), 14 deletions(-)

diff --git a/components-starter/camel-micrometer-starter/pom.xml 
b/components-starter/camel-micrometer-starter/pom.xml
index fe13dc2d265..6907b19a425 100644
--- a/components-starter/camel-micrometer-starter/pom.xml
+++ b/components-starter/camel-micrometer-starter/pom.xml
@@ -49,6 +49,11 @@
       <artifactId>camel-micrometer</artifactId>
       <version>${camel-version}</version>
     </dependency>
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-http-common</artifactId>
+      <version>${camel-version}</version>
+    </dependency>
     <!--START OF GENERATED CODE-->
     <dependency>
       <groupId>org.apache.camel.springboot</groupId>
diff --git 
a/components-starter/camel-micrometer-starter/src/main/docs/micrometer.json 
b/components-starter/camel-micrometer-starter/src/main/docs/micrometer.json
index 523d6e9ca2d..d11fc1b7bed 100644
--- a/components-starter/camel-micrometer-starter/src/main/docs/micrometer.json
+++ b/components-starter/camel-micrometer-starter/src/main/docs/micrometer.json
@@ -78,11 +78,18 @@
       "defaultValue": true
     },
     {
-      "name": "camel.metrics.uri-tag-enabled",
+      "name": "camel.metrics.uri-tag-dynamic",
       "type": "java.lang.Boolean",
-      "description": "Whether HTTP uri tags should be enabled or not. For 
example a REST service defined with base URL: \/users\/{id} will capture 
metrics with uri tag with actual dynamic value such as: \/users\/123. However, 
this can lead to many tags as the URI is dynamic, so use this with care.",
+      "description": "Whether to use static or dynamic values for URI tags in 
captured metrics. When using dynamic tags, then a REST service with base URL: 
\/users\/{id} will capture metrics with uri tag with the actual dynamic value 
such as: \/users\/123. However, this can lead to many tags as the URI is 
dynamic, so use this with care.",
       "sourceType": 
"org.apache.camel.component.micrometer.springboot.metrics.CamelMetricsConfiguration",
       "defaultValue": false
+    },
+    {
+      "name": "camel.metrics.uri-tag-enabled",
+      "type": "java.lang.Boolean",
+      "description": "Whether HTTP uri tags should be enabled or not in 
captured metrics. If disabled then the uri tag, is likely not able to be 
resolved and will be marked as UNKNOWN.",
+      "sourceType": 
"org.apache.camel.component.micrometer.springboot.metrics.CamelMetricsConfiguration",
+      "defaultValue": true
     }
   ],
   "hints": []
diff --git 
a/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/MicrometerTagsAutoConfiguration.java
 
b/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/MicrometerTagsAutoConfiguration.java
index 43c1a8fe342..55b4f6608d2 100644
--- 
a/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/MicrometerTagsAutoConfiguration.java
+++ 
b/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/MicrometerTagsAutoConfiguration.java
@@ -21,17 +21,23 @@ import io.micrometer.core.instrument.Tags;
 import jakarta.servlet.http.HttpServletRequest;
 import jakarta.servlet.http.HttpServletResponse;
 import org.apache.camel.CamelContext;
+import 
org.apache.camel.component.micrometer.springboot.metrics.CamelMetricsConfiguration;
+import org.apache.camel.http.common.CamelServlet;
+import org.apache.camel.http.common.HttpConsumer;
 import org.apache.camel.spring.boot.CamelAutoConfiguration;
 import 
org.apache.camel.spring.boot.util.ConditionalOnCamelContextAndAutoConfigurationBeans;
 import org.springframework.beans.factory.annotation.Autowired;
 import 
org.springframework.boot.actuate.metrics.web.servlet.DefaultWebMvcTagsProvider;
 import org.springframework.boot.actuate.metrics.web.servlet.WebMvcTagsProvider;
 import org.springframework.boot.autoconfigure.AutoConfigureAfter;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Conditional;
 import org.springframework.context.annotation.Configuration;
 
+import java.util.Optional;
+
 @Configuration(proxyBeanMethods = false)
 @Conditional({ConditionalOnCamelContextAndAutoConfigurationBeans.class})
 @ConditionalOnProperty(prefix = "camel.metrics", name = "uriTagEnabled", 
havingValue = "true")
@@ -46,20 +52,36 @@ public class MicrometerTagsAutoConfiguration {
      * camel rest-dsl with servlet.
      */
     @Bean
-    WebMvcTagsProvider webMvcTagsProvider() {
+    WebMvcTagsProvider webMvcTagsProvider(Optional<CamelServlet> servlet, 
CamelMetricsConfiguration configuration) {
         return new DefaultWebMvcTagsProvider() {
             @Override
             public Iterable<Tag> getTags(HttpServletRequest request, 
HttpServletResponse response,
                                          Object handler, Throwable exception) {
-                String uri = request.getServletPath();
+
+                String uri = null;
+                if (servlet.isPresent() && !configuration.isUriTagDynamic()) {
+                    HttpConsumer consumer = 
servlet.get().getServletResolveConsumerStrategy().resolve(request, 
servlet.get().getConsumers());
+                    if (consumer != null) {
+                        uri = consumer.getPath();
+                    }
+                }
+
+                // the request may not be for camel servlet, so we need to 
capture uri from request
                 if (uri == null || uri.isEmpty()) {
-                    uri = request.getPathInfo();
-                } else {
-                    String p = request.getPathInfo();
-                    if (p != null) {
-                        uri = uri + p;
+                    // dynamic uri with the actual value from the http request
+                    uri = request.getServletPath();
+                    if (uri == null || uri.isEmpty()) {
+                        uri = request.getPathInfo();
+                    } else {
+                        String p = request.getPathInfo();
+                        if (p != null) {
+                            uri = uri + p;
+                        }
                     }
                 }
+                if (uri == null) {
+                    uri = "";
+                }
                 return Tags.concat(
                         super.getTags(request, response, handler, exception),
                         Tags.of(Tag.of("uri", uri))
diff --git 
a/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/metrics/CamelMetricsConfiguration.java
 
b/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/metrics/CamelMetricsConfiguration.java
index 154a721618e..f31b115191a 100644
--- 
a/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/metrics/CamelMetricsConfiguration.java
+++ 
b/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/metrics/CamelMetricsConfiguration.java
@@ -22,11 +22,19 @@ import 
org.springframework.boot.context.properties.ConfigurationProperties;
 public class CamelMetricsConfiguration {
 
     /**
-     * Whether HTTP uri tags should be enabled or not. For example a REST 
service defined with
-     * base URL: /users/{id} will capture metrics with uri tag with actual 
dynamic value such as: /users/123.
+     * Whether HTTP uri tags should be enabled or not in captured metrics.
+     * If disabled then the uri tag, is likely not able to be resolved and 
will be marked as UNKNOWN.
+     */
+    private boolean uriTagEnabled = true;
+
+    /**
+     * Whether to use static or dynamic values for URI tags in captured 
metrics.
+     *
+     * When using dynamic tags, then a REST service with base URL: /users/{id} 
will capture metrics
+     * with uri tag with the actual dynamic value such as: /users/123.
      * However, this can lead to many tags as the URI is dynamic, so use this 
with care.
      */
-    private boolean uriTagEnabled;
+    private boolean uriTagDynamic;
 
     /**
      * Set whether to enable the MicrometerRoutePolicyFactory for capturing 
metrics
@@ -63,6 +71,14 @@ public class CamelMetricsConfiguration {
         this.uriTagEnabled = uriTagEnabled;
     }
 
+    public boolean isUriTagDynamic() {
+        return uriTagDynamic;
+    }
+
+    public void setUriTagDynamic(boolean uriTagDynamic) {
+        this.uriTagDynamic = uriTagDynamic;
+    }
+
     public boolean isEnableRoutePolicy() {
         return enableRoutePolicy;
     }
diff --git 
a/components-starter/camel-servlet-starter/src/main/java/org/apache/camel/component/servlet/springboot/ServletMappingAutoConfiguration.java
 
b/components-starter/camel-servlet-starter/src/main/java/org/apache/camel/component/servlet/springboot/ServletMappingAutoConfiguration.java
index 08b1d78bdb7..6107a3380b3 100644
--- 
a/components-starter/camel-servlet-starter/src/main/java/org/apache/camel/component/servlet/springboot/ServletMappingAutoConfiguration.java
+++ 
b/components-starter/camel-servlet-starter/src/main/java/org/apache/camel/component/servlet/springboot/ServletMappingAutoConfiguration.java
@@ -42,10 +42,23 @@ import org.springframework.context.annotation.Lazy;
 @EnableConfigurationProperties({ServletMappingConfiguration.class, 
MultipartProperties.class})
 public class ServletMappingAutoConfiguration {
 
+    /**
+     * Camel servlet
+     */
+    @Bean
+    CamelHttpTransportServlet camelHttpTransportServlet() {
+        CamelHttpTransportServlet servlet = new CamelHttpTransportServlet();
+        return servlet;
+    }
+
+    /**
+     * Spring Boot servlet registration with the Camel server
+     */
     @Bean
-    ServletRegistrationBean 
camelServletRegistrationBean(ServletMappingConfiguration config, 
MultipartProperties multipartProperties) {
+    ServletRegistrationBean 
camelServletRegistrationBean(CamelHttpTransportServlet servlet,
+                                                         
ServletMappingConfiguration config, MultipartProperties multipartProperties) {
         ServletRegistrationBean mapping = new ServletRegistrationBean();
-        mapping.setServlet(new CamelHttpTransportServlet());
+        mapping.setServlet(servlet);
         mapping.addUrlMappings(config.getContextPath());
         mapping.setName(config.getServletName());
         mapping.setLoadOnStartup(1);

Reply via email to