【问题标题】:How to time portions of an apache camel route?如何对 apache 骆驼路线的部分时间进行计时?
【发布时间】:2016-11-10 19:37:47
【问题描述】:

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

【问题讨论】:

    标签: logging apache-camel


    【解决方案1】:

    如果您需要统计数据,请查看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 函数中设置该属性。

    【讨论】:

      【解决方案2】:

      我想到的简单的事情是

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-05
        • 2017-08-14
        • 1970-01-01
        • 2016-12-21
        • 1970-01-01
        • 1970-01-01
        • 2017-11-19
        • 1970-01-01
        相关资源
        最近更新 更多