satishd commented on code in PR #13067: URL: https://github.com/apache/kafka/pull/13067#discussion_r1125990307
########## 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<String, String> 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<String, String> tags) { + StringBuilder nameBuilder = new StringBuilder(100); + nameBuilder.append(group); + nameBuilder.append(":type="); + nameBuilder.append(typeName); + + if (!name.isEmpty()) { + nameBuilder.append(",name="); + nameBuilder.append(name); + } + + String scope = toScope(tags).orElse(null); + Optional<String> tagsName = toMBeanName(tags); + tagsName.ifPresent(s -> nameBuilder.append(",").append(s)); + + return new MetricName(group, typeName, name, scope, nameBuilder.toString()); + } + + public final <T> Gauge<T> newGauge(String name, Gauge<T> metric, Map<String, String> tags) { + return KafkaYammerMetrics.defaultRegistry().newGauge(metricName(name, tags), metric); + } + + public final <T> Gauge<T> newGauge(String name, Gauge<T> metric) { + return newGauge(name, metric, Collections.emptyMap()); + } + + public final Meter newMeter(String name, String eventType, + TimeUnit timeUnit, Map<String, String> tags) { + return KafkaYammerMetrics.defaultRegistry().newMeter(metricName(name, tags), eventType, timeUnit); + } + + public final Meter newMeter(String name, String eventType, + TimeUnit timeUnit) { + return newMeter(name, eventType, timeUnit, Collections.emptyMap()); + } + + public final Meter newMeter(MetricName metricName, String eventType, TimeUnit timeUnit) { + return KafkaYammerMetrics.defaultRegistry().newMeter(metricName, eventType, timeUnit); + } + + public final Histogram newHistogram(String name, boolean biased, Map<String, String> tags) { + return KafkaYammerMetrics.defaultRegistry().newHistogram(metricName(name, tags), biased); + } + + public final Histogram newHistogram(String name) { + return newHistogram(name, true, Collections.emptyMap()); + } + + public final Histogram newHistogram(String name, boolean biased) { Review Comment: Are there any usages for this method? We can add it later if needed. ########## core/src/main/scala/kafka/network/RequestChannel.scala: ########## @@ -497,51 +501,59 @@ object RequestMetrics { val ErrorsPerSec = "ErrorsPerSec" } -class RequestMetrics(name: String) extends KafkaMetricsGroup { +class RequestMetrics(name: String) { import RequestMetrics._ - val tags = Map("request" -> name) + private val metricsGroup = new KafkaMetricsGroup(this.getClass) + + val tags = Map("request" -> name).asJava val requestRateInternal = new Pool[Short, Meter]() // time a request spent in a request queue - val requestQueueTimeHist = newHistogram(RequestQueueTimeMs, biased = true, tags) + val requestQueueTimeHist = metricsGroup.newHistogram(RequestQueueTimeMs, true, tags) // time a request takes to be processed at the local broker - val localTimeHist = newHistogram(LocalTimeMs, biased = true, tags) + val localTimeHist = metricsGroup.newHistogram(LocalTimeMs, true, tags) // time a request takes to wait on remote brokers (currently only relevant to fetch and produce requests) - val remoteTimeHist = newHistogram(RemoteTimeMs, biased = true, tags) + val remoteTimeHist = metricsGroup.newHistogram(RemoteTimeMs, true, tags) // time a request is throttled, not part of the request processing time (throttling is done at the client level // for clients that support KIP-219 and by muting the channel for the rest) - val throttleTimeHist = newHistogram(ThrottleTimeMs, biased = true, tags) + val throttleTimeHist = metricsGroup.newHistogram(ThrottleTimeMs, true, tags) // time a response spent in a response queue - val responseQueueTimeHist = newHistogram(ResponseQueueTimeMs, biased = true, tags) + val responseQueueTimeHist = metricsGroup.newHistogram(ResponseQueueTimeMs, true, tags) // time to send the response to the requester - val responseSendTimeHist = newHistogram(ResponseSendTimeMs, biased = true, tags) - val totalTimeHist = newHistogram(TotalTimeMs, biased = true, tags) + val responseSendTimeHist = metricsGroup.newHistogram(ResponseSendTimeMs, true, tags) + val totalTimeHist = metricsGroup.newHistogram(TotalTimeMs, true, tags) // request size in bytes - val requestBytesHist = newHistogram(RequestBytes, biased = true, tags) + val requestBytesHist = metricsGroup.newHistogram(RequestBytes, true, tags) // time for message conversions (only relevant to fetch and produce requests) val messageConversionsTimeHist = if (name == ApiKeys.FETCH.name || name == ApiKeys.PRODUCE.name) - Some(newHistogram(MessageConversionsTimeMs, biased = true, tags)) + Some(metricsGroup.newHistogram(MessageConversionsTimeMs, true, tags)) else None // Temporary memory allocated for processing request (only populated for fetch and produce requests) // This shows the memory allocated for compression/conversions excluding the actual request size val tempMemoryBytesHist = if (name == ApiKeys.FETCH.name || name == ApiKeys.PRODUCE.name) - Some(newHistogram(TemporaryMemoryBytes, biased = true, tags)) + Some(metricsGroup.newHistogram(TemporaryMemoryBytes, true, tags)) else None private val errorMeters = mutable.Map[Errors, ErrorMeter]() Errors.values.foreach(error => errorMeters.put(error, new ErrorMeter(name, error))) - def requestRate(version: Short): Meter = { - requestRateInternal.getAndMaybePut(version, newMeter(RequestsPerSec, "requests", TimeUnit.SECONDS, tags + ("version" -> version.toString))) + def requestRate(version: Short): Meter = + requestRateInternal.getAndMaybePut(version, metricsGroup.newMeter(RequestsPerSec, "requests", TimeUnit.SECONDS, tagsWithVersion(version))) + + def tagsWithVersion(version: Short): java.util.Map[String, String] = { Review Comment: It can be a private method as it is accessed only with in the class. ########## 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<String, String> 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<String, String> tags) { + StringBuilder nameBuilder = new StringBuilder(100); + nameBuilder.append(group); + nameBuilder.append(":type="); + nameBuilder.append(typeName); + + if (!name.isEmpty()) { + nameBuilder.append(",name="); + nameBuilder.append(name); + } + + String scope = toScope(tags).orElse(null); + Optional<String> tagsName = toMBeanName(tags); + tagsName.ifPresent(s -> nameBuilder.append(",").append(s)); + + return new MetricName(group, typeName, name, scope, nameBuilder.toString()); + } + + public final <T> Gauge<T> newGauge(String name, Gauge<T> metric, Map<String, String> tags) { + return KafkaYammerMetrics.defaultRegistry().newGauge(metricName(name, tags), metric); + } + + public final <T> Gauge<T> newGauge(String name, Gauge<T> metric) { + return newGauge(name, metric, Collections.emptyMap()); + } + + public final Meter newMeter(String name, String eventType, + TimeUnit timeUnit, Map<String, String> tags) { + return KafkaYammerMetrics.defaultRegistry().newMeter(metricName(name, tags), eventType, timeUnit); + } + + public final Meter newMeter(String name, String eventType, + TimeUnit timeUnit) { + return newMeter(name, eventType, timeUnit, Collections.emptyMap()); + } + + public final Meter newMeter(MetricName metricName, String eventType, TimeUnit timeUnit) { + return KafkaYammerMetrics.defaultRegistry().newMeter(metricName, eventType, timeUnit); + } + + public final Histogram newHistogram(String name, boolean biased, Map<String, String> tags) { + return KafkaYammerMetrics.defaultRegistry().newHistogram(metricName(name, tags), biased); + } + + public final Histogram newHistogram(String name) { + return newHistogram(name, true, Collections.emptyMap()); + } + + public final Histogram newHistogram(String name, boolean biased) { + return newHistogram(name, biased, Collections.emptyMap()); + } + + public final Histogram newHistogram(String name, Map<String, String> tags) { Review Comment: Are there any usages for this method? We can add it later if needed. -- 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