-
Notifications
You must be signed in to change notification settings - Fork 150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
kafka-clients-metrics: Refactor node topic metrics #2195
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...on/kafka-clients-metrics-3.0.0/src/main/java/com/nr/instrumentation/kafka/ClientType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* | ||
* * Copyright 2025 New Relic Corporation. All rights reserved. | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package com.nr.instrumentation.kafka; | ||
|
||
public enum ClientType { | ||
CONSUMER("Consume"), | ||
PRODUCER("Produce"); | ||
|
||
private final String operation; | ||
|
||
ClientType(String operation) { | ||
this.operation = operation; | ||
} | ||
|
||
public String getOperation() { | ||
return operation; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...a-clients-metrics-3.0.0/src/main/java/com/nr/instrumentation/kafka/NodeTopicRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* | ||
* * Copyright 2025 New Relic Corporation. All rights reserved. | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package com.nr.instrumentation.kafka; | ||
|
||
import org.apache.kafka.common.Node; | ||
import org.apache.kafka.common.metrics.KafkaMetric; | ||
|
||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import static com.nr.instrumentation.kafka.MetricsConstants.NODE_PREFIX; | ||
|
||
/** | ||
* This class is used to track all the metric names that are related to a specific node: | ||
* | ||
* - MessageBroker/Kafka/Nodes/host:port | ||
* - MessageBroker/Kafka/Nodes/host:port/Consume/topicName | ||
* - MessageBroker/Kafka/Nodes/host:port/Produce/topicName | ||
* | ||
* At initialization time we only have the node and the mode (is this a metrics reporter | ||
* for a Kafka consumer or for a Kafka producer?). | ||
* | ||
* Then, as topics are discovered through the metricChange method, the topic metric names are | ||
* generated. This is the best way we have to get track of the topics since they're not | ||
* available when the KafkaConsumer/KafkaProducer is initialized. | ||
* | ||
* For KafkaConsumer, the SubscriptionState doesn't contain the topics and partitions | ||
* at initialization time because it takes time for the rebalance to happen. | ||
* | ||
* For KafkaProducer, topics are dynamic since a producer could send records to any | ||
* topic and the concept of subscription doesn't exist there. | ||
* | ||
* Alternatively we could get the topics from the records in KafkaProducer.doSend or | ||
* KafkaConsumer.poll, and call NewRelicMetricsReporter.addTopicToNodeMetrics from there. | ||
* This approach would have a small impact in performance, and getting the topics from the | ||
* KafkaMetrics is a good enough solution. | ||
*/ | ||
public class NodeTopicRegistry { | ||
private final Set<String> recordedTopics = ConcurrentHashMap.newKeySet(); | ||
// contains the registered metric or event names, according to config | ||
private final Set<String> convertedNames = ConcurrentHashMap.newKeySet(); | ||
private final Set<String> nodes = new HashSet<>(); | ||
private final ClientType clientType; | ||
|
||
public NodeTopicRegistry(ClientType clientType, Collection<Node> nodes) { | ||
this.clientType = clientType; | ||
for (Node node : nodes) { | ||
String nodeName = node.host() + ":" + node.port(); | ||
this.nodes.add(nodeName); | ||
this.convertedNames.add(convertName(NODE_PREFIX + nodeName)); | ||
} | ||
} | ||
|
||
/** | ||
* @return true if the metric contains a topic and it was registered | ||
*/ | ||
public boolean register(KafkaMetric metric) { | ||
String topic = metric.metricName().tags().get("topic"); | ||
if (topic != null && recordedTopics.add(topic)) { | ||
for (String node : nodes) { | ||
String metricName = NODE_PREFIX + node + "/" + clientType.getOperation() + "/" + topic; | ||
this.convertedNames.add(convertName(metricName)); | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public Collection<String> getNodeTopicNames() { | ||
return this.convertedNames; | ||
} | ||
|
||
private String convertName(String metricName) { | ||
if (MetricsConstants.METRICS_AS_EVENTS) { | ||
return metricName.replace('/', '.'); | ||
} | ||
return metricName; | ||
} | ||
|
||
public void close() { | ||
recordedTopics.clear(); | ||
convertedNames.clear(); | ||
nodes.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This field is not altered after the constructor, so not bothering to add concurrency control to it.