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 21:23:53 UTC

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

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



##########
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:
       @avijayanhwx I thought about keeping this method as part of the interface. But, then to construct this error message, we will have to instantiate `PrometheusServiceProviderImpl` to get endpoint config key. This is because, `metricsServiceProviderFactory.getMetricsServiceProvider()` will return null when no metrics service service providers are configured.




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