如何计算阿帕奇骆驼路线的部分时间?

时间:2016-11-09 21:59:41

标签: logging apache-camel

我有一个camel应用程序,它在路由构建器中定义了许多路由。其中一个路由有一个xslt管道我想记录splunk的性能。日志的格式必须是:

PerformanceDetailResultMs=<numberOfMsTheXsltsTook>

我尝试过以下操作,因为System.currentTimeMillis()的结果是由spring保留的,因此当routeBuilder类执行时,当时提取的任何值都被保存到:

from(direct:somewhere)
    //... other things
    .setProperty(START_TIME, simple(Long.toString(System.currentTimeMillis()), Long.class))
    .to("xslt:templates/bop1.xslt?saxon=true")
    .to("xslt:templates/bop2.xslt?saxon=true")
    .to("xslt:templates/bop3.xslt?saxon=true")
    .to("xslt:templates/bop4.xslt?saxon=true")
    .to("xslt:templates/bop7.xslt?saxon=true")
    .to("xslt:templates/bop8.xslt?saxon=true")
    .to("xslt:templates/bop9.xslt?saxon=true")
    .to("xslt:templates/premGen1.xslt?saxon=true")
    .to("xslt:templates/premGen2.xslt?saxon=true")
    .setProperty(END_TIME, simple(Long.toString(System.currentTimeMillis()), Long.class))
    .log("PerformanceDetailResultMs=${exchangeProperty." + END_TIME + "} - ${exchangeProperty." + START_TIME + "}")
    //... other things

START_TIME和END_TIME变量只是私有静态字符串,希望它们可以重复使用。此代码不起作用,因为START_TIME和END_TIME在routeBuilder实例化时设置并由spring静态保持。每次通过该路线时都不会获得新的时间戳。

对操作子集进行计时的正确“驼峰方式”是什么,以便我可以输出如下日志语句:

PerformanceDetailResultMs=489234

2 个答案:

答案 0 :(得分:2)

我想到的简单事情是

.process(exchange->exchange.setProperty(START_TIME,System.currentTimeMillis()))

答案 1 :(得分:2)

如果您正在进行统计,请查看Camel Metrics component。您可以设置计时器指标,如下所示:

from("direct:somewhere")
    .to("metrics:timer:your.time?action=start")
    .to("xslt:templates/bop1.xslt?saxon=true")
    .to("metrics:timer:your.timer?action=stop");

Splunk可以解析Json,因此要提取指标,您可以设置计时器以Json格式打印:

MetricsRoutePolicyFactory factory = new MetricsRoutePolicyFactory();
factory.setPrettyPrint(true);
context.addRoutePolicyFactory(factory);

默认情况下,这会每60秒打印一次指标,但您可以更改此指标。

如果你确实需要为每条消息提供单独的日志,那么正如@Sagar建议的那样,你可以在lambda函数中设置属性。