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 ac318f0418e55c56d0fb426607c67a5ceb1f6742 Author: Claus Ibsen <[email protected]> AuthorDate: Sat Nov 26 15:08:14 2022 +0100 CAMEL-18754: camel-micrometer - Add auto configuration for capturing metrics. --- .../camel-micrometer-starter/pom.xml | 10 +++++ .../src/main/docs/micrometer.json | 12 +++--- .../MicrometerTagsAutoConfiguration.java | 48 ++++++++++++++++++++++ .../CamelMetricsAutoConfiguration.java} | 44 +++++++++----------- .../CamelMetricsConfiguration.java} | 4 +- .../src/main/resources/META-INF/spring.factories | 3 +- 6 files changed, 88 insertions(+), 33 deletions(-) diff --git a/components-starter/camel-micrometer-starter/pom.xml b/components-starter/camel-micrometer-starter/pom.xml index 9821efd078b..42c632a242e 100644 --- a/components-starter/camel-micrometer-starter/pom.xml +++ b/components-starter/camel-micrometer-starter/pom.xml @@ -34,6 +34,16 @@ <artifactId>spring-boot-starter</artifactId> <version>${spring-boot-version}</version> </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + <version>${spring-boot-version}</version> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-actuator</artifactId> + <version>${spring-boot-version}</version> + </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-micrometer</artifactId> 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 3872996642d..e6c174b84c0 100644 --- a/components-starter/camel-micrometer-starter/src/main/docs/micrometer.json +++ b/components-starter/camel-micrometer-starter/src/main/docs/micrometer.json @@ -13,8 +13,8 @@ }, { "name": "camel.metrics", - "type": "org.apache.camel.component.micrometer.springboot.CamelMicrometerConfiguration", - "sourceType": "org.apache.camel.component.micrometer.springboot.CamelMicrometerConfiguration" + "type": "org.apache.camel.component.micrometer.springboot.metrics.CamelMetricsConfiguration", + "sourceType": "org.apache.camel.component.micrometer.springboot.metrics.CamelMetricsConfiguration" } ], "properties": [ @@ -53,28 +53,28 @@ "name": "camel.metrics.enable-exchange-event-notifier", "type": "java.lang.Boolean", "description": "Set whether to enable the MicrometerExchangeEventNotifier for capturing metrics on exchange processing times.", - "sourceType": "org.apache.camel.component.micrometer.springboot.CamelMicrometerConfiguration", + "sourceType": "org.apache.camel.component.micrometer.springboot.metrics.CamelMetricsConfiguration", "defaultValue": true }, { "name": "camel.metrics.enable-message-history", "type": "java.lang.Boolean", "description": "Set whether to enable the MicrometerMessageHistoryFactory for capturing metrics on individual route node processing times. Depending on the number of configured route nodes, there is the potential to create a large volume of metrics. Therefore, this option is disabled by default.", - "sourceType": "org.apache.camel.component.micrometer.springboot.CamelMicrometerConfiguration", + "sourceType": "org.apache.camel.component.micrometer.springboot.metrics.CamelMetricsConfiguration", "defaultValue": false }, { "name": "camel.metrics.enable-route-event-notifier", "type": "java.lang.Boolean", "description": "Set whether to enable the MicrometerRouteEventNotifier for capturing metrics on the total number of routes and total number of routes running.", - "sourceType": "org.apache.camel.component.micrometer.springboot.CamelMicrometerConfiguration", + "sourceType": "org.apache.camel.component.micrometer.springboot.metrics.CamelMetricsConfiguration", "defaultValue": true }, { "name": "camel.metrics.enable-route-policy", "type": "java.lang.Boolean", "description": "Set whether to enable the MicrometerRoutePolicyFactory for capturing metrics on route processing times.", - "sourceType": "org.apache.camel.component.micrometer.springboot.CamelMicrometerConfiguration", + "sourceType": "org.apache.camel.component.micrometer.springboot.metrics.CamelMetricsConfiguration", "defaultValue": true } ], 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 new file mode 100644 index 00000000000..5745cc72aee --- /dev/null +++ b/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/MicrometerTagsAutoConfiguration.java @@ -0,0 +1,48 @@ +package org.apache.camel.component.micrometer.springboot; + +import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.Tags; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.apache.camel.spring.boot.util.ConditionalOnCamelContextAndAutoConfigurationBeans; +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.context.annotation.Bean; +import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.Configuration; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@Configuration(proxyBeanMethods = false) +@Conditional(ConditionalOnCamelContextAndAutoConfigurationBeans.class) +@AutoConfigureAfter({CamelAutoConfiguration.class}) +public class MicrometerTagsAutoConfiguration { + + /** + * To integrate with micrometer to include uri in tags when for example using + * camel rest-dsl with servlet. + */ + @Bean + WebMvcTagsProvider webMvcTagsProvider() { + return new DefaultWebMvcTagsProvider() { + @Override + public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, + Object handler, Throwable exception) { + String uri = request.getServletPath(); + if (uri == null || uri.isEmpty()) { + uri = request.getPathInfo(); + } else { + String p = request.getPathInfo(); + if (p != null) { + uri = uri + p; + } + } + 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/CamelMicrometerAutoConfiguration.java b/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/metrics/CamelMetricsAutoConfiguration.java similarity index 61% rename from components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/CamelMicrometerAutoConfiguration.java rename to components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/metrics/CamelMetricsAutoConfiguration.java index 69cec7f101c..c654ee34e97 100644 --- a/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/CamelMicrometerAutoConfiguration.java +++ b/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/metrics/CamelMetricsAutoConfiguration.java @@ -14,62 +14,58 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.component.micrometer.springboot; +package org.apache.camel.component.micrometer.springboot.metrics; +import io.micrometer.core.instrument.MeterRegistry; import org.apache.camel.CamelContext; import org.apache.camel.component.micrometer.eventnotifier.MicrometerExchangeEventNotifier; import org.apache.camel.component.micrometer.eventnotifier.MicrometerRouteEventNotifier; import org.apache.camel.component.micrometer.messagehistory.MicrometerMessageHistoryFactory; import org.apache.camel.component.micrometer.routepolicy.MicrometerRoutePolicyFactory; -import org.apache.camel.spi.CamelContextCustomizer; import org.apache.camel.spi.ManagementStrategy; 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.autoconfigure.AutoConfigureAfter; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; @Conditional(ConditionalOnCamelContextAndAutoConfigurationBeans.class) -@EnableConfigurationProperties({CamelMicrometerConfiguration.class}) +@EnableConfigurationProperties({CamelMetricsConfiguration.class}) @AutoConfigureAfter({CamelAutoConfiguration.class}) -public class CamelMicrometerAutoConfiguration { +public class CamelMetricsAutoConfiguration { - @Autowired - private ApplicationContext applicationContext; - private final CamelContext camelContext; - @Autowired - private CamelMicrometerConfiguration configuration; - - public CamelMicrometerAutoConfiguration( - CamelContext camelContext) { - this.camelContext = camelContext; + public CamelMetricsAutoConfiguration( + CamelContext camelContext, CamelMetricsConfiguration configuration, MeterRegistry meterRegistry) { + configureMicrometer(camelContext, configuration, meterRegistry); } - @Bean - public CamelContextCustomizer configureMicrometer() { + private void configureMicrometer(CamelContext camelContext, CamelMetricsConfiguration configuration, MeterRegistry meterRegistry) { if (configuration.isEnableRoutePolicy()) { - camelContext.addRoutePolicyFactory(new MicrometerRoutePolicyFactory()); + MicrometerRoutePolicyFactory factory = new MicrometerRoutePolicyFactory(); + factory.setMeterRegistry(meterRegistry); + camelContext.addRoutePolicyFactory(factory); } ManagementStrategy managementStrategy = camelContext.getManagementStrategy(); if (configuration.isEnableExchangeEventNotifier()) { - managementStrategy.addEventNotifier(new MicrometerExchangeEventNotifier()); + MicrometerExchangeEventNotifier notifier = new MicrometerExchangeEventNotifier(); + notifier.setMeterRegistry(meterRegistry); + managementStrategy.addEventNotifier(notifier); } if (configuration.isEnableRouteEventNotifier()) { - managementStrategy.addEventNotifier(new MicrometerRouteEventNotifier()); + MicrometerRouteEventNotifier notifier = new MicrometerRouteEventNotifier(); + notifier.setMeterRegistry(meterRegistry); + managementStrategy.addEventNotifier(notifier); } if (configuration.isEnableMessageHistory()) { if (!camelContext.isMessageHistory()) { camelContext.setMessageHistory(true); } - camelContext.setMessageHistoryFactory(new MicrometerMessageHistoryFactory()); + MicrometerMessageHistoryFactory factory = new MicrometerMessageHistoryFactory(); + factory.setMeterRegistry(meterRegistry); + camelContext.setMessageHistoryFactory(factory); } - - return null; } } \ No newline at end of file diff --git a/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/CamelMicrometerConfiguration.java b/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/metrics/CamelMetricsConfiguration.java similarity index 96% rename from components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/CamelMicrometerConfiguration.java rename to components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/metrics/CamelMetricsConfiguration.java index a1eced0e87c..5bdeae53ab0 100644 --- a/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/CamelMicrometerConfiguration.java +++ b/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/metrics/CamelMetricsConfiguration.java @@ -14,12 +14,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.component.micrometer.springboot; +package org.apache.camel.component.micrometer.springboot.metrics; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "camel.metrics") -public class CamelMicrometerConfiguration { +public class CamelMetricsConfiguration { /** * Set whether to enable the MicrometerRoutePolicyFactory for capturing metrics diff --git a/components-starter/camel-micrometer-starter/src/main/resources/META-INF/spring.factories b/components-starter/camel-micrometer-starter/src/main/resources/META-INF/spring.factories index 74503547d6a..01c9d88aa55 100644 --- a/components-starter/camel-micrometer-starter/src/main/resources/META-INF/spring.factories +++ b/components-starter/camel-micrometer-starter/src/main/resources/META-INF/spring.factories @@ -18,5 +18,6 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.apache.camel.component.micrometer.springboot.MicrometerComponentAutoConfiguration,\ org.apache.camel.component.micrometer.springboot.MicrometerComponentConverter,\ -org.apache.camel.component.micrometer.springboot.CamelMicrometerAutoConfiguration +org.apache.camel.component.micrometer.springboot.MicrometerTagsAutoConfiguration,\ +org.apache.camel.component.micrometer.springboot.metrics.CamelMetricsAutoConfiguration
