-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PHOENIX-6879 Prometheus endpoint implementation #119
Open
lucakovacs
wants to merge
3
commits into
apache:master
Choose a base branch
from
lucakovacs:PHOENIX-6879
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
phoenix-queryserver/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,39 @@ | ||
/* | ||
* 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; | ||
|
||
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; | ||
} | ||
} |
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
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
57 changes: 57 additions & 0 deletions
57
...phoenix/queryserver/server/customizers/prometheus/PrometheusEndpointServerCustomizer.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,57 @@ | ||
/* | ||
* 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.phoenix.queryserver.server.customizers.prometheus; | ||
|
||
import org.apache.calcite.avatica.server.ServerCustomizer; | ||
import org.eclipse.jetty.server.Handler; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.server.handler.HandlerList; | ||
import org.eclipse.jetty.servlet.ServletContextHandler; | ||
import org.eclipse.jetty.servlet.ServletHolder; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.servlet.Servlet; | ||
import java.util.Arrays; | ||
|
||
public class PrometheusEndpointServerCustomizer implements ServerCustomizer<Server> { | ||
private static final Logger LOG = LoggerFactory.getLogger(PrometheusEndpointServerCustomizer.class); | ||
|
||
@Override | ||
public void customize(Server server) { | ||
Handler[] handlers = server.getHandlers(); | ||
if (handlers.length != 1) { | ||
LOG.warn("Observed handlers on server {}", Arrays.toString(handlers)); | ||
throw new IllegalStateException("Expected to find one handler"); | ||
} | ||
HandlerList list = (HandlerList) handlers[0]; | ||
|
||
ServletContextHandler ctx = new ServletContextHandler(); | ||
ctx.setContextPath("/prometheus"); | ||
|
||
Servlet servlet = new PrometheusHadoopServlet(); | ||
ServletHolder holder = new ServletHolder(servlet); | ||
ctx.addServlet(holder, "/"); | ||
|
||
Handler[] realHandlers = list.getChildHandlers(); | ||
Handler[] newHandlers = new Handler[realHandlers.length + 1]; | ||
newHandlers[0] = ctx; | ||
System.arraycopy(realHandlers, 0, newHandlers, 1, realHandlers.length); | ||
server.setHandler(new HandlerList(newHandlers)); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...org/apache/phoenix/queryserver/server/customizers/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,83 @@ | ||
/* | ||
* 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.phoenix.queryserver.server.customizers.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 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; | ||
|
||
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(), "true".equals(req.getParameter("description")), | ||
req.getParameter("qry")); | ||
} | ||
|
||
String toPrometheusName(String metricRecordName, String metricName) { | ||
String baseName = metricRecordName + metricName.substring(0,1).toUpperCase() + metricName.substring(1); | ||
String[] parts = SPLIT_PATTERN.split(baseName); | ||
return String.join("_", parts).toLowerCase(); | ||
} | ||
|
||
void writeMetrics(Writer writer, boolean descriptionEnabled, String queryParam) | ||
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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this method and its regexp could be called quite often: per metricRecord, per metric, per metric scrape interval (default scrape interval is 1 minute) could add to a bit of CPU time... if I am right, I suggest memoizing this function |
||
|
||
if (queryParam == null || key.contains(queryParam)) { | ||
|
||
if (descriptionEnabled) { | ||
String description = metrics.description(); | ||
if (!description.isEmpty()) writer.append("# HELP ").append(description).append('\n'); | ||
} | ||
|
||
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(); | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
...apache/phoenix/queryserver/server/customizers/prometheus/PrometheusHadoopServletTest.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,53 @@ | ||
/* | ||
* 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.phoenix.queryserver.server.customizers.prometheus; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class PrometheusHadoopServletTest { | ||
private final PrometheusHadoopServlet servlet = new PrometheusHadoopServlet(); | ||
|
||
//NullPointerException is expected because the MetricExportHelper doesn't have any metrics to export | ||
@Test(expected = NullPointerException.class) | ||
public void testPrometheusServletMetricsWriter() throws IOException { | ||
HttpServletResponse response = mock(HttpServletResponse.class); | ||
|
||
PrintWriter writer = new PrintWriter(System.out); | ||
when(response.getWriter()).thenReturn(writer); | ||
|
||
servlet.writeMetrics(response.getWriter(),false,""); | ||
} | ||
|
||
@Test | ||
public void testPrometheusNameConversion() { | ||
String metricRecordName = "TestMetricRecord"; | ||
String metricName = "TestMetricName"; | ||
|
||
Assert.assertEquals("test_metric_record_test_metric_name",servlet.toPrometheusName(metricRecordName,metricName)); | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prometheus typically expects and endpoint path to be "/metrics".
See https://prometheus.io/docs/introduction/first_steps/#:~:text=Prometheus%20expects%20metrics%20to%20be,%3A%2F%2Flocalhost%3A9090%2Fmetrics.