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

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

问题描述

我有一个骆驼应用程序,它在路由构建器中定义了许多路由.其中一条路线有一个 xslt 管道,我想在 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.

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

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