philipnee commented on code in PR #14621: URL: https://github.com/apache/kafka/pull/14621#discussion_r1372479451
########## core/src/main/scala/kafka/metrics/ClientMetricsMetadata.scala: ########## @@ -0,0 +1,119 @@ +/** + * 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 kafka.metrics + +import kafka.metrics.ClientMetricsConfig.ClientMatchingParams._ +import kafka.network.RequestChannel +import org.apache.kafka.common.errors.InvalidConfigurationException + +import java.util.regex.{Pattern, PatternSyntaxException} +import scala.collection.mutable + +/** + * Information from the client's metadata is gathered from the client's request. + */ +object ClientMetricsMetadata { + def apply(request: RequestChannel.Request, clientInstanceId: String): ClientMetricsMetadata = { + val instance = new ClientMetricsMetadata + val ctx = request.context + val clientSoftwareName = if (ctx.clientInformation != null) ctx.clientInformation.softwareName() else "" + val clientSoftwareVersion = if (ctx.clientInformation != null) ctx.clientInformation.softwareVersion() else "" + instance.init(clientInstanceId, ctx.clientId(), clientSoftwareName, clientSoftwareVersion, + ctx.clientAddress.getHostAddress, ctx.clientAddress.getHostAddress) // TODO: Fix Port + instance + } + + def apply(clientInstanceId: String, clientId: String, clientSoftwareName: String, + clientSoftwareVersion: String, clientSourceAddress: String, clientSourcePort: String): ClientMetricsMetadata = { + val instance = new ClientMetricsMetadata + instance.init(clientInstanceId, clientId, clientSoftwareName, clientSoftwareVersion, clientSourceAddress, clientSourcePort) + instance + } + + /** + * Parses the client matching patterns and builds a map with entries that has + * (PatternName, PatternValue) as the entries. + * Ex: "VERSION=1.2.3" would be converted to a map entry of (Version, 1.2.3) + * + * NOTES: + * 1. Client match pattern splits the input into two parts separated by first + * occurrence of the character '=' + * 2. '*' is considered as invalid client match pattern + * @param patterns List of client matching pattern strings + * @return map of client matching pattern entries + */ + def parseMatchingPatterns(patterns: List[String]) : Map[String, String] = { + val patternsMap = mutable.Map[String, String]() + if (patterns != null) { + patterns.foreach(x => { + val nameValuePair = x.split("=", 2).map(x => x.trim) Review Comment: a cool thing you can do is pattern matching, you can match `x.split("=", 2).map(x => x.trim)` with a pair `(k, v)` and direct the non-matching case `case _` This is what I meant ``` pattern.split("=", 2).map(_.trim) match { case (k, v) if isValidParam(k) && validRegexPattern(v) => (k, v) case _ => throw new InvalidConfigurationException() } ``` -- 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