[
https://issues.apache.org/jira/browse/KAFKA-19543?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18011088#comment-18011088
]
Matthias J. Sax edited comment on KAFKA-19543 at 7/31/25 6:02 AM:
------------------------------------------------------------------
Maybe I misunderstand the ticket description? – I thought that it would be ok /
expected, that:
{code:java}
consumer.metrics(); // return a Map of all metrics
consumer.close();
consumer.metrics(); // return an empty Map as all metrics got
de-registered{code}
The other question you ask, if the metrics should be a snapshot vs dynamic seem
to be an orthogonal question? And I think, yes, the metrics (ie, their values)
should be dynamic, and reflect ongoing changes.
But re-reading the ticket it seem that the issue is as follows:
{code:java}
Map metrics = consumer.metrics();
consumer.close();
// the previously returned `metrics` Map lost some keys?{code}
This would indeed be odd. After consumer.close() I would only expect that the
metric values don't change any longer, but not that whole entries disappear.
Should be an easy fix though, to make a "deep copy" of the metric Map? At least
this is what we do in Kafka Streams.
On the other hand, it seems there is two "types" of metrics: ones that are
always present (like `time-between-poll-avg`) vs more dynamic ones like
`record-lag` which depend on the currently assigned partitions, and might be
added/removed during a rebalance? These changes would not be reflected if we
make a deep copy of the metric Map. Following this train of though, it would
make even sense if a previously returned metrics Map becomes empty on
`consumer.close()` as all metrics get de-registered.
Not sure what the intended / best behavior should be.
was (Author: mjsax):
Maybe I misunderstand the ticket description? – I thought that it would be ok /
expected, that:
{code:java}
consumer.metrics(); // return a Map of all metrics
consumer.close();
consumer.metrics(); // return an empty Map as all metrics got
de-registered{code}
The other question you ask, if the metrics should be a snapshot vs dynamic seem
to be an orthogonal question? And I think, yes, the metrics (ie, their values)
should be dynamic, and reflect ongoing changes.
But re-reading the ticket it seem that the issue is as follows:
{code:java}
Map metrics = consumer.metrics();
consumer.close();
// the previously returned `metrics` Map lost some keys?{code}
This would indeed be odd. After consumer.close() I would only expect that the
metric values don't change any longer, but not that whole entries disappear.
Should be an easy fix though, to make a "deep copy" of the metric Map? At least
this is what we do in Kafka Streams.
> The set of metrics returned from Consumer.metrics() changes after
> Consumer.close()
> ----------------------------------------------------------------------------------
>
> Key: KAFKA-19543
> URL: https://issues.apache.org/jira/browse/KAFKA-19543
> Project: Kafka
> Issue Type: Bug
> Components: clients, consumer, metrics
> Affects Versions: 4.1.0
> Reporter: Kirk True
> Assignee: Kirk True
> Priority: Minor
>
> metrics() reflects the current state, so if you get the metrics before you
> close and then look at them afterward, some of the metrics are missing
> because some were removed.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)