admin管理员组文章数量:1432205
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
?
1 Answer
Reset to default 0It 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);
本文标签:
版权声明:本文标题:java - How to handle Prometheus metrics in class extending netty's ChannelInboundHandlerAdapter in exceptionCaught() met 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745575558a2664307.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论