Available as of Camel 2.14
The metrics: component allows to collect various metrics directly from Camel routes. Supported metric types are counter, histogram, meter and timer. Metrics provides simple way to measure behaviour of application. Configurable reporting backend is enabling different integration options for collecting and visualizing statistics. The component also provides a
MetricsRoutePolicyFactory which allows to expose route statistics using codehale metrics, see bottom of page for details.
Maven users will need to add the following dependency to their
pom.xml for this component:
Camel Metrics Component uses by default
Slf4jReporter and 60 second reporting interval. Default registry can be replaced with custom one by providing bean with name
metricRegistry in Camel registry. For example using Spring Java Configuration.
MetricRegistry uses internal thread(s) for reporting. There is no public API in version 3.0.1 for users to clean up on exit. Thus using Camel Metrics Component leads to Java classloader leak and my cause
OutOfMemoryErrors in some cases.
Metric name defined in URI can be overridden by using header with name
will update counter with name
new.name instead of
All Metrics specific headers are removed from the message once Metrics endpoint finishes processing of exchange. While processing exchange Metrics endpoint will catch all exceptions and write log entry using level
Metrics type counter
|increment||-||Long value to add to the counter|
|decrement||-||Long value to subtract from the counter|
decrement is defined then counter value will be incremented by one. If
decrement are both defined only increment operation is called.
Message headers can be used to override
decrement values specified in Metrics component URI.
|CamelMetricsCounterIncrement||Override increment value in URI||Long|
|CamelMetricsCounterDecrement||Override decrement value in URI||Long|
Metric type histogram
|value||-||Value to use in histogram|
value is not set nothing is added to histogram and warning is logged.
Message header can be used to override value specified in Metrics component URI.
|CamelMetricsHistogramValue||Override histogram value in URI||Long|
Metric type meter
|mark||-||Long value to use as mark|
mark is not set then
meter.mark() is called without argument.
Message header can be used to override
mark value specified in Metrics component URI.
|CamelMetricsMeterMark||Override mark value in URI||Long|
Metrics type timer
|action||-||start or stop|
action or invalid value is provided then warning is logged without any timer update. If action
start is called on already running timer or
stop is called on not running timer then nothing is updated and warning is logged.
TimerContext objects are stored as Exchange properties between different Metrics component calls.
Message header can be used to override action value specified in Metrics component URI.
|CamelMetricsTimerAction||Override timer action in URI|
This factory allows to add a RoutePolicy for each route which exposes route utilization statistics using codehale metrics. This factory can be used in Java and XML as the examples below demonstrates.
Instead of using the MetricsRoutePolicyFactory you can define a MetricsRoutePolicy per route you want to instrument, in case you only want to instrument a few selected routes.
From Java you just add the factory to the
CamelContext as shown below:
And from XML DSL you define a <bean> as follows:
MetricsRoutePolicy supports the following options:
|useJmx||false||Whether to report fine grained statistics to JMX by using the |
Notice that if JMX is enabled on CamelContext then a
|jmxDomain||org.apache.camel.metrics||The JMX domain name|
|prettyPrint||false||Whether to use pretty print when outputting statistics in json format|
|metricsRegistry||Allow to use a shared |
|rateUnit||TimeUnit.SECONDS||The unit to use for rate in the metrics reporter or when dumping the statistics as json.|
|durationUnit||TimeUnit.MILLISECONDS||The unit to use for duration in the metrics reporter or when dumping the statistics as json.|
From Java code tou can get hold of the
com.codahale.metrics.MetricRegistry from the
org.apache.camel.component.metrics.routepolicy.MetricsRegistryService as shown below: