duongkame commented on code in PR #4044:
URL: https://github.com/apache/ozone/pull/4044#discussion_r1066153820


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/grpc/metrics/GrpcMetrics.java:
##########
@@ -0,0 +1,168 @@
+/**
+ * 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.grpc.metrics;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.metrics2.annotation.Metric;
+import org.apache.hadoop.metrics2.annotation.Metrics;
+import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
+import org.apache.hadoop.metrics2.lib.MutableGaugeInt;
+import org.apache.hadoop.metrics2.lib.MetricsRegistry;
+import org.apache.hadoop.metrics2.lib.MutableQuantiles;
+import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
+import org.apache.hadoop.metrics2.lib.MutableRate;
+import org.apache.hadoop.ozone.OzoneConfigKeys;
+import org.apache.hadoop.ozone.OzoneConsts;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Class which maintains metrics related to using GRPC.
+ */
+@Metrics(about = "GRPC Metrics", context = OzoneConsts.OZONE)
+public class GrpcMetrics {
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(GrpcMetrics.class);
+  private static final String SOURCE_NAME =
+      GrpcMetrics.class.getSimpleName();
+
+  private final MetricsRegistry registry;
+  private final boolean grpcQuantileEnable;
+
+  public GrpcMetrics(Configuration conf) {
+    registry = new MetricsRegistry("grpc");
+    int[] intervals = conf.getInts(
+        OzoneConfigKeys.OZONE_GPRC_METRICS_PERCENTILES_INTERVALS_KEY);
+    grpcQuantileEnable = (intervals.length > 0);
+    if (grpcQuantileEnable) {
+      grpcQueueTimeMillisQuantiles =
+          new MutableQuantiles[intervals.length];
+      grpcProcessingTimeMillisQuantiles =
+          new MutableQuantiles[intervals.length];
+      for (int i = 0; i < intervals.length; i++) {
+        int interval = intervals[i];
+        grpcProcessingTimeMillisQuantiles[i] = registry
+            .newQuantiles("grpcQueueTime" + interval
+                    + "s", "grpc queue time in milli second", "ops",

Review Comment:
   ```suggestion
                       + "s", "grpc queue time in millisecond", "ops",
   ```



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/grpc/metrics/GrpcMetrics.java:
##########
@@ -0,0 +1,168 @@
+/**
+ * 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.grpc.metrics;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.metrics2.annotation.Metric;
+import org.apache.hadoop.metrics2.annotation.Metrics;
+import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
+import org.apache.hadoop.metrics2.lib.MutableGaugeInt;
+import org.apache.hadoop.metrics2.lib.MetricsRegistry;
+import org.apache.hadoop.metrics2.lib.MutableQuantiles;
+import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
+import org.apache.hadoop.metrics2.lib.MutableRate;
+import org.apache.hadoop.ozone.OzoneConfigKeys;
+import org.apache.hadoop.ozone.OzoneConsts;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Class which maintains metrics related to using GRPC.
+ */
+@Metrics(about = "GRPC Metrics", context = OzoneConsts.OZONE)
+public class GrpcMetrics {
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(GrpcMetrics.class);
+  private static final String SOURCE_NAME =
+      GrpcMetrics.class.getSimpleName();
+
+  private final MetricsRegistry registry;
+  private final boolean grpcQuantileEnable;
+
+  public GrpcMetrics(Configuration conf) {
+    registry = new MetricsRegistry("grpc");
+    int[] intervals = conf.getInts(
+        OzoneConfigKeys.OZONE_GPRC_METRICS_PERCENTILES_INTERVALS_KEY);
+    grpcQuantileEnable = (intervals.length > 0);
+    if (grpcQuantileEnable) {
+      grpcQueueTimeMillisQuantiles =
+          new MutableQuantiles[intervals.length];
+      grpcProcessingTimeMillisQuantiles =
+          new MutableQuantiles[intervals.length];
+      for (int i = 0; i < intervals.length; i++) {
+        int interval = intervals[i];
+        grpcProcessingTimeMillisQuantiles[i] = registry
+            .newQuantiles("grpcQueueTime" + interval
+                    + "s", "grpc queue time in milli second", "ops",
+                "latency", interval);
+        grpcProcessingTimeMillisQuantiles[i] = registry.newQuantiles(
+            "grpcProcessingTime" + interval + "s",
+            "grpc processing time in milli second",
+            "ops", "latency", interval);
+      }
+    }
+    LOG.debug("Initialized " + registry);
+  }
+
+  /**
+   * Create and return GrpcMetrics instance.
+   * @param conf
+   * @return GrpcMetrics
+   */
+  public static synchronized GrpcMetrics create(Configuration conf) {
+    GrpcMetrics metrics = new GrpcMetrics(conf);
+    return DefaultMetricsSystem.instance().register(SOURCE_NAME,
+        "Metrics for using gRPC", metrics);
+  }
+
+  /**
+   * Unregister the metrics instance.
+   */
+  public void unRegister() {
+    DefaultMetricsSystem.instance().unregisterSource(SOURCE_NAME);
+  }
+
+  @Metric("Number of sent bytes")
+  private MutableGaugeLong sentBytes;
+
+  @Metric("Number of received bytes")
+  private MutableGaugeLong receivedBytes;
+
+  @Metric("Queue time")
+  private MutableRate grpcQueueTime;
+
+  // There should be no getter method to avoid
+  // exposing internal representation. FindBugs error raised.
+  private MutableQuantiles[] grpcQueueTimeMillisQuantiles;
+
+  @Metric("Processsing time")
+  private MutableRate grpcProcessingTime;
+
+  // There should be no getter method to avoid
+  // exposing internal representation. FindBugs error raised.
+  private MutableQuantiles[] grpcProcessingTimeMillisQuantiles;
+
+  @Metric("Number of active clients connected")
+  private MutableGaugeInt numOpenClientConnections;
+
+  public void setSentBytes(long byteCount) {

Review Comment:
   If this metric is a Gauge, does it always displays the size of a single 
message?



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/grpc/metrics/GrpcMetricsServerRequestInterceptor.java:
##########
@@ -0,0 +1,101 @@
+/**
+ * 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.grpc.metrics;
+
+import com.google.protobuf.AbstractMessage;
+import io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener;
+import io.grpc.Metadata;
+import io.grpc.ServerCall;
+import io.grpc.ServerCallHandler;
+import io.grpc.ServerInterceptor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Interceptor to gather metrics based on grpc server request.
+ */
+public class GrpcMetricsServerRequestInterceptor implements ServerInterceptor {
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(
+          GrpcMetricsServerRequestInterceptor.class);
+
+  private final GrpcMetrics grpcMetrics;
+  private long bytesReceived;
+  private long receivedTime;
+  private long startTime;
+  private long endTime;
+
+  public GrpcMetricsServerRequestInterceptor(
+      GrpcMetrics grpcMetrics) {
+    super();
+    this.grpcMetrics = grpcMetrics;
+    this.bytesReceived = 0;
+  }
+
+  @Override
+  public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
+      ServerCall<ReqT, RespT> serverCall, Metadata headers,
+      ServerCallHandler<ReqT, RespT> serverCallHandler) {
+
+    // received time
+    receivedTime = System.nanoTime();
+
+    return new SimpleForwardingServerCallListener<ReqT>(
+        serverCallHandler.startCall(serverCall, headers)) {
+
+      @Override
+      public void onMessage(ReqT message) {
+        // start time
+        startTime = System.nanoTime();
+
+        long messageSize = 0;
+        if (message instanceof AbstractMessage) {
+          AbstractMessage parsedMessage = (AbstractMessage) message;
+          messageSize += parsedMessage.getSerializedSize();
+        } else {
+          LOG.error("Unable to register number of bytes received. " +

Review Comment:
   nit: this could bombard the logs if the case happens, and it's not really an 
`ERROR`. It's better to emit a counter indicating message size is not known. 
This helps verify if we miss any cases.
   ```
   grpcMetrics.addUnknownMessages(1);
   ```



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/grpc/metrics/GrpcMetrics.java:
##########
@@ -0,0 +1,168 @@
+/**
+ * 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.grpc.metrics;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.metrics2.annotation.Metric;
+import org.apache.hadoop.metrics2.annotation.Metrics;
+import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
+import org.apache.hadoop.metrics2.lib.MutableGaugeInt;
+import org.apache.hadoop.metrics2.lib.MetricsRegistry;
+import org.apache.hadoop.metrics2.lib.MutableQuantiles;
+import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
+import org.apache.hadoop.metrics2.lib.MutableRate;
+import org.apache.hadoop.ozone.OzoneConfigKeys;
+import org.apache.hadoop.ozone.OzoneConsts;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Class which maintains metrics related to using GRPC.
+ */
+@Metrics(about = "GRPC Metrics", context = OzoneConsts.OZONE)
+public class GrpcMetrics {
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(GrpcMetrics.class);
+  private static final String SOURCE_NAME =
+      GrpcMetrics.class.getSimpleName();
+
+  private final MetricsRegistry registry;
+  private final boolean grpcQuantileEnable;
+
+  public GrpcMetrics(Configuration conf) {
+    registry = new MetricsRegistry("grpc");
+    int[] intervals = conf.getInts(
+        OzoneConfigKeys.OZONE_GPRC_METRICS_PERCENTILES_INTERVALS_KEY);
+    grpcQuantileEnable = (intervals.length > 0);
+    if (grpcQuantileEnable) {
+      grpcQueueTimeMillisQuantiles =
+          new MutableQuantiles[intervals.length];
+      grpcProcessingTimeMillisQuantiles =
+          new MutableQuantiles[intervals.length];
+      for (int i = 0; i < intervals.length; i++) {
+        int interval = intervals[i];
+        grpcProcessingTimeMillisQuantiles[i] = registry
+            .newQuantiles("grpcQueueTime" + interval
+                    + "s", "grpc queue time in milli second", "ops",
+                "latency", interval);
+        grpcProcessingTimeMillisQuantiles[i] = registry.newQuantiles(
+            "grpcProcessingTime" + interval + "s",
+            "grpc processing time in milli second",

Review Comment:
   ```suggestion
               "grpc processing time in millisecond",
   ```



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