1

In Metrics component of Apache Camel 2.14, I'm trying to set the name of counters using Simple Expression Language but it seems that the metrics component is just not using it.

I've tried :

from("direct:foo").routeId("routeFoo")
    .to("metrics:counter:${id}")

And

from("direct:foo").routeId("routeFoo")
    .to("metrics:counter:"+ simple("${id}"))

But in both cases, the name of the counter is set to ${id}.

Is there anyway to use an expression language in this components URI ?

Thanks

EDIT :

Adding a header do the job :

.setHeader(MetricsConstants.HEADER_METRIC_NAME, simple("${id}.${header.operationName}"))

But it would be more convenient to user SEL directly in URI.

Thomas
  • 1,410
  • 10
  • 24

2 Answers2

5

Check out the Camel documentation for the Simple language. On there, you'll see that ${id} refers to the message id, while ${routeId} refers to the route id.

Edit: I see that I misunderstood your original concern--not what the correct variable is for the route id, but rather how to inject that into the to.

The basic issue you're having is that you're attempting to perform a dynamic route with the to. You are attempting to concatenate Strings to compose your endpoint, but this concatenation is evaluated when the configure method is called, not when your route is executed.

Well, Camel does have a couple of mechanisms for performing dynamic routes, like recipient list, dynamic Router, or routing slip.

For example, you might use Recipient List to get a route like:

from("direct:foo").routeId("routeFoo").recipientList(simple("metrics:counter:${routeId}"));

But if you are concerned that setting a header is too much, it isn't clear to me that these mechanisms are much simpler.

Ray
  • 4,829
  • 4
  • 28
  • 55
  • Ok but it changes nothing as the problem is that the value is not evaluated – Thomas Oct 14 '14 at 18:57
  • It looks like I misunderstood your question. I've added to my answer regarding dynamic routing. – Ray Oct 14 '14 at 19:08
  • Just wanted to follow up--any luck with these approaches? – Ray Oct 15 '14 at 14:01
  • Sorry finaly I did not have time to test. It is done now and it works perfectly ! I prefer this solution than the header one. Thanks again ! (i tried with recipientLIst) – Thomas Oct 17 '14 at 12:24
1

An updated answer for Camel 2.16

from("direct:foo").routeId("routeFoo").toD("metrics:counter:${routeId}");

http://camel.apache.org/message-endpoint.html#MessageEndpoint-DynamicTo

Snekse
  • 15,474
  • 10
  • 62
  • 77