[GitHub] [kafka] lbownik commented on a diff in pull request #13067: KAFKA-14524: Rewrite KafkaMetricsGroup in Java

2023-02-28 Thread via GitHub


lbownik commented on code in PR #13067:
URL: https://github.com/apache/kafka/pull/13067#discussion_r1120661170


##
server-common/src/main/java/org/apache/kafka/server/metrics/KafkaMetricsGroup.java:
##
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.metrics;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import com.yammer.metrics.core.Gauge;
+import com.yammer.metrics.core.Histogram;
+import com.yammer.metrics.core.Meter;
+import com.yammer.metrics.core.MetricName;
+import com.yammer.metrics.core.Timer;
+import org.apache.kafka.common.utils.Sanitizer;
+
+public class KafkaMetricsGroup {
+private final Class klass;
+
+public KafkaMetricsGroup(Class klass) {
+this.klass = klass;
+}
+
+/**
+ * Creates a new MetricName object for gauges, meters, etc. created for 
this
+ * metrics group.
+ * @param name Descriptive name of the metric.
+ * @param tags Additional attributes which mBean will have.
+ * @return Sanitized metric name object.
+ */
+public MetricName metricName(String name, Map tags) {
+String pkg;
+if (klass.getPackage() == null) {
+pkg = "";
+} else {
+pkg = klass.getPackage().getName();
+}
+String simpleName = klass.getSimpleName().replaceAll("\\$$", "");
+return explicitMetricName(pkg, simpleName, name, tags);
+}
+
+public static MetricName explicitMetricName(String group, String typeName,
+String name, Map tags) {
+StringBuilder nameBuilder = new StringBuilder();

Review Comment:
   maybe initialize the builder with some initial calacity (default is 16 - a 
little low).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [kafka] lbownik commented on a diff in pull request #13067: KAFKA-14524: Rewrite KafkaMetricsGroup in Java

2023-01-04 Thread GitBox


lbownik commented on code in PR #13067:
URL: https://github.com/apache/kafka/pull/13067#discussion_r1061467816


##
server-common/src/main/java/org/apache/kafka/server/metrics/KafkaMetricsGroup.java:
##
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.metrics;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import com.yammer.metrics.core.Gauge;
+import com.yammer.metrics.core.Histogram;
+import com.yammer.metrics.core.Meter;
+import com.yammer.metrics.core.MetricName;
+import com.yammer.metrics.core.Timer;
+import org.apache.kafka.common.utils.Sanitizer;
+
+public class KafkaMetricsGroup {
+private final Class klass;
+
+public KafkaMetricsGroup(final Class klass) {
+this.klass = klass;
+}
+
+/**
+ * Creates a new MetricName object for gauges, meters, etc. created for 
this
+ * metrics group.
+ * @param name Descriptive name of the metric.
+ * @param tags Additional attributes which mBean will have.
+ * @return Sanitized metric name object.
+ */
+public MetricName metricName(final String name, final Map 
tags) {
+final String pkg;
+if (klass.getPackage() == null) {
+pkg = "";
+} else {
+pkg = klass.getPackage().getName();
+}
+final String simpleName = klass.getSimpleName().replaceAll("\\$$", "");
+return explicitMetricName(pkg, simpleName, name, tags);
+}
+
+public final MetricName explicitMetricName(final String group, final 
String typeName,
+   final String name, final 
Map tags) {
+final StringBuilder nameBuilder = new StringBuilder();
+nameBuilder.append(group);
+nameBuilder.append(":type=");
+nameBuilder.append(typeName);
+
+if (!name.isEmpty()) {
+nameBuilder.append(",name=");
+nameBuilder.append(name);
+}
+
+final String scope = toScope(tags).orElse(null);
+final Optional tagsName = toMBeanName(tags);
+tagsName.ifPresent(s -> nameBuilder.append(",").append(s));
+
+return new MetricName(group, typeName, name, scope, 
nameBuilder.toString());
+}
+
+public final  Gauge newGauge(final String name, final Gauge 
metric, final Map tags) {
+return KafkaYammerMetrics.defaultRegistry().newGauge(metricName(name, 
tags), metric);
+}
+
+public final  Gauge newGauge(final String name, final Gauge 
metric) {
+return newGauge(name, metric, Collections.emptyMap());
+}
+
+public final Meter newMeter(final String name, final String eventType,
+final TimeUnit timeUnit, final Map tags) {
+return KafkaYammerMetrics.defaultRegistry().newMeter(metricName(name, 
tags), eventType, timeUnit);
+}
+
+public final Meter newMeter(final String name, final String eventType,
+final TimeUnit timeUnit) {
+return newMeter(name, eventType, timeUnit, Collections.emptyMap());
+}
+
+public final Meter newMeter(final MetricName metricName, final String 
eventType, final TimeUnit timeUnit) {
+return KafkaYammerMetrics.defaultRegistry().newMeter(metricName, 
eventType, timeUnit);
+}
+
+public final Histogram newHistogram(final String name, final boolean 
biased, final Map tags) {
+return 
KafkaYammerMetrics.defaultRegistry().newHistogram(metricName(name, tags), 
biased);
+}
+
+public final Histogram newHistogram(final String name) {
+return newHistogram(name, true, Collections.emptyMap());
+}
+
+public final Histogram newHistogram(final String name, final boolean 
biased) {
+return newHistogram(name, biased, Collections.emptyMap());
+}
+
+public final Histogram newHistogram(final String name, final Map tags) {
+return newHistogram(name, true, tags);
+}
+
+public final Timer newTimer(final String name, final TimeUnit 
durationUnit, final TimeUnit rateUnit,
+final Map tag

[GitHub] [kafka] lbownik commented on a diff in pull request #13067: KAFKA-14524: Rewrite KafkaMetricsGroup in Java

2023-01-04 Thread GitBox


lbownik commented on code in PR #13067:
URL: https://github.com/apache/kafka/pull/13067#discussion_r1061466176


##
server-common/src/main/java/org/apache/kafka/server/metrics/KafkaMetricsGroup.java:
##
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.metrics;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import com.yammer.metrics.core.Gauge;
+import com.yammer.metrics.core.Histogram;
+import com.yammer.metrics.core.Meter;
+import com.yammer.metrics.core.MetricName;
+import com.yammer.metrics.core.Timer;
+import org.apache.kafka.common.utils.Sanitizer;
+
+public class KafkaMetricsGroup {
+private final Class klass;
+
+public KafkaMetricsGroup(final Class klass) {
+this.klass = klass;
+}
+
+/**
+ * Creates a new MetricName object for gauges, meters, etc. created for 
this
+ * metrics group.
+ * @param name Descriptive name of the metric.
+ * @param tags Additional attributes which mBean will have.
+ * @return Sanitized metric name object.
+ */
+public MetricName metricName(final String name, final Map 
tags) {
+final String pkg;
+if (klass.getPackage() == null) {
+pkg = "";
+} else {
+pkg = klass.getPackage().getName();
+}
+final String simpleName = klass.getSimpleName().replaceAll("\\$$", "");
+return explicitMetricName(pkg, simpleName, name, tags);
+}
+
+public final MetricName explicitMetricName(final String group, final 
String typeName,
+   final String name, final 
Map tags) {
+final StringBuilder nameBuilder = new StringBuilder();
+nameBuilder.append(group);
+nameBuilder.append(":type=");
+nameBuilder.append(typeName);
+
+if (!name.isEmpty()) {
+nameBuilder.append(",name=");
+nameBuilder.append(name);
+}
+
+final String scope = toScope(tags).orElse(null);
+final Optional tagsName = toMBeanName(tags);
+tagsName.ifPresent(s -> nameBuilder.append(",").append(s));
+
+return new MetricName(group, typeName, name, scope, 
nameBuilder.toString());
+}
+
+public final  Gauge newGauge(final String name, final Gauge 
metric, final Map tags) {
+return KafkaYammerMetrics.defaultRegistry().newGauge(metricName(name, 
tags), metric);
+}
+
+public final  Gauge newGauge(final String name, final Gauge 
metric) {
+return newGauge(name, metric, Collections.emptyMap());
+}
+
+public final Meter newMeter(final String name, final String eventType,
+final TimeUnit timeUnit, final Map tags) {
+return KafkaYammerMetrics.defaultRegistry().newMeter(metricName(name, 
tags), eventType, timeUnit);
+}
+
+public final Meter newMeter(final String name, final String eventType,
+final TimeUnit timeUnit) {
+return newMeter(name, eventType, timeUnit, Collections.emptyMap());
+}
+
+public final Meter newMeter(final MetricName metricName, final String 
eventType, final TimeUnit timeUnit) {
+return KafkaYammerMetrics.defaultRegistry().newMeter(metricName, 
eventType, timeUnit);
+}
+
+public final Histogram newHistogram(final String name, final boolean 
biased, final Map tags) {
+return 
KafkaYammerMetrics.defaultRegistry().newHistogram(metricName(name, tags), 
biased);
+}
+
+public final Histogram newHistogram(final String name) {
+return newHistogram(name, true, Collections.emptyMap());
+}
+
+public final Histogram newHistogram(final String name, final boolean 
biased) {
+return newHistogram(name, biased, Collections.emptyMap());
+}
+
+public final Histogram newHistogram(final String name, final Map tags) {
+return newHistogram(name, true, tags);
+}
+
+public final Timer newTimer(final String name, final TimeUnit 
durationUnit, final TimeUnit rateUnit,
+final Map tag

[GitHub] [kafka] lbownik commented on a diff in pull request #13067: KAFKA-14524: Rewrite KafkaMetricsGroup in Java

2023-01-04 Thread GitBox


lbownik commented on code in PR #13067:
URL: https://github.com/apache/kafka/pull/13067#discussion_r1061458021


##
server-common/src/main/java/org/apache/kafka/server/metrics/KafkaMetricsGroup.java:
##
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.metrics;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import com.yammer.metrics.core.Gauge;
+import com.yammer.metrics.core.Histogram;
+import com.yammer.metrics.core.Meter;
+import com.yammer.metrics.core.MetricName;
+import com.yammer.metrics.core.Timer;
+import org.apache.kafka.common.utils.Sanitizer;
+
+public class KafkaMetricsGroup {
+private final Class klass;
+
+public KafkaMetricsGroup(final Class klass) {
+this.klass = klass;
+}
+
+/**
+ * Creates a new MetricName object for gauges, meters, etc. created for 
this
+ * metrics group.
+ * @param name Descriptive name of the metric.
+ * @param tags Additional attributes which mBean will have.
+ * @return Sanitized metric name object.
+ */
+public MetricName metricName(final String name, final Map 
tags) {
+final String pkg;
+if (klass.getPackage() == null) {
+pkg = "";
+} else {
+pkg = klass.getPackage().getName();
+}
+final String simpleName = klass.getSimpleName().replaceAll("\\$$", "");
+return explicitMetricName(pkg, simpleName, name, tags);
+}
+
+public final MetricName explicitMetricName(final String group, final 
String typeName,
+   final String name, final 
Map tags) {
+final StringBuilder nameBuilder = new StringBuilder();

Review Comment:
   the default capacity of 16 will cause internal buffere realocation,, how 
about putting new StringBuilder(50) or  even new StringBuilder(100) here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [kafka] lbownik commented on a diff in pull request #13067: KAFKA-14524: Rewrite KafkaMetricsGroup in Java

2023-01-04 Thread GitBox


lbownik commented on code in PR #13067:
URL: https://github.com/apache/kafka/pull/13067#discussion_r1061455377


##
server-common/src/main/java/org/apache/kafka/server/metrics/KafkaMetricsGroup.java:
##
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.server.metrics;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import com.yammer.metrics.core.Gauge;
+import com.yammer.metrics.core.Histogram;
+import com.yammer.metrics.core.Meter;
+import com.yammer.metrics.core.MetricName;
+import com.yammer.metrics.core.Timer;
+import org.apache.kafka.common.utils.Sanitizer;
+
+public class KafkaMetricsGroup {
+private final Class klass;
+
+public KafkaMetricsGroup(final Class klass) {
+this.klass = klass;
+}
+
+/**
+ * Creates a new MetricName object for gauges, meters, etc. created for 
this
+ * metrics group.
+ * @param name Descriptive name of the metric.
+ * @param tags Additional attributes which mBean will have.
+ * @return Sanitized metric name object.
+ */
+public MetricName metricName(final String name, final Map 
tags) {
+final String pkg;
+if (klass.getPackage() == null) {

Review Comment:
   ternary operator would look great here



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org