This is an automated email from the ASF dual-hosted git repository.

adoroszlai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 1585f660c1 HDDS-7250. Add HttpServer metrics (#3772)
1585f660c1 is described below

commit 1585f660c126d31e22c45a04ce18b89126a3daff
Author: XiChen <[email protected]>
AuthorDate: Thu Oct 6 03:39:32 2022 +0800

    HDDS-7250. Add HttpServer metrics (#3772)
---
 .../hadoop/hdds/server/http/HttpServer2.java       |  3 +
 .../hdds/server/http/HttpServer2Metrics.java       | 94 ++++++++++++++++++++++
 .../server/http/TestHttpServer2MetricsTest.java    | 94 ++++++++++++++++++++++
 3 files changed, 191 insertions(+)

diff --git 
a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/http/HttpServer2.java
 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/http/HttpServer2.java
index 9c241ca278..d9330bd360 100644
--- 
a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/http/HttpServer2.java
+++ 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/http/HttpServer2.java
@@ -194,6 +194,7 @@ public final class HttpServer2 implements FilterContainer {
   static final String STATE_DESCRIPTION_NOT_LIVE = " - not live";
   private final SignerSecretProvider secretProvider;
   private XFrameOption xFrameOption;
+  private HttpServer2Metrics metrics;
   private boolean xFrameOptionIsEnabled;
   public static final String HTTP_HEADER_PREFIX = "ozone.http.header.";
   private static final String HTTP_HEADER_REGEX =
@@ -604,6 +605,7 @@ public final class HttpServer2 implements FilterContainer {
       threadPool.setMaxThreads(maxThreads);
     }
 
+    metrics = HttpServer2Metrics.create(threadPool, name);
     SessionHandler handler = webAppContext.getSessionHandler();
     handler.setHttpOnly(true);
     handler.getSessionCookieConfig().setSecure(true);
@@ -1365,6 +1367,7 @@ public final class HttpServer2 implements FilterContainer 
{
       // clear & stop webAppContext attributes to avoid memory leaks.
       webAppContext.clearAttributes();
       webAppContext.stop();
+      metrics.unRegister();
     } catch (Exception e) {
       LOG.error("Error while stopping web app context for webapp "
           + webAppContext.getDisplayName(), e);
diff --git 
a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/http/HttpServer2Metrics.java
 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/http/HttpServer2Metrics.java
new file mode 100644
index 0000000000..7c0480a7d8
--- /dev/null
+++ 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/http/HttpServer2Metrics.java
@@ -0,0 +1,94 @@
+/**
+ * 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.hdds.server.http;
+
+import org.apache.hadoop.hdds.annotation.InterfaceAudience;
+import org.apache.hadoop.metrics2.MetricsCollector;
+import org.apache.hadoop.metrics2.MetricsInfo;
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
+import org.apache.hadoop.metrics2.MetricsSource;
+import org.apache.hadoop.metrics2.MetricsSystem;
+import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
+import org.eclipse.jetty.util.thread.QueuedThreadPool;
+
+/**
+ * Metrics related to HttpServer threadPool.
+ */
[email protected]
+public final class HttpServer2Metrics implements MetricsSource {
+  enum HttpServer2MetricsInfo implements MetricsInfo {
+    SERVER_NAME("HttpServer2 Metrics."),
+    HttpServerThreadCount("Number of threads in the pool."),
+    HttpServerIdleThreadCount("Number of idle threads but not reserved."),
+    HttpServerMaxThreadCount("Maximum number of threads in the pool."),
+    HttpServerThreadQueueWaitingTaskCount(
+        "The number of jobs in the queue waiting for a thread");
+
+    private final String desc;
+
+    HttpServer2MetricsInfo(String desc) {
+      this.desc = desc;
+    }
+
+    @Override
+    public String description() {
+      return desc;
+    }
+  }
+
+  public static final String SOURCE_NAME =
+      HttpServer2Metrics.class.getSimpleName();
+
+  public static final String NAME = HttpServer2Metrics.class.getSimpleName();
+
+  private final QueuedThreadPool threadPool;
+  private final String name;
+
+  private HttpServer2Metrics(QueuedThreadPool threadPool, String name) {
+    this.threadPool = threadPool;
+    this.name = name;
+  }
+
+  public static HttpServer2Metrics create(
+      QueuedThreadPool threadPool, String name) {
+    MetricsSystem ms = DefaultMetricsSystem.instance();
+    return ms.register(NAME, "HttpServer2 Metrics",
+        new HttpServer2Metrics(threadPool, name));
+  }
+
+  @Override
+  public void getMetrics(MetricsCollector collector, boolean all) {
+    MetricsRecordBuilder recordBuilder = collector.addRecord(SOURCE_NAME)
+        .setContext("HttpServer2")
+        .tag(HttpServer2MetricsInfo.SERVER_NAME, name);
+
+    recordBuilder.addGauge(HttpServer2MetricsInfo.HttpServerThreadCount,
+            threadPool.getThreads())
+        .addGauge(HttpServer2MetricsInfo.HttpServerIdleThreadCount,
+            threadPool.getIdleThreads())
+        .addGauge(HttpServer2MetricsInfo.HttpServerMaxThreadCount,
+            threadPool.getMaxThreads())
+        .addGauge(HttpServer2MetricsInfo.HttpServerThreadQueueWaitingTaskCount,
+            threadPool.getQueueSize());
+  }
+
+  public void unRegister() {
+    MetricsSystem ms = DefaultMetricsSystem.instance();
+    ms.unregisterSource(NAME);
+  }
+}
diff --git 
a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/http/TestHttpServer2MetricsTest.java
 
b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/http/TestHttpServer2MetricsTest.java
new file mode 100644
index 0000000000..9260b76c38
--- /dev/null
+++ 
b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/http/TestHttpServer2MetricsTest.java
@@ -0,0 +1,94 @@
+/**
+ * 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.hdds.server.http;
+
+import static 
org.apache.hadoop.hdds.server.http.HttpServer2Metrics.HttpServer2MetricsInfo.HttpServerIdleThreadCount;
+import static 
org.apache.hadoop.hdds.server.http.HttpServer2Metrics.HttpServer2MetricsInfo.HttpServerMaxThreadCount;
+import static 
org.apache.hadoop.hdds.server.http.HttpServer2Metrics.HttpServer2MetricsInfo.HttpServerThreadCount;
+import static 
org.apache.hadoop.hdds.server.http.HttpServer2Metrics.HttpServer2MetricsInfo.HttpServerThreadQueueWaitingTaskCount;
+import static 
org.apache.hadoop.hdds.server.http.HttpServer2Metrics.HttpServer2MetricsInfo.SERVER_NAME;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.apache.hadoop.metrics2.MetricsCollector;
+import org.apache.hadoop.metrics2.MetricsInfo;
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
+import org.eclipse.jetty.util.thread.QueuedThreadPool;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import java.util.Random;
+
+/**
+ * Testing HttpServer2Metrics.
+ */
+public class TestHttpServer2MetricsTest {
+
+  private QueuedThreadPool threadPool;
+  private MetricsCollector metricsCollector;
+  private MetricsRecordBuilder recorder;
+
+  @Before
+  public void setup() {
+    threadPool = Mockito.mock(QueuedThreadPool.class);
+    metricsCollector = Mockito.mock(MetricsCollector.class);
+    recorder = mock(MetricsRecordBuilder.class);
+  }
+
+  @Test
+  public void testMetrics() {
+    // crate mock metrics
+    Random random = new Random();
+    int threadCount = random.nextInt();
+    int maxThreadCount = random.nextInt();
+    int idleThreadCount = random.nextInt();
+    int threadQueueWaitingTaskCount = random.nextInt();
+    String name = "s3g";
+
+    Mockito.when(threadPool.getThreads()).thenReturn(threadCount);
+    Mockito.when(threadPool.getMaxThreads()).thenReturn(maxThreadCount);
+    Mockito.when(threadPool.getIdleThreads()).thenReturn(idleThreadCount);
+    Mockito.when(threadPool.getQueueSize())
+            .thenReturn(threadQueueWaitingTaskCount);
+    when(recorder.addGauge(any(MetricsInfo.class), anyInt()))
+        .thenReturn(recorder);
+    when(recorder.setContext(anyString())).thenReturn(recorder);
+    when(recorder.tag(any(MetricsInfo.class), anyString()))
+        .thenReturn(recorder);
+    when(metricsCollector.addRecord(anyString())).thenReturn(recorder);
+
+    // get metrics
+    HttpServer2Metrics server2Metrics =
+        HttpServer2Metrics.create(threadPool, name);
+    server2Metrics.getMetrics(metricsCollector, true);
+
+    // verify
+    verify(recorder).tag(SERVER_NAME, name);
+    verify(metricsCollector).addRecord(HttpServer2Metrics.SOURCE_NAME);
+    verify(recorder).addGauge(HttpServerThreadCount, threadCount);
+    verify(recorder).addGauge(HttpServerMaxThreadCount, maxThreadCount);
+    verify(recorder).addGauge(HttpServerIdleThreadCount, idleThreadCount);
+    verify(recorder).addGauge(HttpServerThreadQueueWaitingTaskCount,
+        threadQueueWaitingTaskCount);
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to