Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CXF-8213] Add Micrometer metric support for JAX-WS #642

Merged
merged 19 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion distribution/src/main/release/samples/jaxws_spring_boot/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.cxf.samples</groupId>
Expand Down Expand Up @@ -34,6 +35,19 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
reta marked this conversation as resolved.
Show resolved Hide resolved
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-metrics</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;

import org.apache.cxf.metrics.MetricsFeature;
import org.apache.cxf.metrics.MetricsProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -33,9 +35,14 @@ public class WebServiceConfig {
@Autowired
private Bus bus;

@Autowired
private MetricsProvider metricsProvider;

@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, new HelloPortImpl());
EndpointImpl endpoint = new EndpointImpl(bus, new HelloPortImpl(), null, null, new MetricsFeature[]{
new MetricsFeature(metricsProvider)
});
endpoint.publish("/Hello");
return endpoint;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
cxf.path=/Service

management.endpoint.metrics.enabled=true
management.endpoints.web.exposure.include=*
34 changes: 31 additions & 3 deletions integration/spring-boot/autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>cxf-spring-boot-autoconfigure</artifactId>
<packaging>bundle</packaging>
Expand Down Expand Up @@ -62,7 +63,6 @@
<scope>test</scope>
</dependency>


<!-- Annotation processing -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -74,6 +74,11 @@
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
Expand All @@ -86,12 +91,24 @@
<version>${project.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-metrics</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-service-description-openapi-v3</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
</dependency>
<dependency>
<groupId>${cxf.servlet-api.group}</groupId>
<artifactId>${cxf.servlet-api.artifact}</artifactId>
Expand All @@ -103,12 +120,23 @@
<artifactId>jakarta.validation-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<optional>true</optional>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${cxf.mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class CxfProperties {

private final Servlet servlet = new Servlet();

private final Metrics metrics = new Metrics();

@NotNull
@Pattern(regexp = "/[^?#]*", message = "Path must start with /")
public String getPath() {
Expand All @@ -57,6 +59,10 @@ public Servlet getServlet() {
return this.servlet;
}

public Metrics getMetrics() {
return this.metrics;
}

public static class Servlet {

/**
Expand Down Expand Up @@ -87,4 +93,58 @@ public void setLoadOnStartup(int loadOnStartup) {

}

public static class Metrics {

private final Server server = new Server();

public Server getServer() {
return this.server;
}

public static class Server {

/**
* Whether requests handled by Cxf should be automatically timed. If the number of time series
* emitted grows too large on account of request mapping timings, disable this and use 'Timed'
* on a per request mapping basis as needed.
*/
private boolean autoTimeRequests = true;

/**
* Name of the metric for received requests.
*/
private String requestsMetricName = "cxf.server.requests";

/**
* Maximum number of unique URI tag values allowed. After the max number of tag values is
* reached, metrics with additional tag values are denied by filter.
*/
private int maxUriTags = 100;

public boolean isAutoTimeRequests() {
return this.autoTimeRequests;
}

public void setAutoTimeRequests(boolean autoTimeRequests) {
this.autoTimeRequests = autoTimeRequests;
}

public String getRequestsMetricName() {
return this.requestsMetricName;
}

public void setRequestsMetricName(String requestsMetricName) {
this.requestsMetricName = requestsMetricName;
}

public int getMaxUriTags() {
return this.maxUriTags;
}

public void setMaxUriTags(int maxUriTags) {
this.maxUriTags = maxUriTags;
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/**
* 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.cxf.spring.boot.autoconfigure.micrometer;

import java.util.List;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.metrics.MetricsProvider;
import org.apache.cxf.metrics.micrometer.MicrometerMetricsProperties;
import org.apache.cxf.metrics.micrometer.MicrometerMetricsProvider;
import org.apache.cxf.metrics.micrometer.provider.DefaultExceptionClassProvider;
import org.apache.cxf.metrics.micrometer.provider.ExceptionClassProvider;
import org.apache.cxf.metrics.micrometer.provider.StandardTags;
import org.apache.cxf.metrics.micrometer.provider.StandardTagsProvider;
import org.apache.cxf.metrics.micrometer.provider.TagsCustomizer;
import org.apache.cxf.metrics.micrometer.provider.TagsProvider;
import org.apache.cxf.metrics.micrometer.provider.TimedAnnotationProvider;
import org.apache.cxf.metrics.micrometer.provider.jaxws.JaxwsFaultCodeProvider;
import org.apache.cxf.metrics.micrometer.provider.jaxws.JaxwsFaultCodeTagsCustomizer;
import org.apache.cxf.metrics.micrometer.provider.jaxws.JaxwsOperationTagsCustomizer;
import org.apache.cxf.metrics.micrometer.provider.jaxws.JaxwsTags;
import org.apache.cxf.spring.boot.autoconfigure.CxfProperties;
import org.apache.cxf.spring.boot.autoconfigure.CxfProperties.Metrics.Server;
import org.apache.cxf.spring.boot.autoconfigure.micrometer.provider.SpringBasedTimedAnnotationProvider;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.OnlyOnceLoggingDenyMeterFilter;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.config.MeterFilter;

@Configuration
@AutoConfigureAfter({MetricsAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class})
shark300 marked this conversation as resolved.
Show resolved Hide resolved
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass({JaxWsServerFactoryBean.class, MetricsProvider.class})
@ConditionalOnBean({MeterRegistry.class})
@EnableConfigurationProperties(CxfProperties.class)
public class MicrometerMetricsAutoConfiguration {

private final CxfProperties properties;

public MicrometerMetricsAutoConfiguration(CxfProperties properties) {
this.properties = properties;
}

@Bean
public TimedAnnotationProvider timedAnnotationProvider() {
return new SpringBasedTimedAnnotationProvider();
}

@Bean
public JaxwsTags jaxwsTags() {
return new JaxwsTags();
}

@Bean
@ConditionalOnMissingBean(ExceptionClassProvider.class)
public ExceptionClassProvider exceptionClassProvider() {
shark300 marked this conversation as resolved.
Show resolved Hide resolved
return new DefaultExceptionClassProvider();
}

@Bean
@ConditionalOnMissingBean(JaxwsFaultCodeProvider.class)
public JaxwsFaultCodeProvider jaxwsFaultCodeProvider() {
return new JaxwsFaultCodeProvider();
}

@Bean
@ConditionalOnMissingBean(JaxwsFaultCodeTagsCustomizer.class)
public JaxwsFaultCodeTagsCustomizer jaxwsFaultCodeTagsCustomizer(JaxwsTags jaxwsTags,
JaxwsFaultCodeProvider jaxwsFaultCodeProvider) {
return new JaxwsFaultCodeTagsCustomizer(jaxwsTags, jaxwsFaultCodeProvider);
}

@Bean
@ConditionalOnMissingBean(JaxwsOperationTagsCustomizer.class)
public JaxwsOperationTagsCustomizer jaxwsOperationTagsCustomizer(JaxwsTags jaxwsTags) {
return new JaxwsOperationTagsCustomizer(jaxwsTags);
}

@Bean
@ConditionalOnMissingBean(StandardTags.class)
public StandardTags standardTags() {
return new StandardTags();
}

@Bean
@ConditionalOnMissingBean(TagsProvider.class)
public TagsProvider tagsProvider(ExceptionClassProvider exceptionClassProvider, StandardTags standardTags) {
return new StandardTagsProvider(exceptionClassProvider, standardTags);
}

@Bean
@ConditionalOnMissingBean(MetricsProvider.class)
public MetricsProvider metricsProvider(TagsProvider tagsProvider,
List<TagsCustomizer> tagsCustomizers,
TimedAnnotationProvider timedAnnotationProvider,
MeterRegistry registry) {
MicrometerMetricsProperties micrometerMetricsProperties = new MicrometerMetricsProperties();

Server server = this.properties.getMetrics().getServer();
micrometerMetricsProperties.setAutoTimeRequests(server.isAutoTimeRequests());
micrometerMetricsProperties.setRequestsMetricName(server.getRequestsMetricName());

return new MicrometerMetricsProvider(registry, tagsProvider, tagsCustomizers, timedAnnotationProvider,
micrometerMetricsProperties);
}

@Bean
@Order(0)
public MeterFilter cxfMetricsMaxAllowedServerUriTagsFilter() {
String metricName = this.properties.getMetrics().getServer().getRequestsMetricName();
MeterFilter filter = new OnlyOnceLoggingDenyMeterFilter(
() -> String.format("Reached the maximum number of URI tags for '%s'.", metricName));
return MeterFilter.maximumAllowableTags(
metricName, "uri", this.properties.getMetrics().getServer().getMaxUriTags(), filter);
}
}
Loading