inter12 commented on a change in pull request #1788:
URL: https://github.com/apache/incubator-inlong/pull/1788#discussion_r748035100



##########
File path: 
inlong-common/src/main/java/org/apache/inlong/commons/config/metrics/MetricItemSet.java
##########
@@ -0,0 +1,75 @@
+/**
+ * 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.inlong.commons.config.metrics;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * 
+ * MetricItemSet
+ */
+public abstract class MetricItemSet<T extends MetricItem> implements 
MetricItemSetMBean {
+
+    protected Map<String, T> itemMap = new ConcurrentHashMap<>();
+
+    /**
+     * createItem
+     * 
+     * @return
+     */
+    protected abstract T createItem();
+
+    /**
+     * findMetricItem
+     * 
+     * @param item
+     */
+    public T findMetricItem(Map<String, String> dimensions) {
+        String key = MetricUtils.getDimensionsKey(dimensions);
+        T currentItem = this.itemMap.get(key);
+        if (currentItem != null) {
+            return currentItem;
+        }
+        currentItem = createItem();
+        currentItem.setDimensions(dimensions);
+        T oldItem = this.itemMap.putIfAbsent(key, currentItem);
+        if (oldItem == null) {

Review comment:
       这里用三目表达式是否更好?

##########
File path: 
inlong-common/src/main/java/org/apache/inlong/commons/config/metrics/MetricItemSet.java
##########
@@ -0,0 +1,75 @@
+/**
+ * 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.inlong.commons.config.metrics;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * 
+ * MetricItemSet

Review comment:
       这些类 不需要作者信息?

##########
File path: 
inlong-common/src/main/java/org/apache/inlong/commons/config/metrics/MetricItem.java
##########
@@ -0,0 +1,184 @@
+/**
+ * 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.inlong.commons.config.metrics;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * 
+ * MetricItem
+ */
+public class MetricItem implements MetricItemMBean {
+
+    private String key;
+    private Map<String, String> dimensions;
+    private Map<String, AtomicLong> countMetrics;
+    private Map<String, AtomicLong> gaugeMetrics;
+
+    /**
+     * getDimensionsKey
+     * 
+     * @return
+     */
+    @Override
+    public String getDimensionsKey() {
+        if (key != null) {
+            return key;
+        }
+        //
+        Map<String, String> dimensionMap = this.getDimensions();
+        this.key = MetricUtils.getDimensionsKey(dimensionMap);
+        return key;
+    }
+
+    /**
+     * getDimensions
+     * 
+     * @return
+     */
+    @Override
+    public Map<String, String> getDimensions() {
+        if (dimensions != null) {
+            return dimensions;
+        }
+        dimensions = new HashMap<>();
+
+        // get all fields
+        List<Field> fields = 
getDeclaredFieldsIncludingInherited(this.getClass());
+
+        // filter dimension fields
+        for (Field field : fields) {
+            field.setAccessible(true);
+            for (Annotation fieldAnnotation : field.getAnnotations()) {
+                if (fieldAnnotation instanceof Dimension) {
+                    Dimension dimension = (Dimension) fieldAnnotation;
+                    String name = dimension.name();
+                    name = (name != null && name.length() > 0) ? name : 
field.getName();
+                    try {
+                        Object fieldValue = field.get(this);
+                        String value = (fieldValue == null) ? "" : 
fieldValue.toString();
+                        dimensions.put(name, value);
+                    } catch (Throwable t) {
+                        t.printStackTrace();

Review comment:
       直接print异常是否合适?

##########
File path: 
inlong-common/src/main/java/org/apache/inlong/commons/config/metrics/MetricItemMBean.java
##########
@@ -0,0 +1,55 @@
+/**
+ * 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.inlong.commons.config.metrics;
+
+import java.util.Map;
+
+/**
+ * 
+ * MetricMBean
+ */
+public interface MetricItemMBean {
+
+    String METHOD_KEY = "getDimensionsKey";
+    String METHOD_DIMENSIONS = "getDimensions";
+    String METHOD_METRICS = "getMetrics";
+    char DOMAIN_SEPARATOR = ':';
+    char PROPERTY_SEPARATOR = ',';
+    char PROPERTY_EQUAL = '=';
+
+    /**
+     * getDimensionsKey
+     * 
+     * @return
+     */
+    String getDimensionsKey();

Review comment:
       方法的注释 是否可以更详细?

##########
File path: 
inlong-common/src/main/java/org/apache/inlong/commons/config/metrics/MetricValue.java
##########
@@ -0,0 +1,48 @@
+/**
+ * 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.inlong.commons.config.metrics;
+
+/**
+ * 
+ * MetricValue
+ */
+public class MetricValue {
+
+    public String name;

Review comment:
       属性是否应该private

##########
File path: 
inlong-common/src/main/java/org/apache/inlong/commons/config/metrics/MetricItem.java
##########
@@ -0,0 +1,184 @@
+/**
+ * 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.inlong.commons.config.metrics;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * 
+ * MetricItem
+ */
+public class MetricItem implements MetricItemMBean {
+
+    private String key;
+    private Map<String, String> dimensions;
+    private Map<String, AtomicLong> countMetrics;
+    private Map<String, AtomicLong> gaugeMetrics;
+
+    /**
+     * getDimensionsKey
+     * 
+     * @return
+     */
+    @Override
+    public String getDimensionsKey() {
+        if (key != null) {
+            return key;
+        }
+        //
+        Map<String, String> dimensionMap = this.getDimensions();
+        this.key = MetricUtils.getDimensionsKey(dimensionMap);
+        return key;
+    }
+
+    /**
+     * getDimensions
+     * 
+     * @return
+     */
+    @Override
+    public Map<String, String> getDimensions() {
+        if (dimensions != null) {
+            return dimensions;
+        }
+        dimensions = new HashMap<>();
+
+        // get all fields
+        List<Field> fields = 
getDeclaredFieldsIncludingInherited(this.getClass());
+
+        // filter dimension fields
+        for (Field field : fields) {
+            field.setAccessible(true);
+            for (Annotation fieldAnnotation : field.getAnnotations()) {
+                if (fieldAnnotation instanceof Dimension) {
+                    Dimension dimension = (Dimension) fieldAnnotation;
+                    String name = dimension.name();
+                    name = (name != null && name.length() > 0) ? name : 
field.getName();
+                    try {
+                        Object fieldValue = field.get(this);
+                        String value = (fieldValue == null) ? "" : 
fieldValue.toString();
+                        dimensions.put(name, value);
+                    } catch (Throwable t) {
+                        t.printStackTrace();
+                    }
+                    break;
+                }
+            }
+        }
+        return dimensions;
+    }
+
+    /**
+     * set dimensions
+     * 
+     * @param dimensions the dimensions to set
+     */
+    public void setDimensions(Map<String, String> dimensions) {
+        this.dimensions = new HashMap<String, String>();
+        this.dimensions.putAll(dimensions);
+    }
+
+    /**
+     * snapshot
+     * 
+     * @return
+     */
+    @Override
+    public Map<String, MetricValue> snapshot() {
+        if (this.countMetrics == null || this.gaugeMetrics == null) {
+            this.initMetricField();
+        }
+        //
+        Map<String, MetricValue> metrics = new HashMap<>();
+        this.countMetrics.forEach((key, value) -> {
+            metrics.put(key, MetricValue.of(key, value.getAndSet(0)));
+        });
+        this.gaugeMetrics.forEach((key, value) -> {
+            metrics.put(key, MetricValue.of(key, value.get()));
+        });
+        return metrics;
+    }
+
+    /**
+     * initMetricField
+     */
+    protected void initMetricField() {
+        this.countMetrics = new HashMap<>();
+        this.gaugeMetrics = new HashMap<>();
+
+        // get all fields
+        List<Field> fields = 
getDeclaredFieldsIncludingInherited(this.getClass());
+
+        // filter metric fields
+        for (Field field : fields) {
+            field.setAccessible(true);
+            for (Annotation fieldAnnotation : field.getAnnotations()) {
+                if (fieldAnnotation instanceof CountMetric) {
+                    CountMetric countMetric = (CountMetric) fieldAnnotation;
+                    String name = countMetric.name();
+                    name = (name != null && name.length() > 0) ? name : 
field.getName();
+                    try {
+                        Object fieldValue = field.get(this);
+                        if (fieldValue instanceof AtomicLong) {
+                            this.countMetrics.put(name, (AtomicLong) 
fieldValue);
+                        }
+                    } catch (Throwable t) {
+                        t.printStackTrace();
+                    }
+                    break;
+                } else if (fieldAnnotation instanceof GaugeMetric) {
+                    GaugeMetric gaugeMetric = (GaugeMetric) fieldAnnotation;
+                    String name = gaugeMetric.name();
+                    name = (name != null && name.length() > 0) ? name : 
field.getName();
+                    try {
+                        Object fieldValue = field.get(this);
+                        if (fieldValue instanceof AtomicLong) {
+                            this.gaugeMetrics.put(name, (AtomicLong) 
fieldValue);
+                        }
+                    } catch (Throwable t) {
+                        t.printStackTrace();

Review comment:
       这个地方不能直接 输出异常




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


Reply via email to