This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • Metrics Component
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

Metrics Component

Available as of Camel 2.14

The metrics: component allows to collect various metrics directly from Camel routes. Supported metric types are counterhistogram, meter and timerMetrics 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:

    <!-- use the same version as your Camel core version -->

URI format

metrics:[ meter | counter | histogram | timer ]:metricname[?options]

Metric Registry

Camel Metrics Component uses by default MetricRegistry with 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.

public static class MyConfig extends SingleRouteCamelConfiguration {

    public RouteBuilder route() {
        return new RouteBuilder() {
            public void configure() throws Exception {
                // define Camel routes here

    @Bean(name = MetricsComponent.METRIC_REGISTRY_NAME)
    public MetricRegistry getMetricRegistry() {
        MetricRegistry registry = ...;
        return registry;

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.


Each metric has type and name. Supported types are counter, histogrammeter and timer. Metric name is simple string. If metric type is not provided then type meter is used by default.


Metric name defined in URI can be overridden by using header with name CamelMetricsName.

For example

    .setHeader(MetricsConstants.HEADER_METRIC_NAME, constant(""))

will update counter with name instead of name.not.used.

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 warn.

Metrics type counter



increment -Long value to add to the counter
decrement-Long value to subtract from the counter

If neither increment or decrement is defined then counter value will be incremented by one. If increment and decrement are both defined only increment operation is called. 

// update counter simple.counter by 7
// increment counter simple.counter by 1
// decrement counter simple.counter by 3


Message headers can be used to override increment and decrement values specified in Metrics component URI.

NameDescriptionExpected type
CamelMetricsCounterIncrement Override increment value in URILong
CamelMetricsCounterDecrement Override decrement value in URILong
// update counter simple.counter by 417
    .setHeader(MetricsConstants.HEADER_COUNTER_INCREMENT, constant(417L))
// updates counter using simple language to evaluate body.length
    .setHeader(MetricsConstants.HEADER_COUNTER_INCREMENT, simple("${body.length}"))

Metric type histogram



value-Value to use in histogram

If no value is not set nothing is added to histogram and warning is logged.

// adds value 9923 to simple.histogram
// nothing is added to simple.histogram; warning is logged


Message header can be used to override value specified in Metrics component URI.

NameDescriptionExpected type
CamelMetricsHistogramValueOverride histogram value in URILong
// adds value 992 to simple.histogram
    .setHeader(MetricsConstants.HEADER_HISTOGRAM_VALUE, constant(992L))

Metric type meter



mark -Long value to use as mark

If mark is not set then meter.mark() is called without argument.

// marks simple.meter without value
// marks simple.meter with value 81


Message header can be used to override mark value specified in Metrics component URI.

NameDescriptionExpected type
CamelMetricsMeterMarkOverride mark value in URILong
// updates meter simple.meter with value 345
    .setHeader(MetricsConstants.HEADER_METER_MARK, constant(345L))

Metrics type timer



action-start or stop

If no 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.

// measure time taken by route "calculate"

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.

NameDescriptionExpected type
CamelMetricsTimerActionOverride timer action in URIorg.apache.camel.component.metrics.timer.TimerEndpoint.TimerAction
// sets timer action using header
    .setHeader(MetricsConstants.HEADER_TIMER_ACTION, TimerAction.start)



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:

context.addRoutePolicyFactory(new MetricsRoutePolicyFactory());

And from XML DSL you define a <bean> as follows:

  <!-- use camel-metrics route policy to gather metrics for all routes -->
  <bean id="metricsRoutePolicyFactory" class="org.apache.camel.component.metrics.routepolicy.MetricsRoutePolicyFactory"/>

The MetricsRoutePolicyFactory and MetricsRoutePolicy supports the following options:

useJmxfalseWhether to report fine grained statistics to JMX by using the com.codahale.metrics.JmxReporter.
Notice that if JMX is enabled on CamelContext then a MetricsRegistryService mbean is enlisted under the services type in the JMX tree. That mbean has a single operation to output the statistics using json. Setting useJmx to true is only needed if you want fine grained mbeans per statistics type.
jmxDomainorg.apache.camel.metricsThe JMX domain name
prettyPrintfalseWhether to use pretty print when outputting statistics in json format
metricsRegistry Allow to use a shared com.codahale.metrics.MetricRegistry. If none is provided then Camel will create a shared instance used by the this CamelContext.
rateUnitTimeUnit.SECONDSThe unit to use for rate in the metrics reporter or when dumping the statistics as json.
durationUnitTimeUnit.MILLISECONDSThe unit to use for duration in the metrics reporter or when dumping the statistics as json.
namePattern##name##.##routeId##.##type##Camel 2.17: The name pattern to use. Uses dot as separators, but you can change that. The values ##name##, ##routeId##, and ##type## will be replaced with actual value.


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:

MetricRegistryService registryService = context.hasService(MetricsRegistryService.class);
if (registryService != null) {
  MetricsRegistry registry = registryService.getMetricsRegistry();
  • No labels