You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2020/10/01 22:11:14 UTC

[GitHub] [ignite] ololo3000 commented on a change in pull request #8284: IGNITE-13488 Adds command to print metric value.

ololo3000 commented on a change in pull request #8284:
URL: https://github.com/apache/ignite/pull/8284#discussion_r498537708



##########
File path: modules/core/src/main/java/org/apache/ignite/internal/visor/metric/VisorMetricTask.java
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.ignite.internal.visor.metric;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.internal.processors.metric.GridMetricManager;
+import org.apache.ignite.internal.processors.task.GridInternal;
+import org.apache.ignite.internal.processors.task.GridVisorManagementTask;
+import org.apache.ignite.internal.visor.VisorJob;
+import org.apache.ignite.internal.visor.VisorOneNodeTask;
+import org.apache.ignite.spi.metric.BooleanMetric;
+import org.apache.ignite.spi.metric.DoubleMetric;
+import org.apache.ignite.spi.metric.IntMetric;
+import org.apache.ignite.spi.metric.LongMetric;
+import org.apache.ignite.spi.metric.Metric;
+import org.apache.ignite.spi.metric.ObjectMetric;
+import org.apache.ignite.spi.metric.ReadOnlyMetricRegistry;
+import org.jetbrains.annotations.Nullable;
+
+import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.SEPARATOR;
+import static org.apache.ignite.spi.metric.jmx.MetricRegistryMBean.searchHistogram;
+
+/** Reperesents visor task for obtaining metric values. */
+@GridInternal
+@GridVisorManagementTask
+public class VisorMetricTask extends VisorOneNodeTask<VisorMetricTaskArg, Map<String, ?>> {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** {@inheritDoc} */
+    @Override protected VisorJob<VisorMetricTaskArg, Map<String, ?>> job(VisorMetricTaskArg arg) {
+        return new VisorMetricJob(arg, false);
+    }
+
+    /** */
+    private static class VisorMetricJob extends VisorJob<VisorMetricTaskArg, Map<String, ?>> {
+        /** */
+        private static final long serialVersionUID = 0L;
+
+        /**
+         * Create job with specified argument.
+         *
+         * @param arg   Job argument.
+         * @param debug Flag indicating whether debug information should be printed into node log.
+         */
+        protected VisorMetricJob(@Nullable VisorMetricTaskArg arg, boolean debug) {
+            super(arg, debug);
+        }
+
+        /** {@inheritDoc} */
+        @Override protected Map<String, ?> run(@Nullable VisorMetricTaskArg arg) throws IgniteException {
+            String name = arg.name();
+
+            GridMetricManager mMgr = ignite.context().metric();
+
+            for (ReadOnlyMetricRegistry mReg : mMgr) {
+                String mRegName = mReg.name();
+
+                if (mRegName.equals(name)) {
+                    Map<String, Object> res = new HashMap<>();
+
+                    mReg.forEach(metric -> res.put(metric.name(), valueOf(metric)));
+
+                    return res;
+                }
+
+                String mRegPrefix = mRegName + SEPARATOR;
+
+                if (!name.startsWith(mRegPrefix))
+                    continue;
+
+                if (mRegPrefix.equals(name))
+                    return null;
+
+                String metricName = name.substring(mRegPrefix.length());
+
+                Metric metric = mReg.findMetric(metricName);
+
+                if (metric != null)
+                    return Collections.singletonMap(name, valueOf(metric));
+
+                Object val = searchHistogram(metricName, mReg);
+
+                if (val != null)
+                    return Collections.singletonMap(name, val);
+            }
+
+            return null;
+        }
+
+        /**
+         * Obtains value of the metric.
+         *
+         * @param metric Metric which value should be obtained.
+         * @return Value of the metric.
+         */
+        private Object valueOf(Metric metric) {

Review comment:
       I haven't used getAsString() method for primitive types to reduce size of response message and avoid unnecessary converting on the server side.   




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