Skip to content

Commit

Permalink
Support using CXF TagsCustomizers and deprecate dynamic usage of
Browse files Browse the repository at this point in the history
MeterFilter #1492
  • Loading branch information
ppalaga committed Sep 3, 2024
1 parent 15f64b6 commit eb553e2
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 110 deletions.
3 changes: 2 additions & 1 deletion docs/modules/ROOT/examples/hc5/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ quarkus.cxf.client.myCalculator.metrics.enabled = false
quarkus.cxf.client.observableCalculator.wsdl = ${cxf.it.calculator.baseUri}/calculator-ws/CalculatorService?wsdl
quarkus.cxf.client.observableCalculator.client-endpoint-url = ${cxf.it.calculator.baseUri}/calculator-ws/CalculatorService
quarkus.cxf.client.observableCalculator.service-interface = org.jboss.eap.quickstarts.wscalculator.calculator.CalculatorService
quarkus.cxf.client.observableCalculator.features=io.quarkiverse.cxf.metrics.QuarkusCxfMetricsFeature
quarkus.cxf.client.observableCalculator.metrics.enabled = true
quarkus.cxf.metrics.tags-customizers = #headerToMetricsTagsCustomizer

quarkus.cxf.client.contextPropagationCalculator.wsdl = ${cxf.it.calculator.baseUri}/calculator-ws/CalculatorService?wsdl
quarkus.cxf.client.contextPropagationCalculator.client-endpoint-url = ${cxf.it.calculator.baseUri}/calculator-ws/CalculatorService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ be overridden per client or service endpoint using the `quarkus.cxf.client."clie
*Environment variable*: `+++QUARKUS_CXF_METRICS_ENABLED_FOR+++` +
*Since Quarkus CXF*: 2.7.0

.<| [[quarkus-cxf_quarkus-cxf-metrics-tags-customizers]]`link:#quarkus-cxf_quarkus-cxf-metrics-tags-customizers[quarkus.cxf.metrics.tags-customizers]`
.<| List of ``string``
.<|

3+a|A list of xref:user-guide/configuration.adoc#beanRefs[references] to
`org.apache.cxf.metrics.micrometer.provider.TagsCustomizer` beans
that will be attached to the global metrics feature.

*Environment variable*: `+++QUARKUS_CXF_METRICS_TAGS_CUSTOMIZERS+++` +
*Since Quarkus CXF*: 3.15.0

.<| [[quarkus-cxf_quarkus-cxf-client-client-name-metrics-enabled]]`link:#quarkus-cxf_quarkus-cxf-client-client-name-metrics-enabled[quarkus.cxf.client."client-name".metrics.enabled]`
.<| `boolean`
.<| `true`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.quarkiverse.cxf.metrics;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import io.quarkiverse.cxf.EnabledFor;
import io.quarkiverse.cxf.EnabledFor.EnabledForConverter;
Expand Down Expand Up @@ -124,5 +126,16 @@ public interface GlobalMetricsConfig {
@WithDefault("both")
@WithConverter(EnabledForConverter.class)
EnabledFor enabledFor();

/**
* A list of xref:user-guide/configuration.adoc#beanRefs[references] to
* `org.apache.cxf.metrics.micrometer.provider.TagsCustomizer` beans
* that will be attached to the global metrics feature.
*
* @since 3.15.0
* @asciidoclet
*/
public Optional<List<String>> tagsCustomizers();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class MetricsCustomizer implements ClientFactoryCustomizer, EndpointFacto

@PostConstruct
void init() {
this.feature = new QuarkusCxfMetricsFeature();
this.feature = new QuarkusCxfMetricsFeature(config.metrics().tagsCustomizers());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package io.quarkiverse.cxf.metrics;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.apache.cxf.metrics.MetricsFeature;
import org.apache.cxf.metrics.micrometer.MicrometerMetricsProperties;
Expand All @@ -20,6 +21,7 @@

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Metrics;
import io.quarkiverse.cxf.CXFRuntimeUtils;

public class QuarkusCxfMetricsFeature extends MetricsFeature {

Expand All @@ -29,14 +31,32 @@ public class QuarkusCxfMetricsFeature extends MetricsFeature {
private static final TagsCustomizer faultsCustomizer = new JaxwsFaultCodeTagsCustomizer(jaxwsTags,
new JaxwsFaultCodeProvider());
private static final TimedAnnotationProvider timedAnnotationProvider = new DefaultTimedAnnotationProvider();
private static final List<TagsCustomizer> tagsCustomizers = Arrays.asList(operationsCustomizer, faultsCustomizer);
private static final TagsProvider tagsProvider = new StandardTagsProvider(new DefaultExceptionClassProvider(),
new StandardTags());
private static final MicrometerMetricsProperties micrometerMetricsProperties = new MicrometerMetricsProperties();

public QuarkusCxfMetricsFeature() {
super(new MicrometerMetricsProvider(meterRegistry, tagsProvider, tagsCustomizers, timedAnnotationProvider,
public QuarkusCxfMetricsFeature(Optional<List<String>> tagsCustomizersRefs) {
super(new MicrometerMetricsProvider(meterRegistry, tagsProvider,
joinTagsCustomizers(tagsCustomizersRefs, operationsCustomizer, faultsCustomizer), timedAnnotationProvider,
micrometerMetricsProperties));
}

private static List<TagsCustomizer> joinTagsCustomizers(Optional<List<String>> tagsCustomizersRefsOptional,
TagsCustomizer operationsCustomizer,
TagsCustomizer faultsCustomizer) {
List<String> tagsCustomizersRefs = null;
if (tagsCustomizersRefsOptional.isPresent() && (tagsCustomizersRefs = tagsCustomizersRefsOptional.get()) != null
&& !tagsCustomizersRefs.isEmpty()) {
final List<TagsCustomizer> result = new ArrayList<>(tagsCustomizersRefs.size() + 2);
result.add(operationsCustomizer);
result.add(faultsCustomizer);
for (String ref : tagsCustomizersRefs) {
result.add(CXFRuntimeUtils.getInstance(ref, true));
}
return result;
} else {
return List.of(operationsCustomizer, faultsCustomizer);
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
package io.quarkiverse.cxf.hc5.it;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.ext.Provider;

@ApplicationScoped
@Provider
public class MyContainerRequestFilter implements ContainerRequestFilter {
public class HeaderToMetricsTagRequestFilter implements ContainerRequestFilter {
@Inject
RequestScopedHeader requestScopedHeader;

@Override
public void filter(ContainerRequestContext requestContext) {
requestScopedHeader.setHeaderValue(requestContext.getHeaderString(RequestScopedHeader.header));
}

@RequestScoped
public static class RequestScopedHeader {
public static final String header = "my-header";

public String getHeaderValue() {
return headerValue;
}

public void setHeaderValue(String headerValue) {
this.headerValue = headerValue;
}

private String headerValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkiverse.cxf.hc5.it;

import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Singleton;

import org.apache.cxf.message.Exchange;
import org.apache.cxf.metrics.micrometer.provider.TagsCustomizer;

import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.quarkiverse.cxf.hc5.it.HeaderToMetricsTagRequestFilter.RequestScopedHeader;

@Singleton
@Named("headerToMetricsTagsCustomizer")
public class HeaderToMetricsTagsCustomizer implements TagsCustomizer {

@Inject
RequestScopedHeader requestScopedHeader;

@Override
public Iterable<Tag> getAdditionalTags(Exchange ex, boolean client) {
final String val = requestScopedHeader.getHeaderValue();
if (val != null) {
return Tags.of(Tag.of("my-header", val));
}
return Tags.empty();
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ quarkus.cxf.client.myCalculator.metrics.enabled = false
quarkus.cxf.client.observableCalculator.wsdl = ${cxf.it.calculator.baseUri}/calculator-ws/CalculatorService?wsdl
quarkus.cxf.client.observableCalculator.client-endpoint-url = ${cxf.it.calculator.baseUri}/calculator-ws/CalculatorService
quarkus.cxf.client.observableCalculator.service-interface = org.jboss.eap.quickstarts.wscalculator.calculator.CalculatorService
quarkus.cxf.client.observableCalculator.features=io.quarkiverse.cxf.metrics.QuarkusCxfMetricsFeature
quarkus.cxf.client.observableCalculator.metrics.enabled = true
quarkus.cxf.metrics.tags-customizers = #headerToMetricsTagsCustomizer

quarkus.cxf.client.contextPropagationCalculator.wsdl = ${cxf.it.calculator.baseUri}/calculator-ws/CalculatorService?wsdl
quarkus.cxf.client.contextPropagationCalculator.client-endpoint-url = ${cxf.it.calculator.baseUri}/calculator-ws/CalculatorService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import io.quarkiverse.cxf.hc5.it.HeaderToMetricsTagRequestFilter.RequestScopedHeader;
import io.quarkiverse.cxf.hc5.it.MultiplyingAddInterceptor.RequestScopedFactorHeader;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
Expand All @@ -29,7 +30,7 @@
class Hc5Test {

@ParameterizedTest
@ValueSource(strings = { "sync", "async", "sync-observable", "async-observable" })
@ValueSource(strings = { "sync", "async" })
void add(String syncMode) {
RestAssured.given()
.header(RequestScopedHeader.header, syncMode + "-header-value")
Expand All @@ -40,21 +41,33 @@ void add(String syncMode) {
.statusCode(200)
.body(is("11"));

if (syncMode.endsWith("-observable")) {
/* Make sure that the tagging done in MeterFilterProducer actually works */
}

final Config config = ConfigProvider.getConfig();
final String baseUri = config.getValue("cxf.it.calculator.baseUri", String.class);
final Map<String, Object> metrics = getMetrics();
@ParameterizedTest
@ValueSource(strings = { "sync-observable", "async-observable" })
void addObservable(String syncMode) {
RestAssured.given()
.header(RequestScopedHeader.header, syncMode + "-header-value")
.queryParam("a", 7)
.queryParam("b", 4)
.get("/hc5/add-" + syncMode)
.then()
.statusCode(200)
.body(is("11"));

@SuppressWarnings("unchecked")
Map<String, Object> clientRequests = (Map<String, Object>) metrics.get("cxf.client.requests");
Assertions.assertThat(clientRequests).isNotNull();
String key = "count;exception=None;faultCode=None;method=POST;my-header=" + syncMode
+ "-header-value;operation=add;outcome=SUCCESS;status=200;uri="
+ baseUri + "/calculator-ws/CalculatorService";
Assertions.assertThat((Integer) clientRequests.get(key)).isGreaterThan(0);
}
/* Make sure that the tagging done in MeterFilterProducer actually works */

final Config config = ConfigProvider.getConfig();
final String baseUri = config.getValue("cxf.it.calculator.baseUri", String.class);
final Map<String, Object> metrics = getMetrics();

@SuppressWarnings("unchecked")
Map<String, Object> clientRequests = (Map<String, Object>) metrics.get("cxf.client.requests");
Assertions.assertThat(clientRequests).isNotNull();
String key = "count;exception=None;faultCode=None;method=POST;my-header=" + syncMode
+ "-header-value;operation=add;outcome=SUCCESS;status=200;uri="
+ baseUri + "/calculator-ws/CalculatorService";
Assertions.assertThat((Integer) clientRequests.get(key)).isGreaterThan(0);
}

@ParameterizedTest
Expand Down

0 comments on commit eb553e2

Please sign in to comment.