如何计时阿帕奇骆驼路线的各个部分? [英] How to time portions of an apache camel route?

查看:106
本文介绍了如何计时阿帕奇骆驼路线的各个部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个骆驼应用程序,该应用程序具有在路由构建器中定义的许多路由.其中一条路由具有xslt管道,我想记录in splunk的性能.日志格式必须为:

I have a camel application that has many routes defined in a route builder. One of the routes has an xslt pipeline I would like to log the performance of in splunk. The format of the log needs to be:

PerformanceDetailResultMs=<numberOfMsTheXsltsTook>

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

I have tried doing the following which does not work because the result of System.currentTimeMillis() is held onto by spring and thus when the routeBuilder class executes whatever values were extracted at that time are held onto:

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之前静态保持.您不会在每次通过路线时获得新的时间戳.

The START_TIME and END_TIME variables are just private static strings in hopes they could be re-used. This code does not work because the START_TIME and END_TIME are set on routeBuilder instantiation and held onto statically by spring. You do not get new timestamps every time through the route.

对操作子集计时的正确驼峰方式"是什么,这样我就可以输出如下日志语句:

What is the proper "camel way" of timing a subset of operations such that I can output a log statement like:

PerformanceDetailResultMs=489234

推荐答案

如果需要统计信息,请查看.您可以像这样设置计时器指标:

If you're after statistics, look at the Camel Metrics component. You can setup a timer metric like so:

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格式打印:

Splunk can parse Json, so to pull out metrics, you can setup the timer to print in Json format:

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

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

By default, this prints out metrics every 60 seconds, but you can change this.

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

If you do however require individual logs for each message, then as @Sagar suggests, you could set the property in a lambda function.

这篇关于如何计时阿帕奇骆驼路线的各个部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆