kafka-clients-metrics: Refactor node topic metrics #2195
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
The previous code executed more verifications than needed for its functionality, it also stored duplicate data for those verifications. This PR simplifies the implementation by reducing the number of verifications and data stored.
Functionality
The functionality being altered is to generate metrics (or event attributes) with the format:
For each node a client connects to, a metric of the first format will be created. Then for the cartesian product of the sets of node x topic, a metric of the second format will be created.
So, for a producer client connecting to nodes (n1, n2) and producing for the topics (t1, t2), the following metrics will be created:
Current implementation
For each Kafka client, a
NewRelicMetricsReporter
is instantiated. On its constructor, it creates a map with oneNodeMetricNames
(value) for each node (key) a client connects to. (The key is never used anywhere in the code. All references for this map have a.value()
call after it).When
NodeMetricNames
is instantiated, it creates a metric with format 1 for its node.When a metric is registered on
NewRelicMetricsReporter
, if it has a topic, iterate thru theNodeMetricNames
registering that topic.In
NodeMetricNames
if the topic was not seen before, create a metric name (and an event attribute) with the format 2 and mark the topic as seen.So for each topic,
<node count>
checks are made to create the metric with format 2, also<node count>
sets are kept to keep the seen topics. Since a new topic will be added to all nodes, there is no reason to make the check for each node, nor keep a set of topics for each node.Another difference of the new implementation is that it only keeps metric names or event attribute labels instead of both. Since for an agent run only one of either will ever be used, this will save a little memory.
New implementation
A new class
NodeTopicRegistry
was created. It's functionality is similar toNodeMetricNames
, but the newer keeps track of all nodes and topics, so only one instance of it is created for eachNewRelicMetricsReporter
.When
NodeTopicRegistry
is instantiated, it receives all the nodes the Kafka client connects to, and creates all the metric names of format 1.When a metric is registered with the
NewRelicMetricsReporter
, it will be passed to theNodeTopicRegistry
and if that topic was not seen before, new metrics of format 2 will be created with the new topic for each node.So only one check for each topic is done, as well as only one collection of seen topics is kept.
Related Github Issue
This change was made while adding the same functionality for the
kafka-clients-node-metrics-x.y
(#2186)