priyeshkaratha commented on code in PR #8995:
URL: https://github.com/apache/ozone/pull/8995#discussion_r2329303448


##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/ReconUtils.java:
##########
@@ -875,4 +886,105 @@ public static String 
constructObjectPathWithPrefix(long... ids) {
     }
     return pathBuilder.toString();
   }
+
+  private static HttpURLConnection makeHttpGetCall(String urlString) throws 
IOException {
+    Objects.requireNonNull(urlString, "urlString");
+    URL url = new URL(urlString);
+    final HttpURLConnection conn = openURLConnection(url);
+    conn.setRequestMethod("GET");
+    conn.setConnectTimeout(HTTP_TIMEOUT_MS);
+    conn.setReadTimeout(HTTP_TIMEOUT_MS);
+    conn.setRequestProperty("Accept", "application/json");
+    return conn;
+  }
+
+  private static HttpURLConnection openURLConnection(URL url) throws 
IOException {
+    final String protocol = url.getProtocol().toLowerCase(Locale.ROOT);
+    switch (protocol) {
+    case "https":
+      return (HttpsURLConnection) url.openConnection();
+    case "http":
+      return (HttpURLConnection) url.openConnection();
+    default:
+      throw new IOException("Unsupported protocol: " + protocol + " for URL: " 
+ url);
+    }
+  }
+
+  public static long getMetricsFromDatanode(DatanodeDetails datanode, String 
service, String name, String keyName)
+      throws IOException {
+    // Construct metrics URL for DataNode JMX endpoint
+    String metricsUrl = 
String.format("http://%s:%d/jmx?qry=Hadoop:service=%s,name=%s";,
+        datanode.getIpAddress(),
+        datanode.getPort(DatanodeDetails.Port.Name.HTTP).getValue(),
+        service,
+        name);
+
+    HttpURLConnection conn = makeHttpGetCall(metricsUrl);
+    try {
+      String jsonResponse = getResponseData(conn);
+      return parseMetrics(jsonResponse, name, keyName);
+    } finally {
+      try {
+        conn.disconnect();
+      } catch (Exception ignored) {
+        // no-op
+      }
+    }
+  }
+
+  private static String getResponseData(HttpURLConnection conn) throws 
IOException {
+    int code = conn.getResponseCode();
+    // 2xx: read normal body
+    if (code >= 200 && code < 300) {
+      return readStream(conn.getInputStream());
+    }
+    String err = null;
+    try {
+      if (conn.getErrorStream() != null) {
+        err = readStream(conn.getErrorStream());
+      }
+    } catch (IOException ignored) {
+      // ignore read errors on error stream
+    }
+    log.warn("HTTP {} from {}. Error body: {}", code, conn.getURL(), err);
+    return "";

Review Comment:
   parseMetrics method will handle empty string and it will return 0L as 
metrics.



-- 
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: issues-unsubscr...@ozone.apache.org

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


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

Reply via email to