adoroszlai commented on code in PR #9413: URL: https://github.com/apache/ozone/pull/9413#discussion_r2626904236
########## hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/spi/impl/JmxServiceProviderImpl.java: ########## @@ -0,0 +1,118 @@ +/* + * 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.ozone.recon.spi.impl; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import javax.inject.Singleton; +import javax.ws.rs.core.Response; +import org.apache.hadoop.hdfs.web.URLConnectionFactory; +import org.apache.hadoop.ozone.recon.ReconUtils; +import org.apache.hadoop.ozone.recon.metrics.Metric; +import org.apache.hadoop.ozone.recon.spi.MetricsServiceProvider; + +/** + * Implementation of the Jmx Metrics Service provider. + */ +@Singleton +public class JmxServiceProviderImpl implements MetricsServiceProvider { + + public static final String JMX_INSTANT_QUERY_API = "qry"; + private URLConnectionFactory connectionFactory; + private final String jmxEndpoint; + private ReconUtils reconUtils; + + public JmxServiceProviderImpl( + ReconUtils reconUtils, + String jmxEndpoint, + URLConnectionFactory connectionFactory) { + // Remove the trailing slash from endpoint url. + if (jmxEndpoint != null && jmxEndpoint.endsWith("/")) { + jmxEndpoint = jmxEndpoint.substring(0, jmxEndpoint.length() - 1); + } + this.jmxEndpoint = jmxEndpoint; + this.reconUtils = reconUtils; + this.connectionFactory = connectionFactory; + } + + /** + * Returns {@link HttpURLConnection} after querying Metrics endpoint for the + * given metric. + * + * @param api api. + * @param queryString query string with metric name and other filters. + * @return HttpURLConnection + * @throws Exception exception + */ + @Override + public HttpURLConnection getMetricsResponse(String api, String queryString) + throws Exception { + String url = String.format("%s?%s=%s", jmxEndpoint, api, + queryString); + return reconUtils.makeHttpCall(connectionFactory, + url, false); + } + + @Override + public List<Metric> getMetricsInstant(String queryString) throws Exception { + return Collections.emptyList(); + } + + /** + * Returns a list of {@link Metric} for the given instant query. + * + * @param queryString query string with metric name and other filters. + * @return List of Json map of metrics response. + * @throws Exception exception + */ + @Override + public List<Map<String, Object>> getMetrics(String queryString) + throws Exception { + return getMetrics(JMX_INSTANT_QUERY_API, queryString); + } + + /** + * Returns a list of {@link Metric} for the given api and query string. + * + * @param api api + * @param queryString query string with metric name and other filters. + * @return List of Json map of metrics response. + * @throws Exception + */ + private List<Map<String, Object>> getMetrics(String api, String queryString) + throws Exception { + HttpURLConnection urlConnection = + getMetricsResponse(api, queryString); + if (Response.Status.fromStatusCode(urlConnection.getResponseCode()) + .getFamily() == Response.Status.Family.SUCCESSFUL) { + InputStream inputStream = urlConnection.getInputStream(); + ObjectMapper mapper = new ObjectMapper(); + Map<String, Object> jsonMap = mapper.readValue(inputStream, Map.class); + inputStream.close(); Review Comment: Please: - wrap in try-with-resources - use `JsonUtils` to reuse existing `ObjectMapper` instance -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
