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/18 11:02:29 UTC

[camel-spring-boot] 01/02: CAMEL-19136: camel-micrometer-starter - Turn of metrics with uri tag by default as it can lead to too many tags due to dynamic values.

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

commit 1a4f1aaadd51fc2ea5de4d267c66335647b60a52
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Mar 18 11:52:15 2023 +0100

    CAMEL-19136: camel-micrometer-starter - Turn of metrics with uri tag by default as it can lead to too many tags due to dynamic values.
---
 .../src/main/docs/micrometer.json                      |  7 +++++++
 .../springboot/MicrometerTagsAutoConfiguration.java    | 14 ++++++++++++--
 .../springboot/metrics/CamelMetricsConfiguration.java  | 18 ++++++++++++++++++
 3 files changed, 37 insertions(+), 2 deletions(-)

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 e6c174b84c0..61705f360d1 100644
--- a/components-starter/camel-micrometer-starter/src/main/docs/micrometer.json
+++ b/components-starter/camel-micrometer-starter/src/main/docs/micrometer.json
@@ -76,6 +76,13 @@
       "description": "Set whether to enable the MicrometerRoutePolicyFactory for capturing metrics on route processing times.",
       "sourceType": "org.apache.camel.component.micrometer.springboot.metrics.CamelMetricsConfiguration",
       "defaultValue": true
+    },
+    {
+      "name": "camel.metrics.uri-tag-expand-values",
+      "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.",
+      "sourceType": "org.apache.camel.component.micrometer.springboot.metrics.CamelMetricsConfiguration",
+      "defaultValue": false
     }
   ],
   "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 36e6343c860..5c8948b279a 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
@@ -20,22 +20,29 @@ import io.micrometer.core.instrument.Tag;
 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.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.ConditionalOnProperty;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Conditional;
 import org.springframework.context.annotation.Configuration;
 
 @Configuration(proxyBeanMethods = false)
-@Conditional(ConditionalOnCamelContextAndAutoConfigurationBeans.class)
+@Conditional({ConditionalOnCamelContextAndAutoConfigurationBeans.class})
+@ConditionalOnProperty(prefix = "camel.metrics", name = "uriTagExpandValues", havingValue = "true")
 @AutoConfigureAfter({CamelAutoConfiguration.class})
 public class MicrometerTagsAutoConfiguration {
 
+    @Autowired
+    CamelContext camelContext;
+
     /**
-     * To integrate with micrometer to include uri in tags when for example using
+     * To integrate with micrometer to include expanded uri in tags when for example using
      * camel rest-dsl with servlet.
      */
     @Bean
@@ -44,6 +51,9 @@ public class MicrometerTagsAutoConfiguration {
             @Override
             public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response,
                                          Object handler, Throwable exception) {
+
+                camelContext.getComponent("servlet");
+
                 String uri = request.getServletPath();
                 if (uri == null || uri.isEmpty()) {
                     uri = request.getPathInfo();
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 5bdeae53ab0..ed048170b3d 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
@@ -21,6 +21,16 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
 @ConfigurationProperties(prefix = "camel.metrics")
 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}.
+     *
+     * 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.
+     */
+    private boolean uriTagExpandValues;
+
     /**
      * Set whether to enable the MicrometerRoutePolicyFactory for capturing metrics
      * on route processing times.
@@ -48,6 +58,14 @@ public class CamelMetricsConfiguration {
      */
     private boolean enableRouteEventNotifier = true;
 
+    public boolean isUriTagExpandValues() {
+        return uriTagExpandValues;
+    }
+
+    public void setUriTagExpandValues(boolean uriTagExpandValues) {
+        this.uriTagExpandValues = uriTagExpandValues;
+    }
+
     public boolean isEnableRoutePolicy() {
         return enableRoutePolicy;
     }