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

We have a metered method legaced from ChannelInboundHandlerAdapterpublic void channelRead(ChannelHandl

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745575558a4633941.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信