Skip to content

Commit

Permalink
Workaround AdviceWithRouteBuilder and MicroprofileMetrics conflict
Browse files Browse the repository at this point in the history
Relates #1894
  • Loading branch information
jamesnetherton committed Oct 14, 2020
1 parent c5e3e0c commit 4517ace
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public MicroProfileMetricsContextCustomizer(CamelMicroProfileMetricsConfig confi
@Override
public void customize(CamelContext camelContext) {
if (config.enableRoutePolicy) {
camelContext.addRoutePolicyFactory(new MicroProfileMetricsRoutePolicyFactory());
MicroProfileMetricsRoutePolicyFactory routePolicyFactory = new MicroProfileMetricsRoutePolicyFactory();
// TODO: Remove this after upgrade to Camel >= 3.6.0
// https://github.com/apache/camel-quarkus/issues/1894
routePolicyFactory.setMetricRegistry(MetricRegistries.get(MetricRegistry.Type.APPLICATION));
camelContext.addRoutePolicyFactory(routePolicyFactory);
}

ManagementStrategy managementStrategy = camelContext.getManagementStrategy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.AdviceWithRouteBuilder;

@Path("/microprofile-metrics")
@ApplicationScoped
Expand All @@ -32,6 +34,9 @@ public class MicroProfileMetricsResource {
@Inject
ProducerTemplate template;

@Inject
CamelContext context;

@Path("/counter")
@GET
public Response counterIncrement() throws Exception {
Expand Down Expand Up @@ -87,4 +92,13 @@ public Response logMessage() throws Exception {
template.sendBody("log:message", "Test log message");
return Response.ok().build();
}

@Path("/advicewith")
@GET
public Response adviceWith() throws Exception {
AdviceWithRouteBuilder.adviceWith(context, "log", advisor -> {
advisor.replaceFromWith("direct:replaced");
});
return Response.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,8 @@ public void configure() {
.to("microprofile-metrics:timer:camel-quarkus-timer?action=start")
.delay(100)
.to("microprofile-metrics:timer:camel-quarkus-timer?action=stop");

from("direct:log").routeId("log")
.log("Camel Quarkus MicroProfile Metrics");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ public void testMicroProfileMetricsCamelContextEventNotifier() {
assertTrue(getMetricIntValue("camel.context.uptime") > 0);
}

@Test
public void testAdviceWith() {
RestAssured.get("/microprofile-metrics/advicewith")
.then()
.statusCode(200);
assertTrue(getMetricIntValue("camel.route.count") >= 7);
assertTrue(getMetricIntValue("camel.route.running.count") >= 7);
}

private int getMetricIntValue(String metricName, String... tags) {
return getApplicationMetrics().getInt(sanitizeMetricName(metricName, tags));
}
Expand All @@ -139,10 +148,6 @@ private float getMetricFloatValue(String metricName, String... tags) {
return getApplicationMetrics().getFloat(sanitizeMetricName(metricName, tags));
}

private Map<String, Object> getMetricMapValue(String metricName, String... tags) {
return getApplicationMetrics().getMap(sanitizeMetricName(metricName, tags));
}

private String sanitizeMetricName(String metricName, String... tags) {
if (tags.length == 0) {
tags = new String[] { CAMEL_CONTEXT_METRIC_TAG };
Expand Down

0 comments on commit 4517ace

Please sign in to comment.