-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HBASE-20904 Prometheus /metrics http endpoint for monitoring (#4691)
Co-authored-by: Luca Kovacs <[email protected]> Co-authored-by: Madhusoodan P <[email protected]> Co-authored-by: Luca Kovacs <[email protected]> Signed-off-by: Duo Zhang <[email protected]>
- Loading branch information
1 parent
a0481d1
commit f9ea7ee
Showing
9 changed files
with
392 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
hbase-hadoop-compat/src/main/java/org/apache/hadoop/metrics2/impl/MetricsExportHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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.metrics2.impl; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import org.apache.hadoop.metrics2.MetricsRecord; | ||
import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem; | ||
import org.apache.yetus.audience.InterfaceAudience; | ||
|
||
@InterfaceAudience.Private | ||
public final class MetricsExportHelper { | ||
private MetricsExportHelper() { | ||
} | ||
|
||
public static Collection<MetricsRecord> export() { | ||
MetricsSystemImpl instance = (MetricsSystemImpl) DefaultMetricsSystem.instance(); | ||
MetricsBuffer metricsBuffer = instance.sampleMetrics(); | ||
List<MetricsRecord> metrics = new ArrayList<>(); | ||
for (MetricsBuffer.Entry entry : metricsBuffer) { | ||
entry.records().forEach(metrics::add); | ||
} | ||
return metrics; | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
...-hadoop-compat/src/test/java/org/apache/hadoop/hbase/metrics/TestMetricsExportHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* 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.metrics; | ||
|
||
import java.util.Collection; | ||
import org.apache.hadoop.hbase.HBaseClassTestRule; | ||
import org.apache.hadoop.hbase.testclassification.MetricsTests; | ||
import org.apache.hadoop.hbase.testclassification.SmallTests; | ||
import org.apache.hadoop.metrics2.AbstractMetric; | ||
import org.apache.hadoop.metrics2.MetricsRecord; | ||
import org.apache.hadoop.metrics2.impl.MetricsExportHelper; | ||
import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem; | ||
import org.junit.Assert; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
|
||
@Category({ MetricsTests.class, SmallTests.class }) | ||
public class TestMetricsExportHelper { | ||
|
||
@ClassRule | ||
public static final HBaseClassTestRule CLASS_RULE = | ||
HBaseClassTestRule.forClass(TestMetricsExportHelper.class); | ||
|
||
@Test | ||
public void testExportHelper() { | ||
DefaultMetricsSystem.initialize("exportHelperTestSystem"); | ||
DefaultMetricsSystem.instance().start(); | ||
|
||
String metricsName = "exportMetricsTestGrp"; | ||
String gaugeName = "exportMetricsTestGauge"; | ||
String counterName = "exportMetricsTestCounter"; | ||
|
||
BaseSourceImpl baseSource = new BaseSourceImpl(metricsName, "", metricsName, metricsName); | ||
|
||
baseSource.setGauge(gaugeName, 0); | ||
baseSource.incCounters(counterName, 1); | ||
|
||
Collection<MetricsRecord> metrics = MetricsExportHelper.export(); | ||
DefaultMetricsSystem.instance().stop(); | ||
|
||
Assert.assertTrue(metrics.stream().anyMatch(mr -> mr.name().equals(metricsName))); | ||
Assert.assertTrue(gaugeName + " is missing in the export", | ||
contains(metrics, metricsName, gaugeName)); | ||
Assert.assertTrue(counterName + " is missing in the export", | ||
contains(metrics, metricsName, counterName)); | ||
} | ||
|
||
private boolean contains(Collection<MetricsRecord> metrics, String metricsName, | ||
String metricName) { | ||
return metrics.stream().filter(mr -> mr.name().equals(metricsName)).anyMatch(mr -> { | ||
for (AbstractMetric metric : mr.metrics()) { | ||
if (metric.name().equals(metricName)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
hbase-http/src/main/java/org/apache/hadoop/hbase/http/ServletConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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; | ||
|
||
import org.apache.yetus.audience.InterfaceAudience; | ||
|
||
/* pojo to hold the servlet info */ | ||
|
||
@InterfaceAudience.Private | ||
class ServletConfig { | ||
private String name; | ||
private String pathSpec; | ||
private String clazz; | ||
|
||
public ServletConfig(String name, String pathSpec, String clazz) { | ||
this.name = name; | ||
this.pathSpec = pathSpec; | ||
this.clazz = clazz; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getPathSpec() { | ||
return pathSpec; | ||
} | ||
|
||
public String getClazz() { | ||
return clazz; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...e-http/src/main/java/org/apache/hadoop/hbase/http/prometheus/PrometheusHadoopServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* 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 com.google.errorprone.annotations.RestrictedApi; | ||
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 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 PrometheusHadoopServlet 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(); | ||
} | ||
|
||
/* | ||
* SimpleClient for Prometheus is not used, because the format is very easy to implement and this | ||
* solution doesn't add any dependencies to the project. You can check the Prometheus format here: | ||
* https://prometheus.io/docs/instrumenting/exposition_formats/ | ||
*/ | ||
@RestrictedApi(explanation = "Should only be called in tests or self", link = "", | ||
allowedOnPath = ".*/src/test/.*|.*/PrometheusHadoopServlet\\.java") | ||
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) { | ||
|
||
String key = toPrometheusName(metricsRecord.name(), metrics.name()); | ||
writer.append("# TYPE ").append(key).append(" ") | ||
.append(metrics.type().toString().toLowerCase()).append("\n").append(key).append("{"); | ||
|
||
/* add tags */ | ||
String sep = ""; | ||
for (MetricsTag tag : metricsRecord.tags()) { | ||
String tagName = tag.name().toLowerCase(); | ||
writer.append(sep).append(tagName).append("=\"").append(tag.value()).append("\""); | ||
sep = ","; | ||
} | ||
writer.append("} "); | ||
writer.append(metrics.value().toString()).append('\n'); | ||
} | ||
} | ||
} | ||
writer.flush(); | ||
} | ||
} |
Oops, something went wrong.