219.13. MetricsMessageHistoryFactory

可从 Camel 2.17 开始

此工厂允许使用指标在路由消息期间捕获消息历史性能统计数据。它的工作原理是在所有路由中的每个节点使用指标计时器。 此工厂可以在 Java 和 XML 中使用,作为以下示例。 

从 Java 中,您要将工厂设置为 CamelContext ,如下所示:

context.setMessageHistoryFactory(new MetricsMessageHistoryFactory());

在 XML DSL 中,您将定义 <bean>,如下所示:

  <!-- use camel-metrics message history to gather metrics for all messages being routed -->
  <bean id="metricsMessageHistoryFactory" class="org.apache.camel.component.metrics.messagehistory.MetricsMessageHistoryFactory"/>

因工厂支持以下选项:

名称default描述

useJmx

false

是否使用 com.codahale.metrics.JmxReporter 向 JMX 报告精细统计。
请注意,如果在 CamelContext 上启用了 JMX,则会在 JMX 树中的服务类型下列出 MetricsRegistryService mbean。该 mbean 有一个操作来通过 json 输出统计信息。只有在每个统计类型需要精细的 mbeans 时,才需要将 useJmx 设置为 true。

jmxDomain

org.apache.camel.metrics

JMX 域名

prettyPrint

false

是否在以 json 格式输出统计信息时打印情况

metricsRegistry

 

allow 使用共享的 com.codahale.metrics.MetricRegistry。如果没有提供,Camel 将创建此 CamelContext 使用的共享实例。

rateUnit

TimeUnit.SECONDS

指标报告者中用于速率的单位,或者将统计信息转储到 json。

durationUnit

TimeUnit.MILLISECONDS

指标报告程序中的 for 持续时间或者将统计信息转储到 json 时使用的单位。

namePattern

名称.routeId.id.type

要使用的名称模式。使用点作为分隔符,但您可以修改。值 名称routeIdtypeid 和 id 将替换为实际值。其中 name 是 CamelContext 的名称。routeId 是路由的名称。id 模式表示节点 ID。type 是 history 的值。

在运行时,指标可以从 Java API 或 JMX 访问,后者可以将数据作为 json 输出来收集。

从 Java 代码中,您可以从 CamelContext 获取服务,如下所示:

MetricsMessageHistoryService service = context.hasService(MetricsMessageHistoryService.class);
String json = service.dumpStatisticsAsJson();

而 JMX API,MBean 在 type=services 树中注册, 名称为=MetricsMessageHistoryService