Skip to content
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-18390: Use LinkedHashMap instead of Map in creating MetricName and SensorBuilder (1/N) #19300

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.kafka.common.metrics.stats.WindowedCount;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -105,7 +106,7 @@ void recordBytesFetched(String topic, int bytes) {
String name = topicBytesFetchedMetricName(topic);
maybeRecordDeprecatedBytesFetched(name, topic, bytes);

Sensor bytesFetched = new SensorBuilder(metrics, name, () -> Map.of("topic", topic))
Sensor bytesFetched = new SensorBuilder(metrics, name, () -> mkMap(mkEntry("topic", topic)))
.withAvg(metricsRegistry.topicFetchSizeAvg)
.withMax(metricsRegistry.topicFetchSizeMax)
.withMeter(metricsRegistry.topicBytesConsumedRate, metricsRegistry.topicBytesConsumedTotal)
Expand All @@ -117,7 +118,7 @@ void recordRecordsFetched(String topic, int records) {
String name = topicRecordsFetchedMetricName(topic);
maybeRecordDeprecatedRecordsFetched(name, topic, records);

Sensor recordsFetched = new SensorBuilder(metrics, name, () -> Map.of("topic", topic))
Sensor recordsFetched = new SensorBuilder(metrics, name, () -> mkMap(mkEntry("topic", topic)))
.withAvg(metricsRegistry.topicRecordsPerRequestAvg)
.withMeter(metricsRegistry.topicRecordsConsumedRate, metricsRegistry.topicRecordsConsumedTotal)
.build();
Expand Down Expand Up @@ -294,12 +295,12 @@ private MetricName deprecatedPartitionPreferredReadReplicaMetricName(TopicPartit
}

@Deprecated
static Map<String, String> topicTags(String topic) {
return Map.of("topic", topic.replace('.', '_'));
static LinkedHashMap<String, String> topicTags(String topic) {
return mkMap(mkEntry("topic", topic.replace('.', '_')));
}

@Deprecated
static Map<String, String> topicPartitionTags(TopicPartition tp) {
static LinkedHashMap<String, String> topicPartitionTags(TopicPartition tp) {
return mkMap(mkEntry("topic", tp.topic().replace('.', '_')),
mkEntry("partition", String.valueOf(tp.partition())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.kafka.common.metrics.stats.Value;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Supplier;

Expand All @@ -46,10 +47,10 @@ public class SensorBuilder {
private final Map<String, String> tags;

public SensorBuilder(Metrics metrics, String name) {
this(metrics, name, Collections::emptyMap);
this(metrics, name, LinkedHashMap::new);
}

public SensorBuilder(Metrics metrics, String name, Supplier<Map<String, String>> tagsSupplier) {
public SensorBuilder(Metrics metrics, String name, Supplier<LinkedHashMap<String, String>> tagsSupplier) {
this.metrics = metrics;
Sensor s = metrics.getSensor(name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,14 +812,15 @@ public static <K, V> Map.Entry<K, V> mkEntry(final K k, final V v) {
* @return A map
*/
@SafeVarargs
public static <K, V> Map<K, V> mkMap(final Map.Entry<K, V>... entries) {
public static <K, V> LinkedHashMap<K, V> mkMap(final Map.Entry<K, V>... entries) {
final LinkedHashMap<K, V> result = new LinkedHashMap<>();
for (final Map.Entry<K, V> entry : entries) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}


/**
* Creates a {@link Properties} from a map
*
Expand Down