You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by GitBox <gi...@apache.org> on 2020/09/04 17:58:48 UTC

[GitHub] [hadoop-ozone] avijayanhwx commented on a change in pull request #1390: HDDS-4196. Add an endpoint in Recon to query Prometheus

avijayanhwx commented on a change in pull request #1390:
URL: https://github.com/apache/hadoop-ozone/pull/1390#discussion_r483749537



##########
File path: hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/MetricsServiceProviderFactory.java
##########
@@ -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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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;
+
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.hdds.recon.ReconConfigKeys;
+import org.apache.hadoop.ozone.recon.spi.MetricsServiceProvider;
+import org.apache.hadoop.ozone.recon.spi.impl.PrometheusServiceProviderImpl;
+
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
+/**
+ * Factory class that is used to get the instance of configured Metrics Service
+ * Provider.
+ */
+@Singleton
+public class MetricsServiceProviderFactory {
+
+  private OzoneConfiguration configuration;
+  private ReconUtils reconUtils;
+
+  @Inject
+  public MetricsServiceProviderFactory(OzoneConfiguration configuration,
+                                       ReconUtils reconUtils) {
+    this.configuration = configuration;
+    this.reconUtils = reconUtils;
+  }
+
+  /**
+   * Returns the configured MetricsServiceProvider implementation (defaults
+   * to prometheus).
+   * If no metrics service providers are configured, returns null.
+   *
+   * @return MetricsServiceProvider instance that is configured.
+   */
+  public MetricsServiceProvider getMetricsServiceProvider() {
+    String prometheusEndpoint = getPrometheusEndpoint();
+    if (prometheusEndpoint != null && !prometheusEndpoint.isEmpty()) {

Review comment:
       Nit. can be StringUtils.isEmpty(prometheusEndpoint)
   Nit. Let's add a log line saying we are choosing "Prometheus" as the configured Metrics provider here or inside the constructor of PrometheusServiceProviderImpl.

##########
File path: hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/metrics/Metric.java
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.metrics;
+
+import org.apache.hadoop.hdds.annotation.InterfaceAudience;
+
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.TreeMap;
+
+/**
+ * Class for wrapping a metric response from
+ * {@link org.apache.hadoop.ozone.recon.spi.MetricsServiceProvider}.
+ */
+@InterfaceAudience.Private
+public final class Metric {
+
+  private Map<String, String> metadata;
+  private TreeMap<Double, Double> values;
+
+  public Metric(Map<String, String> metadata,
+                SortedMap<Double, Double> values) {
+    this.metadata = metadata;
+    this.values = (TreeMap<Double, Double>) values;

Review comment:
       We should do values.addAll() or Collections. unmodifiableMap() here, since the caller may mutate the passed in reference.

##########
File path: hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/metrics/Metric.java
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.metrics;
+
+import org.apache.hadoop.hdds.annotation.InterfaceAudience;
+
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.TreeMap;
+
+/**
+ * Class for wrapping a metric response from
+ * {@link org.apache.hadoop.ozone.recon.spi.MetricsServiceProvider}.
+ */
+@InterfaceAudience.Private
+public final class Metric {
+
+  private Map<String, String> metadata;
+  private TreeMap<Double, Double> values;
+
+  public Metric(Map<String, String> metadata,
+                SortedMap<Double, Double> values) {
+    this.metadata = metadata;
+    this.values = (TreeMap<Double, Double>) values;
+  }
+
+  public Map<String, String> getMetadata() {
+    return metadata;
+  }
+
+  public void setMetadata(Map<String, String> metadata) {

Review comment:
       Nit. Do we need setters in a Metric class?

##########
File path: hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/metrics/Metric.java
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.metrics;
+
+import org.apache.hadoop.hdds.annotation.InterfaceAudience;
+
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.TreeMap;
+
+/**
+ * Class for wrapping a metric response from
+ * {@link org.apache.hadoop.ozone.recon.spi.MetricsServiceProvider}.
+ */
+@InterfaceAudience.Private
+public final class Metric {
+
+  private Map<String, String> metadata;
+  private TreeMap<Double, Double> values;

Review comment:
       Nit. Can be final fields.

##########
File path: hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/ReconConstants.java
##########
@@ -52,6 +52,9 @@ private ReconConstants() {
   public static final String RECON_QUERY_BUCKET = "bucket";
   public static final String RECON_QUERY_FILE_SIZE = "fileSize";
 
+  public static final String PROMETHEUS_INSTANT_QUERY_API = "query";

Review comment:
       Nit. These constants can go inside PrometheusServiceProviderImpl.

##########
File path: hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/api/MetricsProxyEndpoint.java
##########
@@ -0,0 +1,117 @@
+/*
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.api;
+
+import org.apache.hadoop.ozone.recon.MetricsServiceProviderFactory;
+import org.apache.hadoop.ozone.recon.spi.MetricsServiceProvider;
+import org.apache.hadoop.ozone.recon.spi.impl.PrometheusServiceProviderImpl;
+
+import javax.inject.Inject;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.DefaultValue;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
+
+import static org.apache.hadoop.ozone.recon.ReconConstants.PROMETHEUS_INSTANT_QUERY_API;
+
+/**
+ * Endpoint to fetch metrics data from Prometheus HTTP endpoint.
+ */
+@Path("/metrics")
+public class MetricsProxyEndpoint {
+
+  private MetricsServiceProvider metricsServiceProvider;
+
+  @Inject
+  public MetricsProxyEndpoint(
+      MetricsServiceProviderFactory metricsServiceProviderFactory) {
+    this.metricsServiceProvider =
+        metricsServiceProviderFactory.getMetricsServiceProvider();
+  }
+
+  /**
+   * Return a response from the configured metrics endpoint.
+   */
+  @GET
+  @Produces(MediaType.APPLICATION_JSON)
+  @Path("/{api}")
+  public void getMetricsResponse(
+      @DefaultValue (PROMETHEUS_INSTANT_QUERY_API) @PathParam("api") String api,
+      @Context UriInfo uriInfo,
+      @Context HttpServletResponse httpServletResponse
+  ) throws Exception {
+    if (metricsServiceProvider != null) {
+      HttpURLConnection connection = metricsServiceProvider.getMetricsResponse(
+          api, uriInfo.getRequestUri().getQuery());
+      InputStream inputStream;
+      if (Response.Status.fromStatusCode(connection.getResponseCode())
+          .getFamily() == Response.Status.Family.SUCCESSFUL) {
+        inputStream = connection.getInputStream();
+      } else {
+        // Throw a bad gateway error if HttpResponseCode is not 2xx
+        httpServletResponse.setStatus(HttpServletResponse.SC_BAD_GATEWAY);
+        inputStream = connection.getErrorStream();
+      }
+      try (
+          OutputStream outputStream =
+              httpServletResponse.getOutputStream();
+          ReadableByteChannel inputChannel =
+              Channels.newChannel(inputStream);
+          WritableByteChannel outputChannel =
+              Channels.newChannel(outputStream)
+        ) {
+        final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
+
+        while(inputChannel.read(buffer) != -1) {
+          buffer.flip();
+          outputChannel.write(buffer);
+          buffer.compact();
+        }
+
+        buffer.flip();
+
+        while(buffer.hasRemaining()) {
+          outputChannel.write(buffer);
+        }
+      } finally {
+        inputStream.close();
+      }
+    } else {
+      // Throw a Bad Gateway Error
+      httpServletResponse.sendError(HttpServletResponse.SC_BAD_GATEWAY,
+          "Metrics endpoint is not configured. Configure " +
+              PrometheusServiceProviderImpl.getEndpointConfigKey() + " and " +

Review comment:
       Nit. getEndpointConfigKey() can be a method on the MetricsServiceProvider interface.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: ozone-issues-unsubscribe@hadoop.apache.org
For additional commands, e-mail: ozone-issues-help@hadoop.apache.org