kevinw66 commented on a change in pull request #8983:
URL: https://github.com/apache/dubbo/pull/8983#discussion_r763693532



##########
File path: 
dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/AbstractMetricsReporter.java
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.dubbo.metrics;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.lang.ShutdownHookCallbacks;
+import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.common.metrics.MetricsReporter;
+import org.apache.dubbo.common.metrics.collector.DefaultMetricsCollector;
+import org.apache.dubbo.common.metrics.collector.MetricsCollector;
+import org.apache.dubbo.common.metrics.model.sample.GaugeMetricSample;
+import org.apache.dubbo.common.metrics.model.sample.MetricSample;
+import org.apache.dubbo.common.utils.NamedThreadFactory;
+import org.apache.dubbo.metrics.collector.AggregateMetricsCollector;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+
+import io.micrometer.core.instrument.Gauge;
+import io.micrometer.core.instrument.MeterRegistry;
+import io.micrometer.core.instrument.Tag;
+import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics;
+import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
+import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
+import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics;
+import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
+import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import static 
org.apache.dubbo.common.constants.MetricsConstants.ENABLE_JVM_METRICS_KEY;
+
+/**
+ * AbstractMetricsReporter.
+ */
+public abstract class AbstractMetricsReporter implements MetricsReporter {
+
+    private final Logger logger = 
LoggerFactory.getLogger(AbstractMetricsReporter.class);
+
+    protected final URL url;
+    protected final List<MetricsCollector> collectors = new ArrayList<>();
+    protected final CompositeMeterRegistry compositeRegistry = new 
CompositeMeterRegistry();
+
+    private final ApplicationModel applicationModel;
+    private ScheduledExecutorService collectorSyncJobExecutor = null;
+
+    private static final int DEFAULT_SCHEDULE_INITIAL_DELAY = 5;
+    private static final int DEFAULT_SCHEDULE_PERIOD = 30;
+
+    protected AbstractMetricsReporter(URL url, ApplicationModel 
applicationModel) {
+        this.url = url;
+        this.applicationModel = applicationModel;
+    }
+
+    @Override
+    public void init() {
+        addJvmMetrics();
+        initCollectors();
+        scheduleMetricsCollectorSyncJob();
+
+        doInit();
+
+        registerDubboShutdownHook();
+    }
+
+    protected void addMeterRegistry(MeterRegistry registry) {
+        compositeRegistry.add(registry);
+    }
+
+    protected ApplicationModel getApplicationModel() {
+        return applicationModel;
+    }
+
+    private void addJvmMetrics() {
+        boolean enableJvmMetrics = url.getParameter(ENABLE_JVM_METRICS_KEY, 
false);
+        if (enableJvmMetrics) {
+            new ClassLoaderMetrics().bindTo(compositeRegistry);
+            new JvmMemoryMetrics().bindTo(compositeRegistry);
+            new JvmGcMetrics().bindTo(compositeRegistry);
+            new ProcessorMetrics().bindTo(compositeRegistry);
+            new JvmThreadMetrics().bindTo(compositeRegistry);
+        }
+    }
+
+    private void initCollectors() {
+        
applicationModel.getBeanFactory().getOrRegisterBean(AggregateMetricsCollector.class);
+
+        
collectors.add(applicationModel.getBeanFactory().getBean(DefaultMetricsCollector.class));
+        
collectors.add(applicationModel.getBeanFactory().getBean(AggregateMetricsCollector.class));
+    }
+
+    private void scheduleMetricsCollectorSyncJob() {
+        NamedThreadFactory threadFactory = new 
NamedThreadFactory("metrics-collector-sync-job", true);
+        collectorSyncJobExecutor = Executors.newScheduledThreadPool(1, 
threadFactory);
+        collectorSyncJobExecutor.scheduleAtFixedRate(() -> {
+            collectors.forEach(collector -> {
+                List<MetricSample> samples = collector.collect();
+                for (MetricSample sample : samples) {
+                    try {
+                        switch (sample.getType()) {
+                            case GAUGE:
+                                GaugeMetricSample gaugeSample = 
(GaugeMetricSample) sample;
+                                List<Tag> tags = new ArrayList<>();
+                                gaugeSample.getTags().forEach((k, v) -> {
+                                    if (v == null) {
+                                        v = "";
+                                    }
+
+                                    tags.add(Tag.of(k, v));

Review comment:
       This will cause error and thread will be block, but without any 
exception.




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