You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by GitBox <gi...@apache.org> on 2022/08/01 12:19:54 UTC

[GitHub] [inlong] Greedyu commented on a diff in pull request #5301: [INLONG-4958][Agent] Unify the exposed metrics for the Agent

Greedyu commented on code in PR #5301:
URL: https://github.com/apache/inlong/pull/5301#discussion_r934464070


##########
inlong-agent/agent-common/src/main/java/org/apache/inlong/agent/metrics/AgentMetricSingleton.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.agent.metrics;
+
+import org.apache.commons.lang3.ClassUtils;
+import org.apache.inlong.agent.conf.AgentConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.inlong.agent.constant.AgentConstants.AGENT_METRIC_LISTENER_CLASS;
+import static org.apache.inlong.agent.constant.AgentConstants.AGENT_METRIC_LISTENER_CLASS_DEFAULT;
+
+/**
+ * metric singleton
+ */
+public class AgentMetricSingleton {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(AgentMetricSingleton.class);
+    private static AgentMetricBaseHandler agentMetricBaseHandler;
+
+    private AgentMetricSingleton() {
+    }
+
+    public static AgentMetricBaseHandler getAgentMetricHandler() {
+        if (agentMetricBaseHandler == null) {
+            synchronized (AgentJmxMetricHandler.class) {
+                if (agentMetricBaseHandler == null) {
+                    agentMetricBaseHandler = getAgentMetricByConf();
+                    agentMetricBaseHandler.init();
+                }
+            }
+        }
+        return agentMetricBaseHandler;
+    }
+
+    private static AgentMetricBaseHandler getAgentMetricByConf() {
+        AgentConfiguration conf = AgentConfiguration.getAgentConf();
+        try {
+            Class<?> handlerClass = ClassUtils
+                    .getClass(conf.get(AGENT_METRIC_LISTENER_CLASS, AGENT_METRIC_LISTENER_CLASS_DEFAULT));
+            Object handlerObject = handlerClass.getDeclaredConstructor().newInstance();
+            //
+            // final MetricListener listener = (MetricListener) listenerObject;
+            // Constructor<?> constructor =
+            //         Class.forName(conf.get(AGENT_METRIC_LISTENER_CLASS, AGENT_METRIC_LISTENER_CLASS_DEFAULT))
+            //                 .getDeclaredConstructor(AgentMetricBaseHandler.class);
+            // constructor.setAccessible(true);
+            return (AgentMetricBaseHandler) handlerObject;
+        } catch (Exception ex) {
+            LOGGER.error("cannot find AgentMetricBaseHandler, {}", ex.getMessage());
+        }
+        return null;
+    }
+
+    public static void init() {
+        getAgentMetricHandler();

Review Comment:
   But this init() method is not AgentMetricSingleton, but the AgentMetricHandler class
   However, the init() here is a bit redundant and can also be removed



-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org