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


##########
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 "";
+  }
+
+  /** Small utility to read an entire stream as a UTF-8 String. */
+  private static String readStream(java.io.InputStream in) throws IOException {
+    StringBuilder sb = new StringBuilder();
+    try (BufferedReader br = new BufferedReader(new InputStreamReader(in, 
StandardCharsets.UTF_8))) {
+      String line;
+      while ((line = br.readLine()) != null) {
+        sb.append(line).append('\n');
+      }
+    }
+    return sb.toString();
+  }
+
+  private static long parseMetrics(String jsonResponse, String serviceName, 
String keyName) {
+    if (jsonResponse == null || jsonResponse.isEmpty()) {

Review Comment:
   Yes, it makes sense.



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

Reply via email to