You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/03/19 11:03:45 UTC

[camel-spring-boot] branch camel-spring-boot-3.x updated: CAMEL-19168: camel-micrometer-starter - Make it possible to capture static uri path as tag

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

davsclaus pushed a commit to branch camel-spring-boot-3.x
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git


The following commit(s) were added to refs/heads/camel-spring-boot-3.x by this push:
     new 6f0c985c134 CAMEL-19168: camel-micrometer-starter - Make it possible to capture static uri path as tag
6f0c985c134 is described below

commit 6f0c985c13431f05388c2fcd8749b1a87233f873
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Mar 19 12:03:35 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                  |  4 +--
 .../MicrometerTagsAutoConfiguration.java           | 34 +++++++++++++++++-----
 .../metrics/CamelMetricsConfiguration.java         | 25 +++++++++-------
 .../ServletMappingAutoConfiguration.java           | 17 +++++++++--
 5 files changed, 64 insertions(+), 21 deletions(-)

diff --git a/components-starter/camel-micrometer-starter/pom.xml b/components-starter/camel-micrometer-starter/pom.xml
index a069efe57b5..e784d6667ea 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 61705f360d1..5d73f147677 100644
--- a/components-starter/camel-micrometer-starter/src/main/docs/micrometer.json
+++ b/components-starter/camel-micrometer-starter/src/main/docs/micrometer.json
@@ -78,9 +78,9 @@
       "defaultValue": true
     },
     {
-      "name": "camel.metrics.uri-tag-expand-values",
+      "name": "camel.metrics.uri-tag-dynamic",
       "type": "java.lang.Boolean",
-      "description": "Whether HTTP uri tags should be expanded or not. For example a REST service defined with base URL: \/users\/{id} will capture metrics with uri tag: \/users\/{id}. There can be some use-cases where you want to expand the URI tag to include the actual requested value instead, so the uri tag will be something like: \/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
     }
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 8224a83ea72..51fa26f37a7 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
@@ -18,6 +18,9 @@ package org.apache.camel.component.micrometer.springboot;
 
 import io.micrometer.core.instrument.Tag;
 import io.micrometer.core.instrument.Tags;
+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.boot.actuate.metrics.web.servlet.DefaultWebMvcTagsProvider;
@@ -30,6 +33,7 @@ import org.springframework.context.annotation.Configuration;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.util.Optional;
 
 @Configuration(proxyBeanMethods = false)
 @Conditional(ConditionalOnCamelContextAndAutoConfigurationBeans.class)
@@ -42,20 +46,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 ed048170b3d..4763aa6e480 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,14 +22,19 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
 public class CamelMetricsConfiguration {
 
     /**
-     * Whether HTTP uri tags should be expanded or not. For example a REST service defined with
-     * base URL: /users/{id} will capture metrics with uri tag: /users/{id}.
+     * 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.
      *
-     * There can be some use-cases where you want to expand the URI tag to include the actual requested value instead,
-     * so the uri tag will be something like: /users/123
-     * However this can lead to many tags as the URI is dynamic, so use this with care.
+     * 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 uriTagExpandValues;
+    private boolean uriTagDynamic;
 
     /**
      * Set whether to enable the MicrometerRoutePolicyFactory for capturing metrics
@@ -58,12 +63,12 @@ public class CamelMetricsConfiguration {
      */
     private boolean enableRouteEventNotifier = true;
 
-    public boolean isUriTagExpandValues() {
-        return uriTagExpandValues;
+    public boolean isUriTagDynamic() {
+        return uriTagDynamic;
     }
 
-    public void setUriTagExpandValues(boolean uriTagExpandValues) {
-        this.uriTagExpandValues = uriTagExpandValues;
+    public void setUriTagDynamic(boolean uriTagDynamic) {
+        this.uriTagDynamic = uriTagDynamic;
     }
 
     public boolean isEnableRoutePolicy() {
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);