Apache9 commented on code in PR #4691: URL: https://github.com/apache/hbase/pull/4691#discussion_r943466191
########## hbase-http/src/main/java/org/apache/hadoop/hbase/http/prometheus/PrometheusHadoop2Servlet.java: ########## @@ -0,0 +1,86 @@ +/* + * 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.hadoop.hbase.http.prometheus; + +import java.io.IOException; +import java.io.Writer; +import java.util.Collection; +import java.util.regex.Pattern; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import com.google.errorprone.annotations.RestrictedApi; +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.metrics2.AbstractMetric; +import org.apache.hadoop.metrics2.MetricType; +import org.apache.hadoop.metrics2.MetricsRecord; +import org.apache.hadoop.metrics2.MetricsTag; +import org.apache.hadoop.metrics2.impl.MetricsExportHelper; +import org.apache.yetus.audience.InterfaceAudience; + +@InterfaceAudience.Private +public class PrometheusHadoop2Servlet extends HttpServlet { + + private static final Pattern SPLIT_PATTERN = + Pattern.compile("(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=([A-Z][a-z]))|\\W|(_)+"); + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws IOException { + writeMetrics(resp.getWriter()); + } + + static String toPrometheusName(String metricRecordName, String metricName) { + String baseName = metricRecordName + StringUtils.capitalize(metricName); + String[] parts = SPLIT_PATTERN.split(baseName); + return String + .join("_", parts) + .toLowerCase(); + } + + @RestrictedApi(explanation = "Should only be called in tests", link = "", + allowedOnPath = ".*/src/test/.*") + void writeMetrics(Writer writer) throws IOException { + Collection<MetricsRecord> metricRecords = MetricsExportHelper.export(); + for (MetricsRecord metricsRecord : metricRecords) { + for (AbstractMetric metrics : metricsRecord.metrics()) { + if (metrics.type() == MetricType.COUNTER || metrics.type() == MetricType.GAUGE) { Review Comment: No problem, just want to know the reason. Then better add some references about the format spec, and also mention that the format is not hard to implement so do not want to add extra dependencies. -- 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: issues-unsubscr...@hbase.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org