最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How to handle Prometheus metrics in class extending netty's ChannelInboundHandlerAdapter in exceptionCaught() met

programmeradmin1浏览0评论

We have a metered method legaced from ChannelInboundHandlerAdapter

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
...
//where we do our metering part here:
            meterRegistry.timer(
                    "processing.handlermand.duration",
                    "command_name", commandName,
                    "success", Boolean.toString(true)
            ).record(duration, java.util.concurrent.TimeUnit.MILLISECONDS);

...

}

however, if there is an exception before this metering has happened, the

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
...
}

is thrown and we can't pass the same values we have initialized in channelRead to put metrics on the same page with attributes channel and duration, but just mark them with homegrown attribute success=false.

How to pass the values between channelRead and exceptionCaught?

We have a metered method legaced from ChannelInboundHandlerAdapter

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
...
//where we do our metering part here:
            meterRegistry.timer(
                    "processing.handlermand.duration",
                    "command_name", commandName,
                    "success", Boolean.toString(true)
            ).record(duration, java.util.concurrent.TimeUnit.MILLISECONDS);

...

}

however, if there is an exception before this metering has happened, the

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
...
}

is thrown and we can't pass the same values we have initialized in channelRead to put metrics on the same page with attributes channel and duration, but just mark them with homegrown attribute success=false.

How to pass the values between channelRead and exceptionCaught?

Share Improve this question asked Nov 19, 2024 at 8:29 EljahEljah 5,2757 gold badges60 silver badges110 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It is possible to pass the attributes to the channel, String or Integer, they can be used for that.

Static initialization of the class:

private static final AttributeKey<Long> START_TIME_KEY = AttributeKey.valueOf("startTime");
private static final AttributeKey<String> COMMAND_NAME_KEY = AttributeKey.valueOf("commandName");

In channelRead():

    ctx.channel().attr(START_TIME_KEY).set(System.currentTimeMillis());
    String commandName = "unknown";
    ctx.channel().attr(COMMAND_NAME_KEY).set(commandName);

In exceptionCaught():

    long duration = System.currentTimeMillis() -  ctx.channel().attr(START_TIME_KEY).get();
    meterRegistry.timer(
            "processing.handlermand.duration",
            "command_name",ctx.channel().attr(COMMAND_NAME_KEY).get(),
            "success", Boolean.toString(false)
    ).record(duration, java.util.concurrent.TimeUnit.MILLISECONDS);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论