Skip to content

Commit

Permalink
Support disable metrics init and sync
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbumenJ committed Sep 8, 2023
1 parent 35746aa commit a4ab481
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ public interface MetricsConstants {
String TAG_VERSION_KEY = "version";

String TAG_APPLICATION_VERSION_KEY = "application.version";

String TAG_KEY_KEY = "key";

String TAG_CONFIG_CENTER = "config.center";

String TAG_CHANGE_TYPE = "change.type";

String ENABLE_JVM_METRICS_KEY = "enable.jvm";

String ENABLE_COLLECTOR_SYNC_KEY = "enable.collector.sync";

String AGGREGATION_COLLECTOR_KEY = "aggregation";

String AGGREGATION_ENABLED_KEY = "aggregation.enabled";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ public class MetricsConfig extends AbstractConfig {
*/
private Boolean exportMetricsService;

/**
* Enable metrics init.
*/
private Boolean enableMetricsInit;

/**
* Enable collector sync.
*/
private Boolean enableCollectorSync;

/**
* @deprecated After metrics config is refactored.
* This parameter should no longer use and will be deleted in the future.
Expand Down Expand Up @@ -208,6 +218,22 @@ public void setEnableThreadpool(Boolean enableThreadpool) {
this.enableThreadpool = enableThreadpool;
}

public Boolean getEnableMetricsInit() {
return enableMetricsInit;
}

public void setEnableMetricsInit(Boolean enableMetricsInit) {
this.enableMetricsInit = enableMetricsInit;
}

public Boolean getEnableCollectorSync() {
return enableCollectorSync;
}

public void setEnableCollectorSync(Boolean enableCollectorSync) {
this.enableCollectorSync = enableCollectorSync;
}

public Boolean getUseGlobalRegistry() {
return useGlobalRegistry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ private void initMetricsReporter() {
collector.setCollectEnabled(true);
collector.collectApplication();
collector.setThreadpoolCollectEnabled(Optional.ofNullable(metricsConfig.getEnableThreadpool()).orElse(true));
collector.setMetricsInitEnabled(Optional.ofNullable(metricsConfig.getEnableMetricsInit()).orElse(true));
MetricsReporterFactory metricsReporterFactory = getExtensionLoader(MetricsReporterFactory.class).getAdaptiveExtension();
MetricsReporter metricsReporter = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public class DefaultMetricsCollector extends CombMetricsCollector<RequestEvent>
private boolean collectEnabled = false;

private volatile boolean threadpoolCollectEnabled = false;

private volatile boolean metricsInitEnabled = true;

private final ThreadPoolMetricsSampler threadPoolSampler = new ThreadPoolMetricsSampler(this);
private String applicationName;
private final ApplicationModel applicationModel;
Expand Down Expand Up @@ -123,6 +126,14 @@ public void setThreadpoolCollectEnabled(boolean threadpoolCollectEnabled) {
this.threadpoolCollectEnabled = threadpoolCollectEnabled;
}

public boolean isMetricsInitEnabled() {
return metricsInitEnabled;
}

public void setMetricsInitEnabled(boolean metricsInitEnabled) {
this.metricsInitEnabled = metricsInitEnabled;
}

public void collectApplication() {
this.setApplicationName(applicationModel.getApplicationName());
applicationSampler.inc(applicationName, MetricsEvent.Type.APPLICATION_INFO);
Expand Down Expand Up @@ -155,6 +166,9 @@ public boolean isSupport(MetricsEvent event) {
@Override
public void onEvent(TimeCounterEvent event) {
if(event instanceof MetricsInitEvent){
if (!metricsInitEnabled) {
return;
}
if(initialized.compareAndSet(false,true)) {
collectors.addAll(applicationModel.getBeanFactory().getBeansOfType(MetricsCollector.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@

package org.apache.dubbo.metrics.report;

import io.micrometer.core.instrument.FunctionCounter;
import io.micrometer.core.instrument.binder.MeterBinder;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.beans.factory.ScopeBeanFactory;
import org.apache.dubbo.common.lang.ShutdownHookCallbacks;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.metrics.MetricsGlobalRegistry;
import org.apache.dubbo.common.utils.NamedThreadFactory;
import org.apache.dubbo.metrics.MetricsGlobalRegistry;
import org.apache.dubbo.metrics.collector.AggregateMetricsCollector;
import org.apache.dubbo.metrics.collector.MetricsCollector;
import org.apache.dubbo.metrics.collector.HistogramMetricsCollector;
import org.apache.dubbo.metrics.collector.MetricsCollector;
import org.apache.dubbo.metrics.model.sample.CounterMetricSample;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.rpc.model.ApplicationModel;

import io.micrometer.core.instrument.FunctionCounter;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
Expand All @@ -53,6 +53,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

import static org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_METRICS_COLLECTOR_EXCEPTION;
import static org.apache.dubbo.common.constants.MetricsConstants.ENABLE_COLLECTOR_SYNC_KEY;
import static org.apache.dubbo.common.constants.MetricsConstants.ENABLE_JVM_METRICS_KEY;

/**
Expand Down Expand Up @@ -140,9 +141,12 @@ private void initCollectors() {
}

private void scheduleMetricsCollectorSyncJob() {
NamedThreadFactory threadFactory = new NamedThreadFactory("metrics-collector-sync-job", true);
collectorSyncJobExecutor = Executors.newScheduledThreadPool(1, threadFactory);
collectorSyncJobExecutor.scheduleWithFixedDelay(this::refreshData, DEFAULT_SCHEDULE_INITIAL_DELAY, DEFAULT_SCHEDULE_PERIOD, TimeUnit.SECONDS);
boolean enableCollectorSync = url.getParameter(ENABLE_COLLECTOR_SYNC_KEY, true);
if (enableCollectorSync) {
NamedThreadFactory threadFactory = new NamedThreadFactory("metrics-collector-sync-job", true);
collectorSyncJobExecutor = Executors.newScheduledThreadPool(1, threadFactory);
collectorSyncJobExecutor.scheduleWithFixedDelay(this::refreshData, DEFAULT_SCHEDULE_INITIAL_DELAY, DEFAULT_SCHEDULE_PERIOD, TimeUnit.SECONDS);
}
}

@SuppressWarnings({"unchecked", "rawtypes"})
Expand Down

0 comments on commit a4ab481

Please sign in to comment.